1

I found some responses for this problem but they don't work with promises probably. I want to clear DB after each test. I am trying to save id from request response and collect/pass it to afterEach. Unfortunatelly, promise doesn't override value and it's null as defined.

describe('Should check DB setup', () => {
    let value;

    let request = chai.request('http://localhost:81')
        .post('/api/report')
        .send(mock);

    let db = require(process.env.DB_DRIVER)(process.env.DB_NAME);

    it('Checks do DB has table', () => {
        request
            .then((res) => {
                let query = db.prepare('PRAGMA table_info('+process.env.ORDERS_TABLE+')').get();
                db.close();
                value = 'Win Later';
                expect(query).is.not.a('undefined');
            });
    });

    afterEach(() => {
        console.log(value); //undefined
    });
});
bdddd
  • 103
  • 1
  • 10

1 Answers1

1

You need to return the promise from your test so that Mocha waits for it before finishing the test.

it('Checks do DB has table', () => {
    return request
        .then((res) => {
            let query = db.prepare('PRAGMA table_info('+process.env.ORDERS_TABLE+')').get();
            db.close();
            value = 'Win Later';
            expect(query).is.not.a('undefined');
        });
});
Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964