javascript - 带诺克的 Mocha 茶 : why am I getting timeout errors when doing cleanup in 'after()' and 'afterEach()' ?

标签 javascript node.js mocha.js nock

我正在 Mocha/柴套件中使用 Nock stub ( mock ?)一个获取请求,它似乎工作正常。但是,当我想在 describe 之后清理并将一切恢复正常时,我正在执行 nock stub ,我收到 Mocha 超时错误。

我对 nock 的设置(我将在每个 it 之前执行此操作,使用不同的 URL,现在我只为其中一个执行此操作)是

it('should succeed for correct nickname, move and gameID with game in progress', () =>
      Promise.resolve()
        .then(_ => {
          nock('http://localhost:8080')
            .post(`/api/user/${nickname}/game/${gameInProgress.id}`)
            .reply(200, {
              message: 'successful move'
            })
          return logic.makeAGameMove(nickname, nickname2, {from: "e2", to: "e4", promotion: "q"}, gameInProgress.id, token)
        })
      .then(res => {
        const {message} = res
        expect(message).to.equal('successful move')
      })

描述的末尾我有

 afterEach(_=> nock.cleanAll())
 after(_=> nock.restore())

但我不断收到以下错误

        "after each" hook for "should succeed for correct nickname, move and gameID with game in progress":
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 
  "after all" hook:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我有点不知所措。我什至尝试将对 nock.cleanAllnock.restore 的调用包装在 Promises 中,但没有帮助。我哪里出错了?

最佳答案

您正在为箭头函数提供一个参数。 Mocha 认为您正在使用 done,您只需完成它,这样就不会使用任何参数。

(()=>nock.cleanAll())

而不是:

(_=>nock.cleanAll())

关于javascript - 带诺克的 Mocha 茶 : why am I getting timeout errors when doing cleanup in 'after()' and 'afterEach()' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52238510/

相关文章:

javascript - Chrome 在高迭代次数上意外减速

node.js - 如何在 Electron 应用程序上正确存储 Google Drive API 的客户端 secret ?

node.js - 无法使用 npm 安装 Firebase CLI

javascript - 如何在 watch 模式下进行任何 mocha 测试之前运行全局设置脚本

javascript - 匹配基本ascii字符的正则表达式

javascript - Angularjs 使用 ng-repeat 和 <th> 标签来重复列标题

node.js - 如何使用 Electron Builder 登录 Windows 安装程序

typescript - 在 VS Code 下进行 mocha 调试以使用 mocha 的 --file 选项启动每个测试

unit-testing - 使用 mocha 进行所有测试后在哪里删除数据库并关闭连接

javascript - javascript代码首先使用setTimeout可以工作,但随后崩溃还是不起作用?