node.js mocha before function runs after test execution

Tags: , , ,



I have moved this around so much and tried with done(), asyncand 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 beforeruns 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

Answer

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

    })
})


Source: stackoverflow