Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Iterators don't work #6492

Closed
pranaygp opened this issue Jun 19, 2018 · 12 comments
Closed

Async Iterators don't work #6492

pranaygp opened this issue Jun 19, 2018 · 12 comments

Comments

@pranaygp
Copy link

pranaygp commented Jun 19, 2018

🐛 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
@ASHISH932
Copy link

ASHISH932 commented Jun 29, 2018

@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

@nooga
Copy link

nooga commented Jul 4, 2018

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

@nooga
Copy link

nooga commented Jul 4, 2018

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.

@ASHISH932
Copy link

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

@nooga
Copy link

nooga commented Jul 10, 2018

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

@ASHISH932
Copy link

@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

@shaunc
Copy link

shaunc commented Oct 21, 2018

@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 -- kulshekhar/ts-jest#471 (comment)

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

@anru
Copy link

anru commented Oct 23, 2018

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 #4936 (comment) might be helpful for you

@SimenB
Copy link
Member

SimenB commented Oct 23, 2018

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 babel/babel#7660 for what I think is the correct solution to this problem

@SimenB SimenB closed this as completed Oct 23, 2018
@shaunc
Copy link

shaunc commented Oct 23, 2018

@SimenB in kulshekhar/ts-jest#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

@SimenB
Copy link
Member

SimenB commented Oct 30, 2018

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

Remember that Babel is needed for code coverage, though.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants