Ava: Clarify docs regarding async function support

Created on 3 Mar 2017  Â·  4Comments  Â·  Source: avajs/ava

Description

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 Source

test('async', async t => {
    t.is(await foo(), 'bar');
});

Error Message & Stack Trace

/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

Config

Copy the relevant section from package.json:

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

Command-Line Arguments


Relevant Links

Gist to reproduce: https://gist.github.com/avaly/b50d0d1d7635aeac101406f9aedb3e2d

Adding babel-polyfill to the ava.require entry makes the test pass.

Environment

Node.js v6.9.4 / v7.7.1
linux 4.4.0-64-generic
ava 0.18.2
yarn 0.20.3

Most helpful comment

  "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.

All 4 comments

"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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sindresorhus picture sindresorhus  Â·  5Comments

clitetailor picture clitetailor  Â·  3Comments

sindresorhus picture sindresorhus  Â·  3Comments

electerious picture electerious  Â·  3Comments

niftylettuce picture niftylettuce  Â·  4Comments