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-runner require "jest-environment-jsdom" without it being listed in its dependencies #7064

Closed
arcanis opened this issue Sep 28, 2018 · 7 comments · Fixed by #7476
Closed
Labels
Milestone

Comments

@arcanis
Copy link
Contributor

arcanis commented Sep 28, 2018

🐛 Bug Report

Full error:

Package "[email protected]" (via "/Users/mael/Library/Caches/Yarn/v3/npm-jest-runner-23.6.0-3894bd219ffc3f3cb94dc48a4170a2e6f23a5a38/node_modules/jest-runner/build/run_test.js") is trying to require the package "jest-environment-jsdom" (via "jest-environment-jsdom") without it being listed in its dependencies (exit, graceful-fs, jest-config, jest-docblock, jest-haste-map, jest-jasmine2, jest-leak-detector, jest-message-util, jest-runtime, jest-util, jest-worker, source-map-support, throat, jest-runner)

To Reproduce

I'm not sure exactly how envs are specified in Jest; I got the error by running yarn jest in the create-react-app repo, which doesn't have any special kind of configuration.

Expected behavior

No error should be thrown. The environment should by default be already resolved, so that the require call in jest-runner would be a noop (I'm not sure where this default configuration is set, but basically it should be testEnvironment ||= require.resolve('jest-environment-jsdom') rather than testEnvironment ||= 'jest-environment-jsdom').

@SimenB
Copy link
Member

SimenB commented Sep 28, 2018

I'm guessing this is a PnP thing?

String comes from here: https://github.com/facebook/jest/blob/f8efff95431dd079abf8ae02b8e541e3019d211b/packages/jest-config/src/Defaults.js#L66

How will this work with people doing @jest-environment my-custom-thing in a docblock and us doing require on it? See https://jestjs.io/docs/en/configuration#testenvironment-string

@SimenB
Copy link
Member

SimenB commented Sep 28, 2018

The resolution should happen here: https://github.com/facebook/jest/blob/f8efff95431dd079abf8ae02b8e541e3019d211b/packages/jest-config/src/utils.js#L105-L142

Ideas on how to make that better/safer/saner? We've had issues with pnpm as well with the environment require before (see #6145)

EDIT: It should be normalized during startup actually, not sure why it's not enough: https://github.com/facebook/jest/blob/f8efff95431dd079abf8ae02b8e541e3019d211b/packages/jest-config/src/normalize.js#L373-L375

@SimenB
Copy link
Member

SimenB commented Sep 28, 2018

@arcanis
Copy link
Contributor Author

arcanis commented Oct 1, 2018

How will this work with people doing @jest-environment my-custom-thing in a docblock and us doing require on it?`

Not great :/

Something that could be interesting would be to use a module instance to tell Jest relative to which module should the environments be loaded (and it would default to itself). Similar to the moduleLoader version of the pnp-webpack-plugin:

const PnpWebpackPlugin = require(`pnp-webpack-plugin`);

module.exports = {
  resolve: {
    plugins: [
      PnpWebpackPlugin,
    ],
  },
  resolveLoader: {
    plugins: [
      PnpWebpackPlugin.moduleLoader(module),
    ],
  },
};

@SimenB SimenB added this to the Jest 24 milestone Oct 16, 2018
@imsnif
Copy link

imsnif commented Nov 29, 2018

Just adding this here for those who might google their way to this thread:

This can be worked around by installing jest-environment-jsdom in the root package of the project (eg. yarn add -D jest-environment-jsdom).

@aleclarson
Copy link
Contributor

aleclarson commented Dec 6, 2018

Specifying an explicit testEnvironment in your Jest config also works. Or provide --env=node when running the Jest CLI. So it seems the problem occurs when testEnvironment is undefined.

edit: Please see #7476

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants