ava is reading .babelrc when given an explicit configuration

Created on 6 Dec 2016  Â·  8Comments  Â·  Source: avajs/ava

Test Source

git clone [email protected]:gajus/ava-issue-1146.git
cd ./ava-issue-1146
npm install
ava --verbose ./test/EventEmitter/\#off.js

Error Message & Stack Trace

ava --verbose ./test/EventEmitter/\#off.js

/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/babel-core/lib/transformation/file/options/option-manager.js:176
          throw new ReferenceError(messages.get("pluginUnknown", plugin, loc, i, dirname));
          ^

ReferenceError: Unknown plugin "test" specified in "/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/.babelrc" at 0, attempted to resolve relative to "/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146"
    at /Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/babel-core/lib/transformation/file/options/option-manager.js:176:17
    at Array.map (native)
    at Function.normalisePlugins (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/babel-core/lib/transformation/file/options/option-manager.js:154:20)
    at OptionManager.mergeOptions (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/babel-core/lib/transformation/file/options/option-manager.js:229:36)
    at OptionManager.init (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/babel-core/lib/transformation/file/options/option-manager.js:374:12)
    at compile (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/babel-register/lib/node.js:103:45)
    at loader (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/babel-register/lib/node.js:144:14)
    at require.extensions.(anonymous function) (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/babel-register/lib/node.js:154:7)
    at extensions.(anonymous function) (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/require-precompiled/index.js:16:3)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/ava/lib/process-adapter.js:104:4)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/test/EventEmitter/#off.js:3:1)
    at Module._compile (module.js:573:32)
    at extensions.(anonymous function) (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/require-precompiled/index.js:13:11)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/ava/lib/process-adapter.js:104:4)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/gajuskuizinas/Documents/dev/mailonline/ava-issue-1146/node_modules/ava/lib/test-worker.js:33:1)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3
  ✖ test/EventEmitter/#off.js exited with a non-zero exit code: 1

  0 tests passed [10:54:11]
  1 uncaught exception

Config

Copy the relevant section from package.json:

{
  "ava": {
    "babel": {
      "plugins": [
        "transform-es2015-modules-commonjs",
        "transform-flow-strip-types"
      ]
    },
    "require": [
      "babel-register"
    ]
  },
  "devDependencies": {
    "ava": "^0.17.0",
    "babel-cli": "^6.18.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
    "babel-plugin-transform-flow-strip-types": "^6.18.0",
    "babel-register": "^6.18.0",
    "sinon": "^1.17.6"
  },
  "private": true
}

Command-Line Arguments

Copy your npm build scripts or the ava command used:

ava --verbose ./test/EventEmitter/\#off.js

Environment

Tell us which operating system you are using, as well as which versions of Node.js, npm, and AVA. Run the following to get it quickly:

Node.js v7.1.0
darwin 16.1.0
0.17.0
4.0.2

Most helpful comment

@gajus thanks.

For others reading this issue, I implemented it the following way.

With my .babelrc file I specified two separate env objects — one that satisfies Rollup and one that satisfies AVA:

{
  "env": {
    "build": {
      "presets": [
        "stage-3",
        ["es2015", { "modules": false }]
      ]
    },
    "test": {
      "presets": [
        "stage-3",
        "es2015"
      ]
    }
  }
}

I then had two separate commands for building and testing:

BABEL_ENV=build npm run build
BABEL_ENV=test npm test

All 8 comments

To clarify, this proves that ava reads and interprets .babelrc even though it is provided an explicit babel configuration in the ava configuration object of ./package.json.

.babelrc intentionally includes a plugin that does not exist to trigger an error:

{
  "plugins": [
    "test"
  ]
}

Note that adding "babelrc": false, does not change this behaviour.

On a related note, this issue can be used to replicate another issue that I cannot figure out:

git clone [email protected]:gajus/ava-issue-1146.git
cd ./ava-issue-1146
npm install
echo '{}' > .babelrc
ava --verbose ./test/EventEmitter/\#off.js

This will give an error:

/Users/gajuskuizinas/.Trash/ava-issue-1145/node_modules/babel-core/lib/transformation/file/index.js:600
      throw err;
      ^

SyntaxError: /Users/gajuskuizinas/.Trash/ava-issue-1145/src/EventEmitter.js: Unexpected token, expected ; (5:5)
  3 | /* eslint-disable promise/prefer-await-to-callbacks */
  4 |
> 5 | type SubscriberType = (data: any) => void;
    |      ^
  6 |
  7 | type SubscriptionsType = {
  8 |   [key: string]: Array<SubscriberType>

Which makes no sense since ava is configured to use transform-flow-strip-types transpiler.

There are two assumptions that mislead me:

  1. I thought that if I configure {"ava": {"babel": {}}} it will be used to transpile all the code, not only whats in ./test.
  2. After I have been pointed out that the latter is not the case, my first thought was to add custom babel-register config that will not ignore ./src. This would not have worked too, because babel-register would have picked up .babelrc config anyway and not the Babel config I have set in "ava".

The reason I got into this situation in the first place is because ava requires ES modules transpilation, while the platform for which I am building the project does not.

Well the trick is that babel-register will not grab a babel config from ava's package.json config. It will go for .babelrc or package.json embedded babel config instead, or it will use defaults and ignore ava's babel config in package.json completely.

And it's VERY frustrating because people are expecting completely the opposite behaviour.

@gajus what was your eventual solution?

I have a similar issue in that I'm using rollup for the build — which excludes modules in the es2015 plugin — however for AVA it requires modules, and I therefore logically thought an overwrite in the package.json would have been the solution – to no avail.

I have ended up using different .babelrc settings for development, production and testing.

@gajus thanks.

For others reading this issue, I implemented it the following way.

With my .babelrc file I specified two separate env objects — one that satisfies Rollup and one that satisfies AVA:

{
  "env": {
    "build": {
      "presets": [
        "stage-3",
        ["es2015", { "modules": false }]
      ]
    },
    "test": {
      "presets": [
        "stage-3",
        "es2015"
      ]
    }
  }
}

I then had two separate commands for building and testing:

BABEL_ENV=build npm run build
BABEL_ENV=test npm test
Was this page helpful?
0 / 5 - 0 ratings