Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using babel-register with AVA and .babelrc's `"ignore": false`, but node_modules are not being transpiled

Tags:

babeljs

ava

I'm trying to have source files (and their dependencies in node_modules) be transpiled when running AVA tests. I've configured AVA to require babel-register and inherit my .babelrc file with the following in package.json:

"ava": {
    "require": "babel-register",
    "babel": "inherit"
  }

and this in .babelrc:

{
  "presets": [ "es2015" ],
  "ignore": false
}

I have a test spec that imports a source file with and that source file imports an ES2015 dependency from node_modules:

However, when running ava I see:

/Users/me/code/esri-rollup-example/node_modules/capitalize-word/index.js:2
export default input => input.replace(regexp, match => match.charAt(0).toUpperCase() + match.substr(1));
^^^^^^

SyntaxError: Unexpected token export

Which tells me that the source file (src/app/utils.js) did transpile, but it's dependency in node_modules (capitalize-string/index) did not.

Both the source modules and dependencies transpile fine when I use babel CLI, so it really seems like the .babelrc's "ignore": false setting is not getting passed to babel-register. I can see from the babel docs that you can explicitly pass an ignore option to babel-register, but I don't see how you can do that from the AVA config. I even tried adding the following to my test file before the line where it imports the source files, but I still see the same error:

require("babel-register")({
  ignore: false
});

I suppose I could add a transpile step before testing, but I wanted to make sure that I wasn't just missing some AVA or babel configuration first.

like image 903
Tom Wayson Avatar asked Apr 26 '16 06:04

Tom Wayson


1 Answers

This is related to issue in babel itself – https://phabricator.babeljs.io/T6726

But you can put babel-register require in separate file (let call it .setup.js):

require('babel-register')({
    ignore: /node_modules\/(?!capitalize\-word)/i
});

const noop = function () {};

require.extensions['.css'] = noop; // If you want to ignore some CSS imports

And then change "require": "babel-register" to "require": "./.setup.js"

like image 57
floatdrop Avatar answered Nov 15 '22 19:11

floatdrop