Ava: Browser recipe doesn't work with default AVA install

Created on 16 Jan 2017  Â·  5Comments  Â·  Source: avajs/ava

@sindresorhus You asked me (https://github.com/avajs/ava/pull/1054#discussion_r81580347) to change from require to import in the browser recipe. Foolishly I didn't check after I changed it but it doesn't actually work on a default install because modules that are required by AVA aren't transpiled.

To fix it we would need to also require the babel bits:

npm install --save-dev babel-register babel-plugin-transform-es2015-modules-commonjs

and require the following settings in package.json:

"ava": {
  "require": [
    "babel-register",
    "./test/helpers/setup-browser-env.js"
  ]
},
"babel": {
  "plugins": ["transform-es2015-modules-commonjs"]
},

Which seems like quite a lot for one little helper.


Do you want me to update the recipe with these requirements?

Or should I revert

import browserEnv from 'browser-env';
browserEnv();

to

require('browser-env')()

so they aren't necessary?

bug

Most helpful comment

Right. It will work in the next release though, as AVA now transpiles helper files too. So I think we can just keep this issue open until the next release and not do anything.

All 5 comments

Right. It will work in the next release though, as AVA now transpiles helper files too. So I think we can just keep this issue open until the next release and not do anything.

New release is out: https://github.com/avajs/ava/releases/tag/v0.18.0 :)

@sindresorhus this example still doesn't work as it throws an error about importing.

➜  SickRage git:(develop) ✗ ava test
/Users/xo/code/SickRage/test/helpers/setup-browser-env.js:1
(function (exports, require, module, __filename, __dirname) { import browserEnv from 'browser-env';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Array.forEach (native)
    at Object.<anonymous> (/Users/xo/code/SickRage/node_modules/ava/lib/test-worker.js:35:22)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)

  1 exception

  ✖ test/index.js exited with a non-zero exit code: 1

test/helpers/setup-browser-env.js

import browserEnv from 'browser-env';

browserEnv();
import test from 'ava';

test/index.js

test('Insert to DOM', t => {
    const div = document.createElement('div');
    document.body.appendChild(div);

    t.is(document.querySelector('div'), div);
});

Confirmed. Seems we do not account for helpers defined in the require config.

Tracking the "require" problem in #1506.

Was this page helpful?
0 / 5 - 0 ratings