如何解决测试执行后函数运行之前的node.js摩卡
我已经进行了很多移动,并尝试使用done()
,async
和链接then()
,并移动describe()
,而我最近的尝试是在Async function in mocha before() is alway finished before it() spec?之前的建议。
在表示测试开始的console.log('finished!')
之后,打印表示表已创建的console.log('starting tests')
。
我应该提到,以某种方式创建了用户表,并且所有用户测试都像超级按钮一样工作。
我所有的测试均失败,因为它们试图对不存在的表执行操作。我不确定了。如何确保before
在实际测试之前运行?
describe('',async () => {
before('setting up database',async () => {
return new Promise(async resolve => {
await db.users.createTable()
await db.stores.createTable()
await db.booths.createTable()
await db.reservations.createTable()
await db.clothing.createTable()
console.log('finished!')
resolve()
})
})
describe('running datalayer test suite',async () => {
try {
console.log('starting tests')
await userTest()
await storeTest()
await boothTest()
await reservationTest()
await clothingTest()
} catch (e) {
console.warn(e)
}
})
after('destroying db',async () => {
await db.clothing.dropTable()
await db.reservations.dropTable()
await db.booths.dropTable()
await db.stores.dropTable()
await db.users.dropTable()
})
})
starting tests
(node:16339) UnhandledPromiseRejectionWarning: Error: something went wrong with persisting the store: error: relation "stores" does not exist
at module.exports (/home/jonas/Projects/sellsome-backend/exceptions/query-exception.js:2:19)
at Object.insert (/home/jonas/Projects/sellsome-backend/logiclayer/stores.js:23:19)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:16339) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:16339) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
..... tons more
finished!
编辑:Mocha版本8.1.1
解决方法
我以前遇到过类似的问题,但不确定是否相同。
请尝试这种风格。请注意async function()
而不是箭头功能。
before('setting up database',async function() {
await db.users.createTable()
await db.stores.createTable()
await db.booths.createTable()
await db.reservations.createTable()
await db.clothing.createTable()
console.log('finished!')
})
,
请检查您的摩卡咖啡版本,就像我以前的版本一样
before(function (done) {
db.collection('user').remove({},function (res) { done(); }); // It is now guaranteed to finish before 'it' starts.
})
done()
是这里的关键
在新版本中,就像
let message = '';
before(() => {
return new Promise((resolve) => {
setTimeout(() => {
message = "hello";
resolve();
},10);
});
});
it('message should be hello',() => {
assert(message === 'hello');
});
,
通常,我回答自己的问题并不多-但是删除内部describe块可以完美地解决它。我尚未弄清楚为什么。
describe('',async () => {
before('setting up database',async () => {
return new Promise(async resolve => {
await db.users.createTable()
await db.stores.createTable()
await db.booths.createTable()
await db.reservations.createTable()
await db.clothing.createTable()
console.log('finished!')
resolve()
})
})
try {
console.log('starting tests')
await userTest()
await storeTest()
await boothTest()
await reservationTest()
await clothingTest()
} catch (e) {
console.warn(e)
}
after('destroying db',async () => {
await db.clothing.dropTable()
await db.reservations.dropTable()
await db.booths.dropTable()
await db.stores.dropTable()
await db.users.dropTable()
})
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。