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

Allow configuring custom web3 instance #38

Merged
merged 25 commits into from May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3974e5e
add web3-utils as a dependency
frangio May 9, 2019
c441f9c
rename main file to index.js
frangio May 9, 2019
644f82f
add explicit web3 injection with global fallback
frangio May 9, 2019
e9c6bb0
change installation procedure for integration tests
frangio May 9, 2019
3268fb0
add integration test for using in migrations
frangio May 9, 2019
0c4185b
emit better error message on migration integration test
frangio May 9, 2019
63d5777
add version check to web3 injection
frangio May 9, 2019
acb707b
allow multiple injection of the same instance
frangio May 9, 2019
33ca0f0
tweak migration integration test
frangio May 9, 2019
7c48143
fix linter errors
frangio May 9, 2019
5e7848e
pin web3-utils to beta 37
frangio May 9, 2019
61c628d
remove leftover comment
frangio May 9, 2019
ba1267e
rename integration test
frangio May 9, 2019
e80d12e
add truffle-migration test to travis
frangio May 9, 2019
1fa013c
keep test directory
frangio May 9, 2019
e0d103d
make inject-web3 a configuration step instead of alternative entry point
frangio May 10, 2019
a918899
remove unnecessary npm install
frangio May 10, 2019
88897cc
move default configuration logic to configure.js
frangio May 10, 2019
ebb9d6e
rename global-web3 to configure-web3
frangio May 10, 2019
0171182
add changelog entry
frangio May 10, 2019
8468b8d
add note about configuration in README
frangio May 10, 2019
5130283
fix linter errors
frangio May 10, 2019
2764287
Revert "remove unnecessary npm install"
frangio May 10, 2019
9fa877d
configure eslint globals correctly
frangio May 10, 2019
d2bfefa
fix some parentheses
frangio May 10, 2019
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
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.
nventuro marked this conversation as resolved.
Show resolved Hide resolved

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.