Jest: Jest doesn't transpile test file in monorepo.

Created on 13 Aug 2018  ·  4Comments  ·  Source: facebook/jest

🐛 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

Most helpful comment

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

All 4 comments

I haven't tested, but this seems like https://github.com/facebook/jest/issues/6053#issuecomment-383632515

Could you confirm if that fixes it or not?

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

Awesome! Closing then 🙂

Thanks, it works for me.

Was this page helpful?
0 / 5 - 0 ratings