0

I have a mocha js test suite, using chai for assertions and superagent for http calls.

Inside, I have some async fucntion that use await on promises. The results of the promises are resolved fine, but when I try to assert on a known wrong value, the test only shows that the assertion failed, but the test itself is not failing.

it.only('attach device via user api - with name - check that userName was created', async () => {
            // create device with admin
            try {
                await Helpers.createDeviceAdmin(deviceId1);

                const userData = {
                    email: userEmail1,
                    first_name: userFirstName1,
                    last_name: userLastName1,
                };
                accessToken = await Helpers.createAndRegUser(userData);
                const { found, deviceFound } = await Helpers.findDevices(deviceId1);
                assert.equal(found, false);
                assert.notProperty(deviceFound, 'userName');
                // attach to user - check if device has name
                const data = {
                    deviceId: deviceId1,
                    accessToken
                };
                setup.front.post(setup.paths.FRONT.USER.ATTACH())
                    .set('Content-Type', 'application/json')
                    .send(data)
                    .expect(200)
                    .expect(async res => {
                        assert.ok(res.body);
                        assert.equal(res.body.status, 'success');
                        assert.propertyVal(res.body.user, 'email', userEmail1);
                        try {
                            const { found, deviceFound } = await Helpers.findDevices(deviceId1);
                            assert.equal(found, true);
                            assert.property(deviceFound, 'userName');
                            assert.equal(deviceFound.userName, `${userFirstName1} ${userLastName1}`);

                        } catch (error) {
                            expect(error).to.be.undefined;
                            throw error;
                        }

                    });

            } catch (error) {
                console.log('error', error);
            }

        });

All helper functions return a promise, and they all resolve correctly. setup.front = is calling superangent in order to make http request.

The issue is with the assertions,they show assertion error, but the mocha test passes nonetheless.

For example: if I change: assert.equal(found, true); to assert.equal(found, false), I get:

enter image description here

My question is: How can I force mocha to fail the test if there is an assertion error, inside async function?

3
  • I had a similar error, did you find a solution? Commented Jun 23, 2021 at 16:09
  • 2
    @DonDiego: yes, remove the try/catch. the catch section catches the error, and prevents superagent to catch it Commented Jul 12, 2021 at 11:31
  • @YishaiNachliel Legend. Thank you Commented Aug 3, 2021 at 18:29

1 Answer 1

0

the answer was: remove the try/catch. the catch section catches the error, and prevents superagent to catch it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.