[英]node.js mocha before function runs after test execution
我已經移動了這么多,並嘗試使用done()
, async
和鏈接then()
,移動describe()
並且我最近的嘗試是在之前返回一個 promise 因為Async function 在 mocha before() 中總是完成在它()規范之前? 建議。
指示表已創建的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!
編輯:摩卡版本 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('', 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()
})
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.