Openzeppelin-contracts: Test helpers reusability issue

Created on 14 Feb 2018  ·  5Comments  ·  Source: OpenZeppelin/openzeppelin-contracts

🎉 Description

I try to reuse the helper functions from test/helpers (which are great by the way) and for some of them (the ones that use ES6 syntax) fail beautifully.

💻 Environment

I use:

  • zeppelin-solidity v1.6.0
  • node v9.5.0
  • truffle v4.0.6

I register babel dependencies in truffle-config.js.

📝 Details

Pieces of code that work as expected:

import assertJump from 'zeppelin-solidity/test/helpers/assertJump';
import expectEvent from 'zeppelin-solidity/test/helpers/expectEvent';
import transactionMined from 'zeppelin-solidity/test/helpers/transactionMined';

Pieces of code that fail:

import assertRevert from 'zeppelin-solidity/test/helpers/assertRevert';
import decodeLogs from 'zeppelin-solidity/test/helpers/decodeLogs';
import hashMessage from 'zeppelin-solidity/test/helpers/hashMessage';

or

const assertRevert = require('zeppelin-solidity/test/helpers/assertRevert');
const decodeLogs = require('zeppelin-solidity/test/helpers/decodeLogs');
const hashMessage = require('zeppelin-solidity/test/helpers/hashMessage');

Both formats throw the same exception:

home/danut/testing/node_modules/zeppelin-solidity/test/helpers/decodeLogs.js:3
export default function decodeLogs (logs, contract, address) {
^^^^^^

SyntaxError: Unexpected token export
    at new Script (vm.js:51:7)
    at createScript (vm.js:138:10)
    at Object.runInThisContext (vm.js:199:10)
    at Module._compile (module.js:624:28)
    at Module._extensions..js (module.js:671:10)
    at Object.require.extensions.(anonymous function) [as .js] (home/danut/testing/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Module.require (module.js:604:17)

I'll appreciate any idea on how to fix this because I am required to copy the files into my project's test/helpers directory which is a workaround. After I copy them I can do

import assertRevert from './helpers/assertRevert';

without any problems.

Most helpful comment

@spalladino Maybe it will be nice to be a separated package for testing. I could benefit from that and I think a lot of people will.

All 5 comments

As far as I understand, the issue is that Babel does not parse node_modules by default, so if we want to make test helpers available, we'd need to preprocess them before publishing the package.

As a workaround, you should be able to instruct Babel to parse the test helpers from Zeppelin (see here for an example on how to do it on webpack). But we should modify the package mechanism. I think @martriay had been looking into this some time ago...?

@spalladino Thanks for the hints.

I've added this to the truffle-config.js file and it did the trick.

require('babel-register')({
  ignore: /node_modules\/(?!zeppelin-solidity\/test\/helpers)/
});

And the truffle-config.js file now looks like:

require('babel-register')({
  ignore: /node_modules\/(?!zeppelin-solidity\/test\/helpers)/
});
require('babel-polyfill');

module.exports = {
  // networks
};

Good to hear!

On Wed, Feb 14, 2018 at 3:42 PM, Avadanei Danut notifications@github.com
wrote:

@spalladino https://github.com/spalladino Thanks for the hints.

I've added this to the truffle-config.js file and it did the trick.

require('babel-register')({
ignore: /node_modules\/(?!zeppelin-solidity\/test\/helpers)/
});

And the truffle-config.js file now looks like:

require('babel-register')({
ignore: /node_modules\/(?!zeppelin-solidity\/test\/helpers)/
});require('babel-polyfill');
module.exports = {
// networks
};


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/OpenZeppelin/zeppelin-solidity/issues/740#issuecomment-365704532,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAaOJJ_QVwAo1F7uLR6nSmyrhznDXNTwks5tUykvgaJpZM4SFvXW
.

@spalladino Maybe it will be nice to be a separated package for testing. I could benefit from that and I think a lot of people will.

Indeed. It is being discussed in issue #326, though it has become somewhat
inactive.

On Wed, Feb 14, 2018 at 3:57 PM, Avadanei Danut notifications@github.com
wrote:

@spalladino https://github.com/spalladino Maybe it will be nice to be a
separated package for testing. I could benefit from that and I think a lot
of people will.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/OpenZeppelin/zeppelin-solidity/issues/740#issuecomment-365708945,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAaOJMt6g_HWyg0AwlCzvydV5Gbd5u49ks5tUyyigaJpZM4SFvXW
.

Was this page helpful?
0 / 5 - 0 ratings