node.js - 在之前块中定义的文档完成之前运行的 Mocha 测试
问题描述
我正在为我的 Node 应用程序创建一些 mocha 测试。在我的测试中,在检索一些创建的文档之前,我需要先在数据库中创建这些文档。然后我检索它们并对结果进行一些测试。
我注意到的问题是,即使我在第一个before()
块中包含了创建文档所需运行的函数,并且即使我正在等待文档创建函数的结果,我的测试在文档之前运行已完成创建。似乎该before()
块并没有像我认为的那样做。
如何纠正此问题以确保在测试检查运行之前完成文档创建?
const seedJobs = require('./seeder').seedJobs;
const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(`${url}${dbName}${auth}`);
describe("Seeding Script", async function () {
const testDate = new Date(2019, 01, 01);
let db;
before(async function () {
await seedJobs(); // This is the function that creates the docs in the db
return new Promise((resolve, reject) => {
client.connect(async function (err) {
assert.equal(null, err);
if (err) return reject(err);
try {
db = await client.db(dbName);
} catch (error) {
return reject(error);
}
return resolve(db);
});
});
});
// Now I retrieve the created doc and run checks on it
describe("Check VBR Code Update", async function () {
let result;
const jobName = 'VBR Code Update';
this.timeout(2000);
before(async function () {
result = await db.collection(collection).findOne({
name: jobName
});
});
it("should have a property 'name'", async function () {
expect(result).to.have.property("name");
});
it("should have a 'name' of 'VBR Code Update'", async function ()
expect(result.name).to.equal(jobName);
});
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);
});
});
// Other tests
});
解决方案
您将 promise 和 async 混合在一起,这是不需要的。Nodejs 驱动程序支持 async/await所以宁愿保持一致。
我看不到该seedJobs
功能,但假设它按预期工作。我建议您before
按照下面的示例更新函数。
您在初始化日期时也有错误,格式应为:
const testDate = new Date(2019, 1, 1);
请参阅下面的 mongodb 客户端的 init 和 await 的使用:
const mongodb = require('mongodb');
const chai = require('chai');
const expect = chai.expect;
const config = {
db: {
url: 'mongodb://localhost:27017',
database: 'showcase'
}
};
describe("Seeding Script", function () {
const testDate = new Date(2019, 1, 1);
let db;
seedJobs = async () => {
const collections = await db.collections();
if (collections.map(c => c.s.namespace.collection).includes('tests')) {
await db.collection('tests').drop();
}
let bulk = db.collection('tests').initializeUnorderedBulkOp();
const count = 5000000;
for (let i = 0; i < count; i++) {
bulk.insert( { name: `name ${i}`} );
}
let result = await bulk.execute();
expect(result).to.have.property("nInserted").and.to.eq(count);
result = await db.collection('tests').insertOne({
name: 'VBR Code Update'
});
expect(result).to.have.property("insertedCount").and.to.eq(1);
};
before(async function () {
this.timeout(60000);
const connection = await mongodb.MongoClient.connect(config.db.url, {useNewUrlParser: true, useUnifiedTopology: true});
db = connection.db(config.db.database);
await seedJobs();
});
// Now I retrieve the created doc and run checks on it
describe("Check VBR Code Update", async function () {
let result;
const jobName = 'VBR Code Update';
this.timeout(2000);
before(async function () {
result = await db.collection('tests').findOne({
name: jobName
});
});
it("should have a property 'name'", async function () {
expect(result).to.have.property("name");
});
});
});
推荐阅读
- gradle - Gradle 运行 webpack 任务但没有效果
- ios - 在 Xcode 升级后 Flutter iOS 构建失败错误并带有多个命令
- angular - 如何从不同的组件加载 Nebular Angular 6 中的菜单?
- r - R中的ggplot2不会在pdf中生成1000多个图
- java - 如何使用 BorderLayout 缩小我的文本字段并在右下角定位标签?
- components - weka的主成分分析返回错误的实例
- mysql - 使用星号(通配符)运算符执行搜索时,MySQL 将如何使用 FT 索引?
- visual-studio - Visual Studio 表单设计
- jquery - 与所有其他输入一起获得无线电值
- jquery - 在jQuery中滚动