Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Asynchronous tests break with Babel #963

Closed
1 task done
michaeldel opened this issue May 23, 2018 · 2 comments
Closed
1 task done

Asynchronous tests break with Babel #963

michaeldel opened this issue May 23, 2018 · 2 comments

Comments

@michaeldel
Copy link

michaeldel commented May 23, 2018


Issue

Asynchronous tests break when adding Babel (babel-register and babel-polyfill) to project (this is set undefined in test sections).

Steps to Reproduce

A minimal example is attached here: example.tar.gz or example.zip

Init a new project with truffle init, create file test/Foo.test.js, with this content:

contract('Foo', accounts => {
  beforeEach(async () => {
    this.x = 0
  })

  it('simple test', async () => {})
})

This should run with no issue when executing truffle test.

Now imagine you need to import a function from somewhere else, like OpenZeppelin's assertRevert testing utility function, one would add these to test/Foo.test.js after having installed OpenZeppelin module (npm install --save openzeppelin-solidity):

import assertRevert from 'openzeppelin-solidity/test/helpers/assertRevert'

// CommonJS alternative, which would fail the same way
const assertRevert = require('openzeppelin-solidity/test/helpers/assertRevert')

// rest of your tests...

Now running truffle test should fail because of unexpected import token (or export (which is in OZ's assertRevert.js file) if you used require). Fine, let us fix that by adding Babel:

npm --save-dev babel-register babel-polyfill babel-preset-env

Now let us add these at the top of truffle.js (or truffle-config.js in case you are using that one):

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

// rest of truffle config...

And configure Babel through a new .babelrc file:

{
    "presets": ["env"]
}

Here we are, we can run truffle test again.

Expected Behavior

Imports should work and test should pass like before.

Actual Results

Imports are indeed fixed and now work properly. However, test fails, telling that this is undefined (within beforeEach section), which is quite of an issue.
I also noticed that this problem only happens for asynchronous tests (like I have written above).

Environment

  • Operating System: Archlinux
  • Ethereum client: Ganache (version 1.1.0-1)
  • Truffle version (truffle version): Truffle v4.1.11 (core: 4.1.11)
  • node version (node --version): v10.1.0
  • npm version (npm --version): 6.0.1
@cgewecke
Copy link
Contributor

cgewecke commented May 24, 2018

mocha's weirdness about this, the ES6 arrow function with non-binding this and babel transpilation is the Bermuda Triangle of this.

Could you see if everything is ok if you rewrite the arrow functions as conventional functions? e.g:

contract('Foo', function(accounts){ // Edit: forgot one
  beforeEach(async function(){
    this.x = 0
  })

  it('simple test', async function(){})
})

@michaeldel
Copy link
Author

@cgewecke this indeed seems to solve the problem, too bad having to add code-style/linting exceptions for such "should not even be a thing" issues though. Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants