node.js - 尝试从 Jest 单元测试连接到 mongo 时超时

标签 node.js mongodb unit-testing mongoose jestjs

我想用 jest 和 mongoose 编写一些单元测试来验证与 mongo 的数据交互。

我不想在这里模拟 mongoose,因为我特别想验证 mongo 文档的创建/修改/处理方式。

package.json 配置为不模拟 Node 模块:

{
  "jest": {
    "unmockedModulePathPatterns": [
      "node_modules"
    ]
  }
}

在我的实际测试中,我设置了一个 beforeAll() 钩子(Hook)来负责连接到 mongo:

const mongoose = require('mongoose');

describe('MyTest', () => {

  beforeAll((done) => {
    mongoose.connect('mongodb://127.0.0.1:27017/test');

    let db = mongoose.connection;

    db.on('error', (err) => {
      done.fail(err);
    });

    db.once('open', () => {
      done();
    });
  });

  it('has some property', () => {
    // should pass but actually never gets run
    expect(1).toBe(1);
  });
});

这是输出:

/usr/local/bin/node node_modules/jest-cli/bin/jest.js --verbose
Using Jest CLI v0.10.0, jasmine2
 FAIL  src/lib/controllers/my-controller/__tests__/my-test.js (5.403s)
MyTest
  ✕ it has some property

MyTest › it has some property
  - Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
        at Timer.listOnTimeout (timers.js:92:15)
1 test failed, 0 tests passed (1 total in 1 test suite, run time 5.903s)

Process finished with exit code 1

每次测试都会超时,因为 done() 从未在 beforeAll() Hook 中调用 - 没有抛出错误,也没有任何内容打印到控制台。我可以在 beforeAll() Hook 中放置断点来验证代码正在运行。似乎 mongoose 在尝试打开与 Mongo 的连接时挂起,然后 Jest 测试超时。

当我在 jest 环境之外运行类似代码时,它会按预期连接(几乎立即启动)。怀疑这可能会导致问题,我尝试完全禁用 jest 的 automock 功能,但行为保持不变。

我想我错过了一些非常明显的事情......有什么想法会发生什么吗?

  • jest-cli v. 0.10.0
  • Mongoose v. 4.4.11

更新:

  • 尝试用纯 function(done) {} 替换 ES6 箭头函数语法。没有区别。
  • 尝试通过传递 done 参数使测试异步,并在测试完成时调用它。没有区别。
  • errorconnected 事件处理程序声明后尝试调用 mongoose.connect()
  • 尝试注释掉所有与 mongoose 相关的代码,以检查 beforeAll() Hook 是否正常工作 - 确实如此。

最佳答案

Jest-jasmine 不同于 Jasmine
您使用的语法实际上属于 Jasmine 2.0+,而不是 Jest-jasmine。因此,如果您想继续使用 jest,您必须查看 jest 文档以找到答案。
更重要的是,“beforeAll”不是标准的 Jest 注入(inject)变量,参见 jest api .

我用 Jasmine 2.3.4 运行了你的代码,它运行得很好。我 Jest 尝试了 Promise/pit 来完成这项工作,但失败了。

首先,install jasmine .

npm install -g jasmine
mkdir jasmine-test  
cd jasmine-test
jasmine init
jasmine examples
touch spec/mongodbspec.js

这是我的目录结构:

fenqideMacBook-Pro:jasmine-test fenqi$ ls -R
.:
spec/

./spec:
mongodbspec.js  support/

./spec/support:
jasmine.json

然后,编辑 spec/mongodbspec.js,我只需在您的代码中添加一行“'use strict';”。

'use strict';

const mongoose = require('mongoose');

describe('MyTest', () => {

  beforeAll((done) => {
    mongoose.connect('mongodb://127.0.0.1:27017/test');

    let db = mongoose.connection;

    db.on('error', (err) => {
      done.fail(err);
    });

    db.once('open', () => {
      done();
    });
  });

  it('has some property', () => {
    // should pass but actually never gets run
    expect(1).toBe(1);
  });
});

最后,运行“ Jasmine ”:

fenqideMacBook-Pro:jasmine-test fenqi$ jasmine
Started
.


1 spec, 0 failures
Finished in 0.023 seconds

关于node.js - 尝试从 Jest 单元测试连接到 mongo 时超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36424027/

相关文章:

linux - 拦截输入设备发送的数据

node.js - nodeJS ZeroMQ : why cant send message if sock. 未在 setInterval 中发送

mongodb - 如何在更新时间MongoDb中添加字段的总值作为预聚合字段?

node.js - 将多个 csv 导入 mongodb

mongodb - 对象仅保存到 Parse Server 而不是保存到 MongoDB

node.js - -bash : nvm: command not found

node.js - K8s pod 内存高于进程要求

c# - 使用 StreamWriter 作为参数之一对 void 方法进行单元测试

javascript - 为什么我的 Vue 单元测试无法识别函数?

unit-testing - 如何使用Visual Studio 2015预览生成“智能单元测试”?