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:
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…