在mocha中同步执行事件

executing event synchronously in mocha

我在 mocha 测试中遇到了 运行 异步函数的问题。我在 beforeEach 调用中启动节点服务器,并在执行任何其他 it() 语句之前链接一个客户端套接字以连接到它。

问题是 - 我每次 mocha 调用都会得到不同的输出

这是我的摩卡测试

//httpServer的测试事件

import chai,{expect} from 'chai';
import sinon from 'sinon'
import SocketCluster from 'socketcluster-client';
import testServer from '../../server/server.js';
import net from 'net';
import chaiAsPromised from 'chai-as-promised';

function startServer(port){
  return new Promise(function(resolve,reject){
    resolve(testServer(port))
  })
}

chai.use(chaiAsPromised)

describe('httpServer',() => {

  var client;

  var options = {
    port: 4000
  }

  beforeEach(() => {
    startServer(4000).then(() => {
      console.log('Server started')
      client = SocketCluster.connect(options)
    })
  })

  it('should return Anonymous user if client doesnt send a valid JWT token on user_connected event',() => {

      return client.emit('user_connected',{id_token:false},(err,data) => {
        expect(data).to.eventually.be.a('string');
      })
  })

})

这是第一次测试调用的输出

 httpServer
Test server started on 4000
Server started
user connected
    1) "before each" hook for "should return Anonymous user if client doesnt send a valid JWT token on user_connected event"

  Main page
    ✓ should show a sign-in page if isAuthenticated is false (60ms)
    ✓ should show a welcome text if isAuthenticated is true

  SignUp login
    ✓ should return isAuthenticated=false on SIGNUP_REQUEST
    ✓ should return isAuthenticated=true on SIGNUP_SUCCESS
    ✓ should return isAuthenticated=false and errorMessage on SIGNUP_FAILURE


  5 passing (2s)
  1 failing

  1) httpServer "before each" hook for "should return Anonymous user if client doesnt send a valid JWT token on user_connected event":
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

这是第二次调用输出

  httpServer
    1) should return Anonymous user if client doesnt send a valid JWT token on user_connected event
Test server started on 4000
Server started

  Main page
    ✓ should show a sign-in page if isAuthenticated is false (82ms)
    ✓ should show a welcome text if isAuthenticated is true
user connected

  SignUp login
    ✓ should return isAuthenticated=false on SIGNUP_REQUEST
    ✓ should return isAuthenticated=true on SIGNUP_SUCCESS
    ✓ should return isAuthenticated=false and errorMessage on SIGNUP_FAILURE


  5 passing (347ms)
  1 failing

  1) httpServer should return Anonymous user if client doesnt send a valid JWT token on user_connected event:
     TypeError: Cannot read property 'emit' of undefined
      at Context.<anonymous> (server.test.js:34:14)

如您所见,'user connected' 日志的发生非常随机。我如何控制它同步发生?

我看到的唯一问题是您的 beforeEach 挂钩没有 return 它的承诺。删除大括号使箭头右侧的表达式应该起作用:

beforeEach(() => 
  startServer(4000).then(() => {
    console.log('Server started')
    client = SocketCluster.connect(options)
  })
);

或者这样:

beforeEach(() => {
  return startServer(4000).then(() => {
    console.log('Server started')
    client = SocketCluster.connect(options)
  })
});

适用于测试中异步代码的规则同样适用于挂钩中的异步代码。您必须 return 承诺或调用 done 回调。