Create-react-app: jest tests are failing when running within a Yarn workspace

Created on 13 Feb 2018  ·  10Comments  ·  Source: facebook/create-react-app

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes.

Which terms did you search for in User Guide?

yarn workspaces, jest fails, yarn jest, jest env, dispose is not a function, jest typeError environment, etc.

Environment

  1. node -v: 894
  2. npm -v: 5.6.0
  3. yarn --version (if you use Yarn): 1.3.2
  4. npm ls react-scripts (if you haven’t ejected):
# react-scripts
[email protected] /Users/agroza/Projects/bbp/sauron
└── (empty)

# react-scripts-ts
[email protected] /Users/agroza/Projects/bbp/sauron
└── [email protected]  extraneous

Then, specify:

  1. Operating system: Mac OS X Sierra 10.12
  2. Browser and version (if relevant):

Steps to Reproduce

  1. Clone cra-workspaces
  2. Run yarn install at root
  3. Navigate to apps/cra and run yarn test
  4. Navigate to packages/package-a/packages/package-b and run yarn run test:cov

Expected Behavior

Tests should run without errors.

Actual Behavior

Some tests are throwing:

 FAIL  src/foo.spec.ts
  ● Test suite failed to run

    TypeError: environment.setup is not a function

      at ../../node_modules/jest-runner/build/run_test.js:112:23

Reproducible Demo

See cra-workspaces repo for an example of setup that has this issue.

Most helpful comment

Just to update that after hours of trying, I am able to find a way to resolve this issue:

Add this piece of code into your root package.json

"resolutions": {
  "jest": "^22.4.3",
  "babel-jest": "^22.4.3", // if you are using babel-jest as well
  "babel-core": "^7.0.0-bridge.0" // if you are using babel 7
},

All 10 comments

I'm guessing it could have something to do with https://github.com/facebook/jest/issues/5401?

Ok, after trying out a few more things, it seems like having jest as a dep. in another workspace causes the issue.

It could be some incompatibilities between versions of jest, but I'm actually also fixed at the same version that react-scripts is (22.1.4).

Your app is using [email protected] which uses [email protected], but package-a and -b are using [email protected].

If you change your package-a and package-b to use [email protected], you won't have this error. (I don't think you need the jest-environment-jsdom dependency in package-a or -b -- I think you can just remove those, otherwise they probably need to match, too.)

I don't know of a way to use two different versions of jest, possibly with a --nohoist option which is not yet supported by yarn, but will be soon.

@bradfordlemley yes, that seems to be true. I used the version I saw on the react-scripts-ts repo instead of the package.json from the npm dist.

I'll give it a shot to see if anything changes.

Ok, now the cra app tests throw that error but the packages' tests work just fine.

You probably have some old dependencies hanging around. Delete all the node_modules dirs (cra-workspaces/node_modules, app/cra/node_modules, packages/package-a/node_modules, packages/package-b/node_modules), then run yarn to re-install. (FYI, I was able to successfully run tests from both packages and the app in your example monorepo after updating the packages to use [email protected] as described above.)

Oh, my bad. I tried with 22.0.4, not 20.0.4. Unfortunately for me, I cannot back to 20 due to jsdom upgrade. So if I do, some of my tests will be broken anyway 😆

I created an issue to ask for a version bump though.

Thanks.

Just to update that after hours of trying, I am able to find a way to resolve this issue:

Add this piece of code into your root package.json

"resolutions": {
  "jest": "^22.4.3",
  "babel-jest": "^22.4.3", // if you are using babel-jest as well
  "babel-core": "^7.0.0-bridge.0" // if you are using babel 7
},

Thank you @huchenme .

Looks like this issue has been resolved. Please open a new issue if you continue to experience problems.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rovansteen picture rovansteen  ·  192Comments

gaearon picture gaearon  ·  86Comments

Timer picture Timer  ·  144Comments

jantimon picture jantimon  ·  154Comments

riceyeh picture riceyeh  ·  116Comments