Jest: Async Iterators don't work

Created on 19 Jun 2018  路  11Comments  路  Source: facebook/jest

馃悰 Bug Report

Async Iterators or generators simply don't work with jest even thought they're natively supported in Node 10

To Reproduce

  1. Install Node 10.x.x
  2. cd to a test project where jest works
  3. add async function* foo() {} to some file
  4. run your project (with yarn start or node index.js etc.)
  5. Notice that it works
  6. Try running your test cases yarn test
  7. Notice that jest complains about "unexpected token *"

Expected behavior

The test suite should run normally and not throw this error

Link to repl or repo (highly encouraged)

Seems like repl.it doesn't have node 10. Sorry :/

Issues without a reproduction link are likely to stall.

Run npx envinfo --preset jest

Paste the results here:

npx: installed 1 in 1.672s

  System:
    OS: macOS High Sierra 10.13.5
    CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  Binaries:
    Node: 10.3.0 - ~/.nvm/versions/node/v10.3.0/bin/node
    Yarn: 1.7.0 - ~/.yarn/bin/yarn
    npm: 6.1.0 - ~/.nvm/versions/node/v10.3.0/bin/npm

Most helpful comment

I've been able to get this working by:

  1. Creating the following .babelrc in my project.
{
    "plugins": ["syntax-async-generators"]
}
  1. Installing babel-plugin-syntax-async-generators by
npm install babel-plugin-syntax-async-generators --save-dev

After that, I got expected behavior. Having said that, I think that if those async generators and async iteration are natively supported by node starting from 10.5.0 tools like jest should support them out of box as well.

All 11 comments

@pranaygp tried testing async function on node 10.5.0 and it is working fine.
Here is one example to test generator function:
screen shot 2018-06-29 at 10 42 45 pm

@ASHISH932 I'm dealing with the exact same case as @pranaygp described and it or no it doesn't make a difference. I get that unexpected token * no matter what.

I've been able to get this working by:

  1. Creating the following .babelrc in my project.
{
    "plugins": ["syntax-async-generators"]
}
  1. Installing babel-plugin-syntax-async-generators by
npm install babel-plugin-syntax-async-generators --save-dev

After that, I got expected behavior. Having said that, I think that if those async generators and async iteration are natively supported by node starting from 10.5.0 tools like jest should support them out of box as well.

@nooga you should be using env preset of babel(babel-preset-env) as it's support latest javascript feature, whenever it's get official . Here is an article to set up your react environment.

@ASHISH932 Thanks, although I don't use babel in my project directly and it has absolutely nothing to do with react.

@nooga that's up to you to decide architecture of your project but if you read documentation of jest they have mentioned to use env preset

@ASHISH932, I am using node 10.5. Still getting the error after I added a .babelrc.js (below). How do you suggest using babel-preset-env?

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current'
        }
      }
      }
    ]
  ]
}

EDIT

Possibly related to the fact that I am using ts-jest, which skips babel -- https://github.com/kulshekhar/ts-jest/issues/471#issuecomment-375739463

AFAIK this should work without babel if node accepts without babel.

Node does support async generators starting from version 10.3.
So jest _should_ work without any babel configuration on latest node.

@shaunc if you are using babel this comment https://github.com/facebook/jest/issues/4936#issuecomment-373772896 might be helpful for you

This is essentially the same issue as #6829.

tl;dr: Jest uses babel by default, so you have to tell it to understand your syntax or explicitly disable babel.

You can follow https://github.com/babel/babel/issues/7660 for what I think is the correct solution to this problem

@SimenB in https://github.com/kulshekhar/ts-jest/issues/471 the problem is that ts-jest passes typescript compiler results to jest without using babel by default. So

or explicitly disable babel.

doesn't actually work, but it should

That's something that should be addressed in ts-jest then.

Remember that Babel is needed for code coverage, though.

Was this page helpful?
0 / 5 - 0 ratings