question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when using jest + collectCoverage/babel-plugin-lodash

See original GitHub issue

I posted this issue before on https://github.com/facebook/jest/issues/3959 and https://github.com/lodash/babel-plugin-lodash/issues/179

Using babel-plugin-lodash, collectCoverage enabled and a lodash function as show below imported from test file, results into an error.

import { flow, flatten } from 'lodash/fp'

export const testFunction1 = flatten 
// export const testFunction2 = flow(flatten) 

example#testFunction1 npm test will output

    /home/nam/projects/jest-babel-lodash-issue/src/example.js:3
    var testFunction1 = /* istanbul ignore next */exports.testFunction1 = ();
                                                                           ^
    
    SyntaxError: Unexpected token )

example#testFunction2 npm test will output

    ReferenceError: /home/nam/projects/jest-babel-lodash-issue/src/example.js: Container is falsy
      
      at NodePath._replaceWith (node_modules/babel-traverse/lib/path/replacement.js:170:11)

Removing collectCoverage from package.json lets the tests run without failure. Removing babel-plugin-lodash from .babelrc or importing directly from lodash also runs without failure.

Testcase https://github.com/namjul/jest-babel-lodash-issue

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:8
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
patsissonscommented, May 1, 2018

Ran into this too with babel-plugin-dotenv-import (which is very similar to react-native-dotenv). The solution is to import all dotenv defines in a single file and then ignore that file for coverage:

    "coveragePathIgnorePatterns": [
      "<rootDir>/node_modules/",
      "<rootDir>/src/utils/env.js"
    ],
1reaction
AndersDJohnsoncommented, Jul 21, 2017

Have you tried moving thebabel-plugin-lodash plugin to a non-test environment config?

{
  "plugins": [],
  "env": {
    "production": {
      "plugins": ["lodash"]
     }
  }
}

then when running tests, use:

BABEL_ENV=test jest --coverage

https://github.com/namjul/jest-babel-lodash-issue/issues/1 https://github.com/facebook/jest/issues/3959#issuecomment-317018979

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
Indicates whether the coverage information should be collected while executing the test. Because this retrofits all executed files with coverage ...
Read more >
Configuring Jest compiled - w3resource
In this tutorial we will focus on configuring Jest. Jest's configuration can be defined inside the package.json file of your project, ...
Read more >
Invalid or unexpected token while Jest migration
Adding these plugins in babel.config solved the issue for me 'transform-class-properties', 'babel-plugin-parameter-decorator'.
Read more >
jest lodash-es - You.com | The AI Search Engine You Control
I am writing a library using typescript, jest and lodash and I would like to ... Here's the Jest error, related to Jest...
Read more >
Configuring Jest
When using the --config option, the JSON file must not contain a "jest" key: ... can be used here to have Jest stop...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found