Skip to content

A logical error in assertRevert.js #938

@barakman

Description

@barakman

🎉 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 try part, the promise completes successfully
  • In the try part, you throw an exception with "revert" in its error message
  • In the catch part, you immediately catch that exception
  • In the catch part, the assert completes 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:

  1. No error has occurred (the promise has completed successfully)
  2. An error without "revert" has occurred (the promise has failed but not because of require)

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`);
}

Metadata

Metadata

Assignees

Labels

buggood first issueLow hanging fruit for new contributors to get involved!testsTest suite and helpers.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions