Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

javascript - assertion error in mocha js shows - but not failing the entire test in async function

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?

question from:https://stackoverflow.com/questions/65869857/assertion-error-in-mocha-js-shows-but-not-failing-the-entire-test-in-async-fun

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...