Node.js Before块中定义的函数在Mocha中运行测试之前未完成
我正在我的节点项目中使用Mocha/Chai创建一些测试。在一个特定的测试中,我需要首先将一个文档保存到数据库中,然后检索它并对找到的文档运行一些检查 我遇到的问题是,在文档保存到数据库之前,it检查正在运行。我不明白为什么会发生这种情况,因为我正在before块中进行保存-根据Mocha文档,该块应该在同一描述块中的测试之前运行,请参见以下文档:Node.js Before块中定义的函数在Mocha中运行测试之前未完成,node.js,mocha.js,chai,Node.js,Mocha.js,Chai,我正在我的节点项目中使用Mocha/Chai创建一些测试。在一个特定的测试中,我需要首先将一个文档保存到数据库中,然后检索它并对找到的文档运行一些检查 我遇到的问题是,在文档保存到数据库之前,it检查正在运行。我不明白为什么会发生这种情况,因为我正在before块中进行保存-根据Mocha文档,该块应该在同一描述块中的测试之前运行,请参见以下文档: > With its default "BDD"-style interface, Mocha provides the hooks >
> With its default "BDD"-style interface, Mocha provides the hooks
> before(), after(), beforeEach(), and afterEach(). These should be used
> to set up preconditions and clean up after your tests.
>
> describe('hooks', function() { before(function() {
> // runs before all tests in this block });
>
> after(function() {
> // runs after all tests in this block });
>
> beforeEach(function() {
> // runs before each test in this block });
>
> afterEach(function() {
> // runs after each test in this block });
>
> // test cases });
为了澄清这一点,当我第二次运行测试和所有后续的测试时,所有检查都通过了,因为到那时它可以在数据库中找到文档
我错过了什么
这是我的密码:
const assert = require("chai").assert;
const expect = require("chai").expect;
const chai = require("chai");
chai.use(require("chai-datetime"));
const Agenda = require('agenda');
const config = require('./../../configuration');
const url = config.get('MONGO_URL');
const dbName = config.get('MONGO_DATABASE');
const collection = config.get('MONGO_COLLECTION');
const createAgendaJob = require('./../../lib/agenda-jobs/contact-firstname-to-proper-case');
const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(url);
describe("Contact FirstName to Proper Case", async function () {
const jobName = "Contact FirstName To Proper Case";
const testDate = new Date(2019, 01, 01);
let result;
let agenda;
this.timeout(2000);
before(async function () {
const connectionOpts = {
db: {
address: `${url}/${dbName}`,
collection
}
};
agenda = new Agenda(connectionOpts);
await new Promise(resolve => agenda.once('ready', resolve));
await createAgendaJob(agenda); // Here is where I save the job to the DB
});
client.connect(async function (err) {
assert.equal(null, err);
const db = await client.db(dbName);
result = await db.collection("jobs").findOne({
"name": jobName
});
client.close();
});
it("should have a property 'name'", function () {
expect(result).to.have.property("name");
});
it("should have a 'name' of 'Contact FirstName To Proper Case'", function () {
expect(result.name).to.equal("Contact FirstName To Proper Case");
});
it("should have a property 'type'", function () {
expect(result).to.have.property("type");
});
it("should have a 'type' of 'normal'", function () {
expect(result.type).to.equal("normal");
});
it("should have a property 'repeatTimezone'", function () {
expect(result).to.have.property("repeatTimezone");
});
it("should have a property 'repeatInterval'", function () {
expect(result).to.have.property("repeatInterval");
});
it("should have a property 'lastModifiedBy'", function () {
expect(result).to.have.property("lastModifiedBy");
});
it("should have a property 'nextRunAt'", function () {
expect(result).to.have.property("nextRunAt");
});
it("should return a date for the 'nextRunAt' property", function () {
assert.typeOf(result.nextRunAt, "date");
});
it("should 'nextRunAt' to be a date after test date", function () {
expect(result.nextRunAt).to.afterDate(testDate);
});
});
我还尝试将it检查放入后续的“描述”块中,但这也无法得到相同的结果-只有在第二次运行测试文件时才能找到文档:
const assert = require("chai").assert;
const expect = require("chai").expect;
const chai = require("chai");
chai.use(require("chai-datetime"));
const Agenda = require('agenda');
const config = require('./../../configuration');
const url = config.get('MONGO_URL');
const dbName = config.get('MONGO_DATABASE');
const collection = config.get('MONGO_COLLECTION');
const createAgendaJob = require('./../../lib/agenda-jobs/contact-firstname-to-proper-case');
const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(url);
describe("Contact FirstName to Proper Case", async function () {
const jobName = "Contact FirstName To Proper Case";
const testDate = new Date(2019, 01, 01);
let result;
let agenda;
this.timeout(2000);
before(async function () {
const connectionOpts = {
db: {
address: `${url}/${dbName}`,
collection
}
};
agenda = new Agenda(connectionOpts);
await new Promise(resolve => agenda.once('ready', resolve));
await createAgendaJob(agenda); // Here is where I save the job to the DB
});
client.connect(async function (err) {
assert.equal(null, err);
const db = await client.db(dbName);
result = await db.collection("jobs").findOne({
"name": jobName
});
client.close();
});
describe("Check Contact FirstName To ProperCase Found Job", function () {
it("should have a property 'name'", function () {
expect(result).to.have.property("name");
});
it("should have a 'name' of 'Contact FirstName To Proper Case'", function () {
expect(result.name).to.equal("Contact FirstName To Proper Case");
});
it("should have a property 'type'", function () {
expect(result).to.have.property("type");
});
it("should have a 'type' of 'normal'", function () {
expect(result.type).to.equal("normal");
});
it("should have a property 'repeatTimezone'", function () {
expect(result).to.have.property("repeatTimezone");
});
it("should have a property 'repeatInterval'", function () {
expect(result).to.have.property("repeatInterval");
});
it("should have a property 'lastModifiedBy'", function () {
expect(result).to.have.property("lastModifiedBy");
});
it("should have a property 'nextRunAt'", function () {
expect(result).to.have.property("nextRunAt");
});
it("should return a date for the 'nextRunAt' property", function () {
assert.typeOf(result.nextRunAt, "date");
});
it("should 'nextRunAt' to be a date after test date", function () {
expect(result.nextRunAt).to.afterDate(testDate);
});
});
});
它不是在完成之前运行,而是在一切之前运行:
client.connect(async function (err) {
...
});
Mocha,当创建测试时,suide运行如下代码:
调用传递的每个函数来描述
收集所有的前/后*挂钩
这就是实际调用client.connect代码段的时候!
然后,在为每个套件创建整个套件/测试树之后
在*挂钩之前打适当的电话
调用所有its-tests,可能被before/afterAll钩子包围
追索
在您的情况下,每个钩子/测试运行至少都会等待finish,因为您正确地使用了async/await
另外,请注意,这是非常可疑的:
await setTimeout(() => {
console.log('finished pausing for 5 seconds...');
}, 5000);
这并不等待任何事情,因为setTimeout可能会返回timer it number,而且它没有很好地定义wait应该如何处理这些数据,因为这不是承诺。
当然,它不会等待5秒钟
适当的等待如下所示:
await new Promise(resolve => setTimeout(resolve, 5000))