Skip to content

Commit

Permalink
Added revertedWith method to the matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
vanruch authored and marekkirejczyk committed Aug 31, 2018
1 parent 6522046 commit 49504de
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 158 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ await expect(token.transfer(walletTo.address, 7))
await expect(token.transfer(walletTo.address, 1007)).to.be.reverted;
```

* Testing if transaction was reverted with certain message:
```js
await expect(token.transfer(walletTo.address, 1007)).to.be.revertedWith('Insufficient funds');
```


* Testing if string is a proper address:
```js
expect('0x28FAA621c3348823D6c6548981a19716bcDc740e').to.be.properAddress;
Expand Down
26 changes: 26 additions & 0 deletions lib/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ const solidity = (chai, utils) => {
return derivedPromise;
});

Assertion.addMethod('revertedWith', function (revertReason) {
/* eslint-disable no-underscore-dangle */
const promise = this._obj;
const derivedPromise = promise.then(
(value) => {
this.assert(false,
'Expected transaction to be reverted',
'Expected transaction NOT to be reverted',
'not reverted',
'reverted');
return value;
},
(reason) => {
this.assert(reason.toString().search('revert') >= 0 && reason.toString().search(revertReason) >= 0,
`Expected transaction to be reverted with ${revertReason}, but other exception was thrown: ${reason}`,
`Expected transaction NOT to be reverted with ${revertReason}`,
'Reverted',
reason);
return reason;
}
);
this.then = derivedPromise.then.bind(derivedPromise);
this.catch = derivedPromise.catch.bind(derivedPromise);
return derivedPromise;
});

const filterLogsWithTopics = (logs, topics) =>
logs.filter((log) => arrayIntersection(topics, log.topics).length > 0);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"node": ">=9.3"
},
"dependencies": {
"ethers": "^3.0.26",
"ethers": "^3.0.27",
"solc": "^0.4.24"
},
"devDependencies": {
Expand Down
79 changes: 79 additions & 0 deletions test/matchers/reverted.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,82 @@ describe('Matchers: reverted', () => {
).to.be.eventually.rejected;
});
});

describe('Matchers: revertedWith', () => {
let provider;
let matchers;
let wallet;

beforeEach(async () => {
provider = createMockProvider();
[wallet] = await getWallets(provider);
matchers = await deployContract(wallet, Matchers);
});

it('Throw: success', async () => {
await expect(matchers.doThrow()).to.be.revertedWith('');
});

it('Not to revert: success', async () => {
await expect(matchers.doNothing()).not.to.be.revertedWith('');
});

it('Revert with modification: success', async () => {
await expect(matchers.doRevertAndModify()).to.be.revertedWith('Revert cause');
});

it('ThrowAndModify: success', async () => {
await expect(matchers.doThrowAndModify()).to.be.revertedWith('');
});

it('Revert: success', async () => {
await expect(matchers.doRevert()).to.be.revertedWith('Revert cause');
});

it('Revert: fail when different message was thrown', async () => {
await expect(expect(matchers.doRevert()).to.be.revertedWith('Different message')).to.be.eventually.rejected;
});

it('Revert: fail no exception', async () => {
await expect(
expect(matchers.doNothing()).to.be.revertedWith('')
).to.be.eventually.rejected;
});

it('Require: success', async () => {
await expect(matchers.doRequireFail()).to.be.revertedWith('Require cause');
});

it('Require: fail when no exception was thrown', async () => {
await expect(expect(matchers.doRequireSuccess()).to.be.revertedWith('Never to be seen')).to.be.eventually.rejected;
});

it('Require: fail when different message', async () => {
await expect(expect(matchers.doRequireFail()).to.be.revertedWith('Different message')).to.be.eventually.rejected;
});

it('Not to revert: fail', async () => {
await expect(
expect(matchers.doThrow()).not.to.be.revertedWith('')
).to.be.eventually.rejected;
});

it('Not to revert: fail when same message was thrown', async () => {
expect(matchers.doRevert()).not.to.be.revertedWith('Revert cause');
});

it('Not to revert: success when different message was thrown', async () => {
expect(matchers.doRevert()).not.to.be.revertedWith('Different message');
});

it('Revert: fail, random exception', async () => {
await expect(alwaysReject).not.to.be.revertedWith('Random reason');
});

it('Not to revert: fail, random exception', async () => {
await expect(
expect(alwaysReject).to.be.revertedWith('Random reason')
).to.be.eventually.rejected;
});
});

Loading

0 comments on commit 49504de

Please sign in to comment.