Skip to content

Commit

Permalink
make chai a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio committed Oct 11, 2019
1 parent ca500ba commit 8a674ae
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 18 deletions.
17 changes: 5 additions & 12 deletions package-lock.json

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

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@truffle/contract": "^4.0.35",
"ansi-colors": "^3.2.3",
"chai": "^4.2.0",
"chai-bn": "^0.2.0",
"ethjs-abi": "^0.2.1",
"lodash.flatten": "^4.4.0",
Expand All @@ -46,7 +47,6 @@
},
"devDependencies": {
"@openzeppelin/contract-loader": "^0.1.0",
"chai": "^4.2.0",
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
Expand All @@ -56,8 +56,5 @@
"eslint-plugin-standard": "^4.0.0",
"ganache-cli": "^6.5.0",
"truffle": "^5.0.29"
},
"peerDependencies": {
"chai": "^4.2.0"
}
}
14 changes: 12 additions & 2 deletions src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ require('../configure')();

const web3 = require('./config/web3').getWeb3();

const chai = require('chai');
const BN = web3.utils.BN;
const chaiBN = require('chai-bn')(BN);

chai.use(require('chai-bn')(BN));
require('chai').use(chaiBN);

// Installing chai-bn for the user is part of the offering.
//
// The chai module used internally by us may not be the same one that the user
// has in their own tests. This can happen if the version ranges required don't
// intersect, or if the package manager doesn't dedupe the modules for any
// other reason. We do our best to install chai-bn for the user.
function useChaiBN (chai) { if (chai) chai.use(chaiBN); }
useChaiBN(require.main.require('chai'));
useChaiBN(module.parent.require('chai'));

module.exports = {
web3,
Expand Down
48 changes: 48 additions & 0 deletions test-integration/chai-bn/package-lock.json

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

14 changes: 14 additions & 0 deletions test-integration/chai-bn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "chai-bn",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "node test.js"
},
"author": "Francisco Giordano <[email protected]>",
"license": "MIT",
"dependencies": {
"chai": "^3.5.0",
"semver": "^6.3.0"
}
}
17 changes: 17 additions & 0 deletions test-integration/chai-bn/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -o errexit

cd "$(dirname $0)"

# Get a package like a user would install
pkg="$(npm pack ../../ 2> /dev/null)"

# Clean it up afterwards
trap "rm -f $pkg" EXIT

npm install

npm install --no-save "$pkg"

npm test
23 changes: 23 additions & 0 deletions test-integration/chai-bn/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Validate that this is testing what we want: that this package uses a
// different chai module than the one being used internally by test-helpers.
// This is verified by checking that the chai versions don't intersect, so that
// the package manager is unable to dedupe them.
const assert = require('assert');
const semver = require('semver');

const our = require('./package.json');
const testHelpers = require('@openzeppelin/test-helpers/package.json');

assert(
!semver.intersects(our.dependencies.chai, testHelpers.dependencies.chai),
'Integration test is not set up correctly: chai module may be deduped',
);

// Even though we're using different chai modules, chai-bn is still being
// installed.
require('@openzeppelin/test-helpers');

const { expect } = require('chai');

// If chai-bn is not installed the following line will fail.
expect('1').to.bignumber.equal('1');

0 comments on commit 8a674ae

Please sign in to comment.