The docs mention that AVA has built-in support for async functions: https://github.com/avajs/ava#async-function-support
However it does not mention that async functions depend on babel-polyfill to be required by AVA. Even for node 7.7.1, which has native support for async functions, the async functions are still transformed by babel.
test('async', async t => {
t.is(await foo(), 'bar');
});
/home/avaly/dev/temp/ava-babel/foo.test.js:16
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(t) {
^
ReferenceError: regeneratorRuntime is not defined
at /home/avaly/dev/temp/ava-babel/foo.test.js:4:1
at Object.<anonymous> (/home/avaly/dev/temp/ava-babel/foo.test.js:4:1)
at Module._compile (module.js:570:32)
at extensions.(anonymous function) (/home/avaly/dev/temp/ava-babel/node_modules/require-precompiled/index.js:13:11)
at Object.require.extensions.(anonymous function) [as .js] (/home/avaly/dev/temp/ava-babel/node_modules/ava/lib/process-adapter.js:105:4)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/avaly/dev/temp/ava-babel/node_modules/ava/lib/test-worker.js:33:1)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
1 exception
✖ foo.test.js exited with a non-zero exit code: 1
Copy the relevant section from package.json:
{
"ava": {
"babel": "inherit",
"require": [
"babel-register"
]
}
}
Gist to reproduce: https://gist.github.com/avaly/b50d0d1d7635aeac101406f9aedb3e2d
Adding babel-polyfill to the ava.require entry makes the test pass.
Node.js v6.9.4 / v7.7.1
linux 4.4.0-64-generic
ava 0.18.2
yarn 0.20.3
"babel": "inherit",
This means you're no longer using AVA's built-in Babel config. Something in your own Babel config is causing async/await to break. It works out of the box, otherwise.
Even for node 7.7.1, which has native support for async functions, the async functions are still transform by babel.
We'll stop transforming async/await for Node.js 8.
I've already added the @ava/stage-4 preset in my babel config (see gist), as instructed by the Babel recipe at: https://github.com/avajs/ava/blob/master/docs/recipes/babelrc.md, so I would expect that would be the only thing needed to configure. Is AVA adding anything else to the babel config in the case where I don't use "babel": "inherit"?
There definitely is some docs missing regarding this, since this can be confusing, especially for new Babel users.
OK, this seems to have been triggered by the transform-regenerator babel plugin which gets included with the es2015 preset.
Closing this, since it's not an AVA issue.
"ava": {
"babel": {
"presets": [
"es2015",
"stage-0"
]
},
"require": [
"babel-register",
"babel-polyfill"
],
"files": [
"src/**/*.unit.test.js",
"test/**/*.unit.test.js"
]
},
Thanks @avaly, adding babel-polyfill fixed it here.
Most helpful comment
Thanks @avaly, adding
babel-polyfillfixed it here.