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

Support testEnvironment=jest-environment-jsdom with pnpm #6145

Closed
wants to merge 3 commits into from

Conversation

solsson
Copy link

@solsson solsson commented May 7, 2018

Summary

The dynamic require of testEnvironment (transpiled to const testFramework = require(config.testRunner) in published source) is incompatible with pnpm.

I agree with pnpm/pnpm#1130 (comment).

For environment to be truly pluggable, with stricter npm install, I guess this PR needs to consider how to support environments other than jsdom. It could be argued that a class should be injected, rather than required using a runtime value. In practice though I think the suggested addition to package.json solves the problem.

Test plan

Fixes #5369

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@codecov-io
Copy link

codecov-io commented May 7, 2018

Codecov Report

Merging #6145 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #6145   +/-   ##
=======================================
  Coverage   64.19%   64.19%           
=======================================
  Files         219      219           
  Lines        8423     8423           
  Branches        4        4           
=======================================
  Hits         5407     5407           
  Misses       3015     3015           
  Partials        1        1

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4ca03cd...0f1d392. Read the comment docs.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this, it should also include jest-environment-node, and remove them from jest-config.

Also please update the changelog

@SimenB
Copy link
Member

SimenB commented May 7, 2018

Sorta related #5913

@SimenB
Copy link
Member

SimenB commented May 7, 2018

Another possibility is to do require.resolve in jest-config (and the whole "try with jest-environment prefix" logic), and pass the full path to the runner.

@cpojer @thymikee @rickhanlonii thoughts? I'd also like to not paint us into a corner with regards to #5913 (maybe even solve it at the same time)

@octogonz
Copy link

This issue is affecting my application also, because we have to install two versions of Jest side by side. In our specific case, the jest-runner@21.2.1 ends up requiring jest-environment-jsdom@22.4.3 which causes this error:

TypeError: environment.dispose is not a function

PNPM brings the problem to light, but this is fundamentally an architectural problem with Jest not just a compatibility issue: In the modern world, libraries cannot naively call require(X) to fish a dependency out of the node_modules folder without any idea of what version of X they will get. The version needs to be constrained somehow, either directly via package.json as a dependency, or indirectly via a peer dependency or injection.

The approach suggested in this PR would help the simple case where you get jsdom as your default. However the jsdom dependency is like 11MB with all its dependencies. Forcing everyone to install that and probably also jest-environment-node seems contrary to the goal of modeling the environment as a swappable plug-in. It also doesn't help advanced users who have custom packages.

It also poses a backwards compatibility problem: If jest-environment-jsdom is a peer dependency, upgrading consumers will fail to install unless they satisfy a new peer. Or if it's a regular dependency, consumers may get an unexpected version if the node_modules tree gets shuffled around.

A better solution would be to make options.testEnvironment an injectable instance instead of a simple text string. This is how other libraries treat their plugins, and it is the architecturally sound way to specify a plugin.

@octogonz
Copy link

octogonz commented May 11, 2018

I tried creating a PR that would illustrate how to allow something like this:

// jest.config.js
module.exports = {
  verbose: true,
  testEnvironment: require('my-custom-environment'),
};

or for jest.config.json

{
  "verbose": true,
  "testEnvironment": "../node_modules/my-custom-environment"
}

However I cannot seem to get the master branch of Jest to build at all, on Windows or Mac. The yarn install keeps failing with various errors...

@solsson
Copy link
Author

solsson commented May 15, 2018

@SimenB Would you be willing to merge this fix, or @pgonzal's fix, as an intermediate solution?

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing require.resolve from within jest-config and pass a full path through to the runner might make the most sense. However, this change should work, and is better than nothing

@cpojer cpojer requested a review from arcanis May 16, 2018 11:48
@cpojer
Copy link
Member

cpojer commented May 16, 2018

Leaving this one for @arcanis to merge.

@SimenB SimenB added this to the Jest 23 milestone May 21, 2018
@thymikee
Copy link
Collaborator

@arcanis friendly ping :)

@@ -12,8 +12,6 @@
"babel-jest": "^22.4.1",
"chalk": "^2.0.1",
"glob": "^7.1.1",
"jest-environment-jsdom": "^22.4.1",
"jest-environment-node": "^22.4.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot remove them from here, because they are required by the following line:

https://github.com/facebook/jest/blob/master/packages/jest-config/src/utils.js#L123

If you remove them from the package list, you start relying on the hoisting to make sure that the environments are siblings of jest-config, which is an unsafe assumption.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a require based on a runtime variable is done in both jest-runner and jest-config. I guess that has implications on @SimenB's suggestion "pass a full path through to the runner" too.

I suppose that leaves us with the option of some refactoring towards dependency injection, and maybe @pgonzal's suggestion with a require done at config time is a simple way to do that.

@cpojer cpojer removed this from the Jest 23 milestone May 24, 2018
@thymikee
Copy link
Collaborator

thymikee commented Jul 6, 2018

@solsson mind taking a look at @arcanis #6145 (comment)?

@thymikee
Copy link
Collaborator

thymikee commented Aug 8, 2018

It's been a month already without a sign of interest, closing. Happy to reopen once the feedback is addressed :)

@github-actions
Copy link

This pull request 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Not working if Jest is installed by pnpm
8 participants