-
Notifications
You must be signed in to change notification settings - Fork 133
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
Adding "x" as reason in expectRevert causes test to pass #125
Comments
Hello @alsco77, thanks for reporting this! Could you provide a contract and a test snippet so we can reproduce this issue to better understand it? |
Sure @nventuro - in the below test file, both tests pass. SafeToken.sol
Test.spec.ts
|
Hi @alsco77, I was able to reproduce using OpenZeppelin Test Environment as the test runner. A.sol// contracts/A.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract A {
function alwaysRevert() public {
revert("A: reason");
}
} A.test.js// test/A.test.js
const { contract } = require('@openzeppelin/test-environment');
// Import utilities from Test Helpers
const { expectRevert } = require('@openzeppelin/test-helpers');
const A = contract.fromArtifact('A');
describe('A', function () {
beforeEach(async function () {
this.contract = await A.new();
});
it('should fail as reason is not x', async function () {
await expectRevert(this.contract.alwaysRevert(), 'x');
});
it('should pass', async function () {
await expectRevert(this.contract.alwaysRevert(), 'A: reason');
});
}); Test$ npm run test
> issue125@1.0.0 test /home/abcoathup/projects/forum/issue125
> mocha --exit --recursive test
A
✓ should fail as reason is not x (96ms)
✓ should pass (45ms)
2 passing (571ms) |
The test is passing because the complete error message includes an 'x':
We're currently matching against the entire string, not just the revert reason. I think I recall @frangio at one point considering stripping the message at the beginning, we may have decided not to do so due to there being no guarantees of its contents accross versions and clients. Considering the helper is designed for you to input the entire expected revert reason, I think this makes sense. |
Yes it's kind of hard to compare against the entire message because of incompatibilities between clients... But we could try it out and see how it works. A middle point would be to require the message to match at the end of the string, rather than at any point. |
Yeah I think that there is some middle ground to stand on. I understand that the whole string is being matched against, but passing with 'x' seems like its being much too liberal, especially given that that part of the message is generally disregarded. Requiring a larger portion of the string to be matched, whether that is at the end or simply a full word somewhere seems like a fair patch to me. |
An issue with matching at the end is that we'd be forcing users to input the entire revert reason, which is not a requirement today. A best effort approach to remove the leading message ('vm exception') might be less disruptive. |
In my first attempt, I was expecting an exact reason matching excluding the generic |
Description
Specifying "x" as the 'expectedError' in the 'expectRevert' call causes the test to pass, even when "x" does not appear in the parsed error message.
This might not be directly a bug, although I can't see why it should be getting through the following check in
expectRevert.js#9
->if (error.message.indexOf(expectedError) === -1) {
.Either way it should certainly be more strict in this instance.
The text was updated successfully, but these errors were encountered: