Skip to content

Commit

Permalink
Allow configuring custom web3 instance (#38)
Browse files Browse the repository at this point in the history
* add web3-utils as a dependency

* rename main file to index.js

* add explicit web3 injection with global fallback

* change installation procedure for integration tests

* add integration test for using in migrations

* emit better error message on migration integration test

* add version check to web3 injection

* allow multiple injection of the same instance

* tweak migration integration test

* fix linter errors

* pin web3-utils to beta 37

* remove leftover comment

* rename integration test

* add truffle-migration test to travis

* keep test directory

* make inject-web3 a configuration step instead of alternative entry point

* remove unnecessary npm install

* move default configuration logic to configure.js

* rename global-web3 to configure-web3

* add changelog entry

* add note about configuration in README

* fix linter errors

* Revert "remove unnecessary npm install"

This reverts commit a918899.

* configure eslint globals correctly

* fix some parentheses
  • Loading branch information
Francisco Giordano committed May 10, 2019
1 parent 1b11b39 commit 401e0bd
Show file tree
Hide file tree
Showing 34 changed files with 1,217 additions and 39 deletions.
6 changes: 0 additions & 6 deletions .eslintrc
Expand Up @@ -13,12 +13,6 @@
"mocha" : true,
"jest" : true,
},
"globals" : {
"artifacts": false,
"contract": false,
"assert": false,
"web3": false,
},
"rules": {

// Strict mode
Expand Down
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -28,6 +28,11 @@ jobs:
name: "Ganache Core 2.1.x"
script: ./test-integration/ganache-core-2.1.x/run.sh

- stage: Integration tests
name: "Truffle Migration"
script: ./test-integration/truffle-migration/run.sh


notifications:
slack:
rooms:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
* Fix `send.transaction` not working on contracts with a fallback function. ([#26](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/26)
* `shouldFail.reverting.withMessage` fails if no error string is provided. ([#28](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/28)
* Rename `makeInterfaceId` to `makeInterfaceId.ERC165`, and add `makeInterfaceId.ERC1820`. ([#21](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/21)
* Add possibility to configure a custom web3 instance. ([#38](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/38))

#### How to upgrade from 0.3
- Change all occurences of `makeInterfaceId` to `makeInterfaceId.ERC165`.
Expand Down
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -47,6 +47,16 @@ contract('ERC20', ([sender, receiver]) => {
});
```

### Configuration

By default, this library will look for a global web3 instance, but you can run a manual configuration and supply a custom one.

```javascript
require('openzeppelin-test-helpers/configure')({ web3: ... });

const { expectEvent } = require('openzeppelin-test-helpers');
```

## Reference

This documentation is a work in progress: if in doubt, head over to the [tests directory](https://github.com/OpenZeppelin/openzeppelin-test-helpers/tree/master/test/src) to see examples of how each helper can be used.
Expand Down
55 changes: 55 additions & 0 deletions configure.js
@@ -0,0 +1,55 @@
/* global web3 */

const { setWeb3 } = require('./src/configure-web3');

let loadedConfig;

function configure (config) {
if (!config) {
// if there already is a loaded config keep it
if (!loadedConfig) {
defaultConfigure();
loadedConfig = 'default';
}
} else {
if (loadedConfig) {
let errorMessage = 'Cannot configure openzeppelin-test-helpers a second time.';

if (loadedConfig === 'default') {
errorMessage += `
A configuration has been loaded by default. Make sure to do custom configuration before importing the library.
require('openzeppelin-test-helpers/configure')({ web3: ... });
const { expectEvent } = require('openzeppelin-test-helpers');
`;
}

throw new Error(errorMessage);
}

customConfigure(config);
loadedConfig = 'custom';
}
};

function defaultConfigure () {
if (typeof web3 === 'undefined') {
throw new Error(`Cannot find a global Web3 instance. Please configure one manually:
require('openzeppelin-test-helpers/configure')({ web3: ... });
`
);
}

// use global web3
setWeb3(web3);
}

function customConfigure (config) {
setWeb3(config.web3);
}

module.exports = configure;
File renamed without changes.

0 comments on commit 401e0bd

Please sign in to comment.