node.js mocha before function runs after test execution

node.js mocha before function runs after test execution

我已经移动了这么多并尝试使用 done()async 和链接 then(),移动 describe(),我最近的尝试是 return 如 Async function in mocha before() is alway finished before it() spec? 建议的那样在之前承诺。

指示 table 已创建的 console.log('finished!') 打印在指示测试开始的 console.log('starting tests') 之后。

我应该提一下,用户 table 以某种方式创建,所有用户测试都非常有效。

我的所有测试都失败了,因为它们试图对不存在的 table 执行操作。我不确定了。如何确保 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!')
})

一定要检查你的 mocha 版本,我知道之前的版本是这样的

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');
});

Link

通常我不太喜欢回答自己的问题 - 但删除内部描述块可以完美修复它。我还没有弄清楚到底是为什么。

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()

    })
})