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

Remove chai autoinstall #18

Merged
merged 8 commits into from Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -14,28 +14,35 @@ npm install --save-dev openzeppelin-test-helpers
## Usage

```javascript
// Import all required modules from openzeppelin-test-helpers
const { BN, constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');

// Import preferred chai flavor: both expect and should are supported
const { expect } = require('chai');

const ERC20 = artifacts.require('ERC20');

contract('ERC20', ([sender, receiver]) => {
beforeEach(async function () {
this.erc20 = await ERC20.new();
this.value = new BN(1);
this.value = new BN(1); // The bundled BN library is the same one truffle and web3 use under the hood
});

it('reverts when transferring tokens to the zero address', async function () {
// Edge cases that trigger a require statement can be tested for, optionally checking the revert reason as well
await shouldFail.reverting(this.erc20.transfer(constants.ZERO_ADDRESS, this.value, { from: sender }));
});

it('emits a Transfer event on successful transfers', async function () {
const { logs } = this.erc20.transfer(receiver, this.value, { from: sender });
// Log-checking will not only look at the event name, but also the values, which can be addresses, strings, numbers, etc.
expectEvent.inLogs(logs, 'Transfer', { from: sender, to: receiver, value: this.value });
});

it('updates balances on successful transfers', async function () {
this.erc20.transfer(receiver, this.value, { from: sender });
(await this.token.balanceOf(receiver)).should.be.bignumber.equal(this.value);
// chai-bn is installed, which means BN values can be tested and compared using the bignumber property in chai
expect(await this.token.balanceOf(receiver)).to.be.bignumber.equal(this.value);
});
});
```
Expand Down
4 changes: 1 addition & 3 deletions openzeppelin-test-helpers.js
@@ -1,15 +1,13 @@
const { BN, expect, should } = require('./src/setup');
const { BN } = require('./src/setup');

module.exports = {
balance: require('./src/balance'),
BN,
constants: require('./src/constants'),
ether: require('./src/ether'),
expect,
expectEvent: require('./src/expectEvent'),
makeInterfaceId: require('./src/makeInterfaceId'),
send: require('./src/send'),
should,
shouldFail: require('./src/shouldFail'),
singletons: require('./src/singletons'),
time: require('./src/time'),
Expand Down
17 changes: 12 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -33,13 +33,13 @@
"homepage": "https://github.com/OpenZeppelin/openzeppelin-test-helpers#readme",
"dependencies": {
"ansi-colors": "^3.2.3",
"chai": "^4.2.0",
"chai-bn": "^0.1.1",
"ethjs-abi": "^0.2.1",
"semver": "^5.6.0",
"truffle-contract": "^4.0.8"
},
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
Expand All @@ -49,5 +49,8 @@
"eslint-plugin-standard": "^4.0.0",
"ganache-cli": "^6.2.5",
"truffle": "^5.0.1"
},
"peerDependencies": {
"chai": "^4.2.0"
}
}
5 changes: 3 additions & 2 deletions src/expectEvent.js
@@ -1,4 +1,5 @@
const { BN, expect } = require('./setup');
const { BN } = require('./setup');
const { expect } = require('chai');

function inLogs (logs, eventName, eventArgs = {}) {
const events = logs.filter(e => e.event === eventName);
Expand Down Expand Up @@ -35,7 +36,7 @@ async function inTransaction (txHash, emitter, eventName, eventArgs = {}) {
}

function contains (args, key, value) {
(key in args).should.equal(true, `Unknown event argument '${key}'`);
expect(key in args).to.equal(true, `Unknown event argument '${key}'`);

if (value === null) {
expect(args[key]).to.equal(null);
Expand Down
7 changes: 1 addition & 6 deletions src/setup.js
@@ -1,13 +1,8 @@
const chai = require('chai');

const BN = web3.utils.BN;

const should = chai
.use(require('chai-bn')(BN))
.should();
chai.use(require('chai-bn')(BN));

module.exports = {
BN,
expect: chai.expect,
should,
};
6 changes: 3 additions & 3 deletions src/shouldFail.js
@@ -1,4 +1,4 @@
const { should } = require('./setup');
const { expect } = require('chai');
const colors = require('ansi-colors');
const semver = require('semver');

Expand All @@ -7,12 +7,12 @@ async function shouldFailWithMessage (promise, message) {
await promise;
} catch (error) {
if (message) {
error.message.should.include(message, `Wrong failure type, expected '${message}'`);
expect(error.message).to.include(message, `Wrong failure type, expected '${message}'`);
}
return;
}

should.fail('Expected failure not received');
expect.fail('Expected failure not received');
}

async function reverting (promise) {
Expand Down
2 changes: 0 additions & 2 deletions src/singletons.js
@@ -1,5 +1,3 @@
require('./setup');

const ether = require('./ether');
const send = require('./send');

Expand Down
8 changes: 5 additions & 3 deletions test-integration/ganache-core-2.1.x/package.json
Expand Up @@ -9,7 +9,9 @@
"author": "",
"license": "ISC",
"devDependencies": {
"truffle": "^5.0.1",
"ganache-cli": "6.1.0"
}
"chai": "^4.2.0",
"ganache-cli": "6.1.0",
"truffle": "^5.0.1"
},
"dependencies": {}
}
5 changes: 4 additions & 1 deletion test-integration/ganache-core-2.1.x/test.sh
Expand Up @@ -3,7 +3,10 @@
# Delete the installed dependency
rm -rf node_modules/openzeppelin-test-helpers
# Replace it with the local package
ln -s ../../.. node_modules/openzeppelin-test-helpers
mkdir -p node_modules/openzeppelin-test-helpers/src
cp -r ../../openzeppelin-test-helpers.js node_modules/openzeppelin-test-helpers/
cp -r ../../package.json node_modules/openzeppelin-test-helpers/
cp -r ../../src/* node_modules/openzeppelin-test-helpers/src/

# Exit script as soon as a command fails.
set -o errexit
Expand Down
5 changes: 3 additions & 2 deletions test-integration/ganache-core-2.1.x/test/Tested.test.js
@@ -1,4 +1,5 @@
const { shouldFail } = require('openzeppelin-test-helpers');
const { expect } = require('chai');

const Tested = artifacts.require('Tested');

Expand All @@ -14,13 +15,13 @@ contract('Tested', function (accounts) {
const expectedMessage = 'lorem ipsum';
// Asserting that locally installed ganache-core is v2.1.0.
const nodeInfo = await web3.eth.getNodeInfo();
nodeInfo.should.include('2.1.0');
expect(nodeInfo).to.include('2.1.0');
try {
await this.contract.failWithRevertReason();
}
catch (error) {
// Asserting that older ganache does NOT return reason message.
error.message.should.not.include(expectedMessage);
expect(error.message).to.not.include(expectedMessage);
}
// With that said, following revert should be accepted without regard to the specified
// reason message.
Expand Down
1 change: 1 addition & 0 deletions test-integration/simple-project-truffle-5.x/package.json
Expand Up @@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"chai": "^4.2.0",
"truffle": "^5.0.1"
}
}
6 changes: 5 additions & 1 deletion test-integration/simple-project-truffle-5.x/test.sh
Expand Up @@ -2,7 +2,11 @@

# Delete the installed dependency
rm -rf node_modules/openzeppelin-test-helpers

# Replace it with the local package
ln -s ../../.. node_modules/openzeppelin-test-helpers
mkdir -p node_modules/openzeppelin-test-helpers/src
cp -r ../../openzeppelin-test-helpers.js node_modules/openzeppelin-test-helpers/
cp -r ../../package.json node_modules/openzeppelin-test-helpers/
cp -r ../../src/* node_modules/openzeppelin-test-helpers/src/

npx truffle test
@@ -1,10 +1,11 @@
const { balance, BN, ether, send } = require('openzeppelin-test-helpers');
const { expect } = require('chai');

contract('accounts', function (accounts) {
it('sends ether and tracks balances', async function () {
const value = ether('42', 'ether');
const tracker = await balance.tracker(accounts[0]);
await send.ether(accounts[0], accounts[1], value);
(await tracker.delta()).should.be.bignumber.equals(value.neg());
expect(await tracker.delta()).to.be.bignumber.equals(value.neg());
});
});
15 changes: 8 additions & 7 deletions test/src/balance.test.js
@@ -1,4 +1,5 @@
require('../../src/setup');
const { expect } = require('chai');

const balance = require('../../src/balance');
const send = require('../../src/send');
Expand All @@ -7,48 +8,48 @@ const ether = require('../../src/ether');
contract('balance', function ([sender, receiver]) {
describe('current', function () {
it('returns the current balance of an account as a BN', async function () {
(await balance.current(sender)).should.be.bignumber.equal(await web3.eth.getBalance(sender));
expect(await balance.current(sender)).to.be.bignumber.equal(await web3.eth.getBalance(sender));
});
});

describe('balance tracker', function () {
it('returns current balance ', async function () {
const tracker = await balance.tracker(receiver);
(await tracker.get()).should.be.bignumber.equal(await web3.eth.getBalance(receiver));
expect(await tracker.get()).to.be.bignumber.equal(await web3.eth.getBalance(receiver));
});

it('get() adds a new checkpoint ', async function () {
const tracker = await balance.tracker(sender);
await send.ether(sender, receiver, ether('1'));
await tracker.get();
(await tracker.delta()).should.be.bignumber.equal('0');
expect(await tracker.delta()).to.be.bignumber.equal('0');
});

it('returns correct deltas after get() checkpoint', async function () {
const tracker = await balance.tracker(receiver);
await send.ether(sender, receiver, ether('1'));
await tracker.get();
await send.ether(sender, receiver, ether('1'));
(await tracker.delta()).should.be.bignumber.equal(ether('1'));
expect(await tracker.delta()).to.be.bignumber.equal(ether('1'));
});

it('returns balance increments', async function () {
const tracker = await balance.tracker(receiver);
await send.ether(sender, receiver, ether('1'));
(await tracker.delta()).should.be.bignumber.equal(ether('1'));
expect(await tracker.delta()).to.be.bignumber.equal(ether('1'));
});

it('returns balance decrements', async function () {
const tracker = await balance.tracker(sender);
await send.ether(sender, receiver, ether('1'));
(await tracker.delta()).should.be.bignumber.equal(ether('-1'));
expect(await tracker.delta()).to.be.bignumber.equal(ether('-1'));
});

it('returns consecutive deltas', async function () {
const tracker = await balance.tracker(sender);
await send.ether(sender, receiver, ether('1'));
await tracker.delta();
(await tracker.delta()).should.be.bignumber.equal('0');
expect(await tracker.delta()).to.be.bignumber.equal('0');
});
});
});
5 changes: 3 additions & 2 deletions test/src/ether.test.js
@@ -1,12 +1,13 @@
const { BN } = require('../../src/setup');
const { expect } = require('chai');
const ether = require('../../src/ether');

describe('ether', function () {
it('returns a BN', function () {
ether('1').should.be.bignumber.equal(new BN('1000000000000000000'));
expect(ether('1')).to.be.bignumber.equal(new BN('1000000000000000000'));
});

it('works with negative amounts', function () {
ether('-1').should.be.bignumber.equal(new BN('-1000000000000000000'));
expect(ether('-1')).to.be.bignumber.equal(new BN('-1000000000000000000'));
});
});