简体   繁体   中英

node.js mocha before function runs after test execution

I have moved this around so much and tried with done() , async and chaining then() , moving the describe() around and my latest attempt was to return a promise in the before as Async function in mocha before() is alway finished before it() spec? suggested.

The console.log('finished!') that indicates the tables have been created is printed way after console.log('starting tests') that indicates the start of the tests.

I should mention that somehow the user table is created and all the user tests works like a charm.

All of my tests fail because they try to perform operations on tables that does not exist. I am not sure of much anymore. How can I make sure the before runs before the actual tests?

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!

Edit: Mocha version 8.1.1

I have experiences a similiar issue before, but im unsure if this is the same.

Try this style please. Note the async function() instead of arrow functions.

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

Do check your mocha version, as I know for the previous version it was something like

before(function (done) {
   db.collection('user').remove({}, function (res) { done(); }); // It is now guaranteed to finish before 'it' starts.
})

done() is the key here

In new version it's like

let message = '';
before(() => {
  return new Promise((resolve) => {
    setTimeout(() => {
      message = "hello";
      resolve();
    }, 10);
  });
});
it('message should be hello', () => {
  assert(message === 'hello');
});

Link

Normally I am not much for answering my own questions - but removing the inner describe block fixed it perfectly. I am yet to figure out exactly why.

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

    })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM