Skip to content
Advertisement

mocha test passes even though I throw error

it.only('test', async() => {
    const lol = pdfjs.getDocument({data: data, password: "123"})
    lol.promise.then((ex) => { return ex }).catch((err) => {
        console.log(err)
        throw err;
    });
});

in this block of code “err” is being printed, and the test passes. also tried –

assert.fail('expected', 'actual', err);

and done().

nothing worked, test is still passing every time.

why is this happening?

Advertisement

Answer

Option 1: await the promise

Just add await before the promise expression. You don’t even need those then and catch.

it.only('test', async () => {
    const lol = pdfjs.getDocument({data: data, password: "123"});
    await lol.promise;
});

Option 2: return the promise

If you just return the promise, you don’t need to declare the test function as async.

it.only('test', () => {
    const lol = pdfjs.getDocument({data: data, password: "123"})
    return lol.promise;
});

Option 3: use a callback

A callback is declared as a parameter of the test function, and is called when the test finished, taking the error as a parameter (if any).

it.only('test', done => {
    const lol = pdfjs.getDocument({data: data, password: "123"})
    lol.promise.then(() => done()).catch((err) => done(err));
});
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement