Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest tests are failing when running within a Yarn workspace #4021

Closed
rolandjitsu opened this issue Feb 13, 2018 · 10 comments
Closed

jest tests are failing when running within a Yarn workspace #4021

rolandjitsu opened this issue Feb 13, 2018 · 10 comments

Comments

@rolandjitsu
Copy link

rolandjitsu commented Feb 13, 2018

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
sauron@0.4.0 /Users/agroza/Projects/bbp/sauron
└── (empty)

# react-scripts-ts
sauron@0.4.0 /Users/agroza/Projects/bbp/sauron
└── react-scripts-ts@2.13.0  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.

@rolandjitsu
Copy link
Author

I'm guessing it could have something to do with jestjs/jest#5401?

@rolandjitsu
Copy link
Author

rolandjitsu commented Feb 14, 2018

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).

@bradfordlemley
Copy link
Contributor

Your app is using react-scripts-ts@2.13.0 which uses jest@20.0.4, but package-a and -b are using jest@22.1.4.

If you change your package-a and package-b to use jest@20.0.4, 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.

@rolandjitsu
Copy link
Author

rolandjitsu commented Feb 15, 2018

@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.

@rolandjitsu
Copy link
Author

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

@bradfordlemley
Copy link
Contributor

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 jest@20.0.4 as described above.)

@rolandjitsu
Copy link
Author

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.

@huchenme
Copy link
Contributor

huchenme commented May 15, 2018

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
},

@nnennajohn
Copy link

Thank you @huchenme .

@bugzpodder
Copy link

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

@facebook facebook locked as resolved and limited conversation to collaborators May 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants