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.

TypeScript support makes test with certain deps fail

See original GitHub issue

Is this a bug report?

Yes.

Certain dependencies (specifically carbon-charts) make the tests fail. The bug ist that the relevant settings for jest can not be overwritten. See https://github.com/carbon-design-system/carbon-charts/issues/126 for the correspondent issue at carbon-charts.

Did you try recovering your dependencies?

Yes

Steps to Reproduce

npx create-react-app test --typescript
cd test
npm i @carbon/charts --save
echo "import '@carbon/charts';" >> src/App.tsx
npm test

Expected Behavior

Tests should run fine.

Actual Behavior

 FAIL  src/App.test.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /tmp/test/node_modules/@carbon/charts/index.js:2
    import { BaseChart } from "./base-chart";
           ^

    SyntaxError: Unexpected token {

      25 | 
      26 | export default App;
    > 27 | import '@carbon/charts';
         | ^
      28 | 

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:471:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:513:25)
      at Object.<anonymous> (src/App.tsx:27:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.65s

Reproducible Demo

See commands above.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

14reactions
lordicommented, May 9, 2019

My workaround is to change the test command in the package.json:

    "test": "react-scripts test --transformIgnorePatterns 'node_modules/(?!(@carbon/charts|@carbon/charts-react)/).+\\.(js|jsx|ts|tsx)$'",

It’s not perfect, but works for us.

I think the user should be able to at least overwrite transformIgnorePatterns in jest key.

8reactions
volivacommented, Sep 2, 2019

In case someone doesn’t want to use the inline command, another solution is adding it as a jest config override in package.json:

{
  ....,
  "jest": {
    "transformIgnorePatterns": [
      "/!node_modules\\/lodash-es/"
    ]
  }
}

It seems to be supported by CRA, from this line: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/utils/createJestConfig.js#L71

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running tests with Jest: getting an error with TypeScript config ...
I'm using NodeJS, TypeORM with TypeScript. I'm getting this error when running yarn test : Error: Jest: Failed to parse the TypeScript config ......
Read more >
TypeScript unit tests with debugging - Meticulous
A short guide on how to set up TypeScript unit tests with debugging on VsCode, allowing you to set breakpoints on your unit...
Read more >
Documentation - Migrating from JavaScript - TypeScript
If you have a tests folder outside of your src directory, you might have one tsconfig.json in src , and one in tests...
Read more >
TypeScript configuration - Angular
Browsers can't execute TypeScript directly. Typescript must be "transpiled" into JavaScript using the tsc compiler, which requires some configuration. This page ...
Read more >
The correct way of mocking dependencies in the Angular unit ...
I was confused by the whole situation so I started to unravel the mystery that was behind the failing tests. After some time...
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