-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Description
🎉 Description
- 🐛 This is a bug report.
- 📈 This is a feature request.
💻 Environment
Agnostic (platform-independent)
📝 Details
If the promise is completed successfully, then you throw an exception with an error message containing "revert".
You then immediately catch this exception, and since its error message contains "revert", the assert completes successfully and no exception is thrown.
The thing to understand is that the Mocha framework reports a failure only when the tested scenario (it) throws an exception.
When that happens, the Mocha framework catches this exception and reports failure on the specific test at hand.
When you throw an exception and immediately catch it, the Mocha framework is not even aware of it.
For example, let's analyze a scenario in which the promise completes successfully, hence the test should fail:
- In the
trypart, thepromisecompletes successfully - In the
trypart, you throw an exception with"revert"in its error message - In the
catchpart, you immediately catch that exception - In the
catchpart, theassertcompletes successfully because the error message contains"revert" - No exception is thrown, and the test does not fail
In order to understand the logical confusion, keep in mind that there are two different failure scenarios which you need to detect and handle:
- No error has occurred (the
promisehas completed successfully) - An error without
"revert"has occurred (thepromisehas failed but not because ofrequire)
In your code, I believe that you detect the first scenario correctly, but you handle it incorrectly.
I therefore suggest the following alternative:
try {
await promise;
throw null;
}
catch (error) {
assert(error, `Expected an error but did not get one`);
assert(error.message.includes("revert"), `Expected an error containing "revert" but got "${error.message}" instead`);
}