Jest尚未完成对回调中的done()的测试

时间:2018-12-06 12:48:59

标签: node.js socket.io jestjs

我有两个异步测试,其中一个运行良好,而第二个则无法在超时之前调用异步回调。好吧,实际上我想它确实调用了done()回调,但是一切仍在继续。

第一(正确)如下:

    it('User should be verified', done => {
      const wsClient = require('socket.io-client')('http://localhost:7575/')

      function callback({ user, isNameTaken }) {
        expect(isNameTaken).toBe(false)
        expect(typeof user.id).toBe('string')
        expect(user.name).toBe('Sam')
        done()
      }

      wsClient.emit(VERIFY_USER, 'Sam', callback)
    })

第二个(不正确):

 it('Do not allow duplicate nicknames', done => {
    const wsClient = require('socket.io-client')('http://localhost:7575/')
    function callbackFinal({ user, isNameTaken }) {
      console.log('============ CALLBACK B ===============', new Date())
      console.log({ user, isNameTaken })
      console.log({ done })
      try {
        expect(isNameTaken).toBe(true) // 4. Acutal test
        expect(user).toBe(null)
        done()
        console.log('============ CALLBACK B After done() ===============', new Date())
      } catch (e) {
        done(e)
      }          
    }

    function callback({ user, isNameTaken }) {
      console.log('============ CALLBACK A ===============', new Date())
      wsClient.emit(USER_CONNECTED, user) // 2. Add verified user to connected users list
      wsClient.emit(VERIFY_USER, 'Sam', callbackFinal) // 3. Try verifying user for the second time
    }

    wsClient.emit(VERIFY_USER, 'Sam', callback) // 1. Try verifying user for the first time
  })

第二次测试的控制台输出:

FAIL  test/integration/loggingIn.spec.js (7.398s)
● Console

console.log test/integration/loggingIn.spec.js:12
  BEFORE
console.log test/integration/loggingIn.spec.js:79
  ============ CALLBACK A =============== 2018-12-06T12:00:17.500Z
console.log test/integration/loggingIn.spec.js:69
  ============ CALLBACK B =============== 2018-12-06T12:00:17.520Z
console.log test/integration/loggingIn.spec.js:70
  { user: null, isNameTaken: true }
console.log test/integration/loggingIn.spec.js:71
  { done: { [Function: next] fail: [Function] } }
console.log test/integration/loggingIn.spec.js:75
  ============ CALLBACK B After done() =============== 2018-12-06T12:00:17.530Z

● User connection tests › Do not allow duplicate nicknames

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

日志输出显示已触发回调,应调用done()。我无法获得测试或开玩笑的毛病,或者……好吧,这是怎么回事?

UPD :这是存储库:https://github.com/dsamoylov/jest-issue

0 个答案:

没有答案