Ava: @babel/register is the wrong module when loading sources compiled with Babel 6

Created on 12 Apr 2018  ยท  14Comments  ยท  Source: avajs/ava

Description

babel options under ava congifure isn't working.

Test Source

import test from 'ava';

import { add } from './index'

test('add', t => {
  t.is(add(1, 2), 3)
})

Error Message & Stack Trace

image

Config

{
  "ava": {
    "require": [
      "@babel/register"
    ],
    "babel": {
      "testOptions": {
        "babelrc": false,
        "presets": [
          "@babel/env"
        ]
      }
    }
  }
}

Command-Line Arguments

ava

Relevant Links

ava-demo

Environment

Node.js v9.8.0
darwin 17.2.0
ava 1.0.0-beta.3
npm 5.6.0

good for beginner help wanted documentation

Most helpful comment

confirmed

update ava config

"ava": {
    "require": [
      "./setup.js"
    ],
    "babel": {
      "testOptions": {
        "babelrc": false
      }
    }
  }

setup.js

require('@babel/register')({
  babelrc: false,
  presets: ['@babel/env']
})

it works.

All 14 comments

I guess the problem is @babel/register. @babel/register will read the config from .babelrc unless You specify options like below

require('@babel/register')({
  babelrc: false
})

confirmed

update ava config

"ava": {
    "require": [
      "./setup.js"
    ],
    "babel": {
      "testOptions": {
        "babelrc": false
      }
    }
  }

setup.js

require('@babel/register')({
  babelrc: false,
  presets: ['@babel/env']
})

it works.

just read the docs again. looks like this is the expected behavior? .

kind of weird to me, that test file and source file using different way to handle babel config.

I guess the problem is @babel/register. @babel/register will read the config from .babelrc unless You specify option

Oh that makes sense!

kind of weird to me, that test file and source file using different way to handle babel config.

I think in your case you should require babel-register. You shouldn't need that or @babel/register to run your tests, unless you want to use it to compile sources on-the-fly. And then you need to use the register that is compatible with how you compile your source files.

We should update https://github.com/avajs/ava/blob/master/docs/recipes/babel.md#compile-sources so it recommends the correct register module based on how source files are compiled.

Have you tried to use the babel bridge repo?
https://github.com/babel/babel-bridge
yarn add babel-core@^7.0.0-0

I also experienced it. I tried many times and i can confirm the config

"babel": {
      "testOptions": {
        "babelrc": false
      }
    }

don't working.

Than i use .babelrc and installed babel-core@^7.0.0-0 and run test, it throws an error:

Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.

@xxxxxMiss given that you're commenting here, are you using babel-register? That's Babel 6, so it won't work with Babel 7 configs or plugins. You need to either use @babel/register and Babel 7 configs, or babel-register with Babel 6 configs.

@novemberborn Sorry that i view issues to it and commented BTW. About my that issue, i give a demo link here

@xxxxxMiss I get the following:

โฏ npm t

> [email protected] test /private/var/folders/2_/qczp184x76b2nl034sq5hvxw0000gn/T/tmp.ki5NUCbmLQ/ava-test
> ava


  1 failed

  test dynamic import

  /private/var/folders/2_/qczp184x76b2nl034sq5hvxw0000gn/T/tmp.ki5NUCbmLQ/ava-test/test/test.js:11

   10: test('test dynamic import', t => {
   11:   return import('lodash').then(m => {
   12:     const _ = m.default || m

  Rejected promise returned by test. Reason:

  Error {
    message: 'Not supported',
  }

I think the problem you're seeing now is that import() is not supported by @babel/preset-env.

Could you post any follow-up to this on Spectrum instead? https://spectrum.chat/ava

@novemberborn OK, no problem.

Hi Ava Team,

I would love to update the correct register module based on how source files are compiled.
Can you please guide me on the process. (New to the OSS community).

@ashimagarwal that sounds great! Perhaps this article will be useful: https://medium.com/@vadimdemedes/making-your-first-contribution-de6576ddb190

Now that Babel 7 is out, I think we can assume people are upgrading. We probably shouldn't be making our documentation more complicated by covering Babel 6.

Was this page helpful?
0 / 5 - 0 ratings