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.

Jest doesn't transpile test file in monorepo.

See original GitHub issue

🐛 Bug Report

Recently I’m working on migrating my project to monorepo. but after I set up the monorepo, all the tests failed.

The first issue it that the test files in packages will not be transpiled by babel-jest. so I will get this error:

Click here to see the output log.
yarn run v1.7.0
$ NODE_ENV=test jest --config jest.config.js packages/a/a.spec.js
 FAIL  packages/a/a.spec.js
  ● 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:

    /Users/ulivz/Documents/ULIVZ/__forked__/jest-issue/packages/a/a.spec.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { APP } from './module';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)


After some investigation, and checked out the related issues: #2081, #3202, etc, I found my issue should be caused by monorepo.

To Reproduce

I create a minimal reproduction repo for that: https://github.com/ulivz/jest-6835

git clone https://github.com/ulivz/jest-issue.git
cd jest-issue
yarn
yarn test-1 # test in root dir passed
yarn test-2 # test tests in `packages` failed

All the detailed output has been included in the repo’s readme.

Expected behavior

The test files in packages should be transpiled

Link to repl or repo (highly encouraged)

https://github.com/ulivz/jest-6835

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 8.11.3 - /usr/local/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 5.6.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^23.0.0 => 23.5.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
ranyitzcommented, Aug 15, 2018

As @SimenB suggested, replacing .babelrc with a babel.config.js fixes it.

1reaction
ulivzcommented, Aug 26, 2018

Thanks, it works for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest in a Monorepo doesn't work if running from a specific ...
In my package package.json I have a prepare npm script that transpiles the ES6 to ES5: { ... "main": "lib/index.js", "files": [ "lib"...
Read more >
Monorepo testing using jest projects - Orlando Bayo Adeyemi
Jest projects helps you manage and run tests for project in a mono-repo using a single instance of jest. Goals. Run tests globally...
Read more >
A guide through The Wild Wild West of setting up a mono repo ...
This config will inform Jest about how to handle files correctly. Testing packages. Before we can run our tests we first need to...
Read more >
Configuring Jest
To read TypeScript configuration files Jest requires ts-node . ... You cannot retrieve globals defined here in your test suites.
Read more >
How to Configure Babel For Your Monorepo
In my Pedalboard monorepo I have several packages which are tested using Jest. Jest, as you may know, uses babel-jest OOTB in order...
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