Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swapping await - expect sequence produce inconsistent results in test scripts #4813

Closed
nuliknol opened this issue Feb 1, 2024 · 1 comment
Assignees

Comments

@nuliknol
Copy link

nuliknol commented Feb 1, 2024

Version of Hardhat

2.19.5

What happened?

When using the sequence await expect(method()).to.be.revertedWith() will miss a revert in a test script.

I am attaching the picture to illustrate the bug. I am also attaching sample project to show the bug. Many test scripts (including the automatic example created by Hardhat itself when initializing the project) first place await keyword, then expect keyword, but using await before expect may lead to incorrect evaluation of unit tests.

Bug illustration in a github project, just compile and run: https://github.com/nuliknol/await-expect

bug-explanation
await-expect.tar.gz

Minimal reproduction steps

already included

Search terms

No response

@fvictorio
Copy link
Member

Hey @nuliknol, I think everything is working as expected. I'll try to explain what's going on here.

First, your method() function reverts with a "You can't run me" reason string. This means that if you add an assertion for that:

      await expect(sample.method()).to.be.revertedWith("You can't run me");

then the test should pass (and it does). You seem to be expecting the test to fail, but that doesn't make sense to me.

The real question is why one of the tests doesn't pass. The reason is that when you do this:

expect(await sample.method()).to.be.revertedWith("You can't run me");

the first thing that will be evaluated is await sample.method(). But this function returns a rejected promise, because the method reverts. If you await a rejected promise, and error is thrown; if an error is thrown within a test, then the test fails. await expect(sample.method(), on the other hand, doesn't await the rejected promise, and it handles it internally, so in that case that's not a problem.

Hopefully that makes sense.

@fvictorio fvictorio closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in Hardhat Feb 8, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

2 participants