Skip to content

Tests fail with async #1160

Closed
Closed

Description

@EnoahNetzach

Not completely sure this is an issue of this repo, could be a jest's one. I'm reporting it here because it could be a configuration problem.

Description

Running npm test fails when using async/await.

Expected behavior

The test runs

Actual behavior

I receive an error:

  ● Test suite failed to run

    /src/merchants.js: Expected type "Expression" with option {}
      
      at Object.t.(anonymous function) [as assertExpression] (node_modules/babel-types/lib/index.js:362:13)
      at Array.forEach (native)

Environment

Run these commands in the project folder and fill in their results:

  • npm ls react-scripts: v0.8.1

  • node -v: v6.9.1

  • npm -v: v3.10.8

  • Operating system: Fedora 24

  • Browser and version: --

Reproducible Demo

Just a new "cra" project, with an async/await function tested.

I have a demonstration here, just git clone, npm install and npm test.

Activity

  1. added this to the 0.8.2 milestone on Dec 5, 2016
  2. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    The problem seems to be caused by some interaction between the return value of an awaited function and destructuring it.

    E.g.:

    const getId = async () => {
      const p = await dispatch(42)
      return p.id
    }

    works, while

    const getId = async () => {
      const { id } = await dispatch(42)
      return id
    }

    or

    const getId = async () => {
      const p = await dispatch(42)
      const { id } = p
      return id
    }

    don't work.

  3. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    In facts, tapping into node_modules/babel-generator/lib/buffer.js and casting the argument str to a String, I get an error that looks like:

    var p, null;return _regenerator2.default.wrap
           ^^^^
  4. gaearon commented on Dec 5, 2016

    @gaearon
    Contributor

    Did it work before?

  5. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    Yes, we're migrating a project from v0.6.1 to the latest

  6. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    I'm getting a lot of "strange" errors.

    Another one, for example, is that while using immutable, this won't work anymore:

    const { Record } = require("immutable")
    
    const User = new Record({ id: NaN })
    new User({ id: 42 })

    while it worked before (and works now, same version).

    and the error is:

    TypeError: _definition2.default is not a constructor
    
  7. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    I was able to fix all the errors by reintroducing babel-preset-latest in the test env, so it might be some misconfiguration of babel-preset-env?

  8. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    Also removing { targets: { node: 'current' } }, without reintroducing babel-preset-latest seems to make the deed.

  9. gaearon commented on Dec 5, 2016

    @gaearon
    Contributor

    Can you try:

    ?

  10. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    If you mean like this:

    // ...
    
    const plugins = [
    
    // ...
    
        // Polyfills the runtime needed for async/await and generators
        [require.resolve('babel-plugin-transform-runtime'), {
          helpers: false,
          polyfill: false,
          regenerator: true,
          // Resolve the Babel runtime relative to the config.
          moduleName: path.dirname(require.resolve('babel-runtime/package'))
        }],
        // The following two plugins are currently necessary to get
        // babel-preset-env to work with rest/spread. More info here:
        // https://github.com/babel/babel-preset-env#caveats
        // https://github.com/babel/babel/issues/4074
        // const { a, ...z } = obj;
        require.resolve('babel-plugin-transform-es2015-destructuring'),
        // const fn = ({ a, ...otherProps }) => otherProps;
        require.resolve('babel-plugin-transform-es2015-parameters')
      ];
    
    // ...
    
    if (env === 'test') {
      module.exports = {
        presets: [
          // ES features necessary for user's Node version
          [require('babel-preset-env').default, {
            targets: {
              node: 'current',
            },
          }],
    
    // ...

    no, unfortunately it doesn't work (I always double-check it by reintroducing babel-preset-latest at the end)

  11. gaearon commented on Dec 5, 2016

    @gaearon
    Contributor

    @hzoo Any ideas?

  12. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    The result I get from babel-preset-env is that I need:

    
    check-es2015-constants: false
    syntax-trailing-function-commas: true
    transform-async-to-generator: true
    transform-es2015-arrow-functions: false
    transform-es2015-block-scoped-functions: false
    transform-es2015-block-scoping: false
    transform-es2015-classes: false
    transform-es2015-computed-properties: false
    transform-es2015-destructuring: false
    transform-es2015-for-of: false
    transform-es2015-function-name: false
    transform-es2015-literals: false
    transform-es2015-object-super: false
    transform-es2015-parameters: false
    transform-es2015-shorthand-properties: false
    transform-es2015-spread: false
    transform-es2015-sticky-regex: false
    transform-es2015-template-literals: false
    transform-es2015-typeof-symbol: false
    transform-es2015-unicode-regex: false
    transform-exponentiation-operator: true
    transform-regenerator: false
    

    I'll try to reintroduce them one by one..

  13. gaearon commented on Dec 5, 2016

    @gaearon
    Contributor

    Thanks for your help debugging this.

  14. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    No worries ^_^

    Apparently what I need are:

    • transform-es2015-arrow-functions
    unknown Expression of type "ArrowFunctionExpression"
    
    • transform-es2015-destructuring
    Expected type "Expression" with option {}
    
    • transform-es2015-parameters
    var addIndex = function addIndex({ index, ...routes } = {}, base)
                                              ^^^
        SyntaxError: Unexpected token ...
    

    so yeah, the two you were saying before, plus () => {}.

    Although we use es6+ a lot, and we have approx 230 unit tests, I have to say that these could be only our needs, and others might need more plugins..

  15. gaearon commented on Dec 5, 2016

    @gaearon
    Contributor

    So these are likely Babel bugs about hidden plugin dependencies? Can you reduce each to an isolated Babel bug report?

  16. EnoahNetzach commented on Dec 5, 2016

    @EnoahNetzach
    ContributorAuthor

    Either that, or some bug in babel-plugin-env, I'm going to investigate further tomorrow.

    In either case should I make a PR reverting to using babel-plugin-latest for the moment?

  17. gaearon commented on Dec 6, 2016

    @gaearon
    Contributor

    I won't be able to cut a release today anyway so might as well wait for more details (or investigate myself tomorrow if I get there first). Once we know more we'll decide and cut a release.

  18. EnoahNetzach commented on Dec 6, 2016

    @EnoahNetzach
    ContributorAuthor

    Seems reasonable

  19. existentialism commented on Dec 6, 2016

    @existentialism
    Contributor

    @EnoahNetzach I'll try and take a look at this later tonight as well

  20. EnoahNetzach commented on Dec 6, 2016

    @EnoahNetzach
    ContributorAuthor

    It seems that babel-plugin-transform-regenerator tries to assert that an ObjectPattern is of type Expression.

    I think that definitely we should ask help from @kittens?

  21. EnoahNetzach commented on Dec 6, 2016

    @EnoahNetzach
    ContributorAuthor

    btw, this seems to be already issued on babel, maybe I'll continue the conversation there.

  22. gaearon commented on Dec 6, 2016

    @gaearon
    Contributor

    Is this issue underlying for all the problems listed here?

  23. EnoahNetzach commented on Dec 6, 2016

    @EnoahNetzach
    ContributorAuthor

    Not entirely sure, I checked only transform-es2015-destructuring for now

  24. EnoahNetzach commented on Dec 6, 2016

    @EnoahNetzach
    ContributorAuthor

    I'm closing this issue in favor to #1156

  25. locked and limited conversation to collaborators on Jan 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions