在mocha中同步执行事件

时间:2016-05-06 09:47:48

标签: promise mocha chai es6-promise

我在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)

正如您所看到的,“用户连接”日志非常随机地发生。我如何控制它同步发生?

1 个答案:

答案 0 :(得分:1)

我看到的唯一问题是你的beforeEach挂钩不会返回其承诺。删除大括号以使箭头的右侧表达式应该起作用:

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)
  })
});

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