【问题标题】:node.js mocha before function runs after test execution测试执行后函数运行前的node.js mocha
【发布时间】:2020-11-30 23:35:55
【问题描述】:

我已经移动了很多,并尝试使用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!

编辑:摩卡版本 8.1.1

【问题讨论】:

    标签: javascript node.js testing mocha.js


    【解决方案1】:

    通常我不会回答我自己的问题 - 但是删除内部描述块完美地修复了它。我还没有弄清楚究竟是为什么。

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

    【讨论】:

      【解决方案2】:

      请检查您的摩卡咖啡版本,因为我知道以前的版本类似于

      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

      【讨论】:

      • 我的 mocha 版本是最新的 8.1.1
      【解决方案3】:

      我之前也遇到过类似的问题,但不确定是不是一样。

      请尝试这种风格。注意 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!')
      })
      

      【讨论】:

      • 用常规函数替换所有箭头函数后,我可以遗憾地报告结果是一样的..
      • 请尝试从环绕的describe 块中删除async
      • 我认为这可以解决问题,但不管有没有àsync,它的行为都是一样的
      猜你喜欢
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 2023-03-08
      • 2020-08-26
      相关资源
      最近更新 更多