Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

why is this happening?

>Solution :

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));
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading