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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsdom integration with custom resource loader doesn't work in parallel mode #8701

Closed
sormy opened this issue Jul 17, 2019 · 3 comments
Closed

Comments

@sormy
Copy link

sormy commented Jul 17, 2019

馃悰 Bug Report

JSDOM integration with custom resource loader doesn't work in parallel mode.
JSDOM ResourceLoader: https://github.com/jsdom/jsdom#advanced-configuration
testEnvironmentOptions is serialized to simple JSON to pass to the worker thus causing custom resource loader to be JSONified. Tests do not work as expected in parallel mode and are producing cryptic error:

    TypeError: resources must be an instance of ResourceLoader

      at resourcesToResourceLoader (node_modules/jsdom/lib/api.js:324:15)
      at transformOptions (node_modules/jsdom/lib/api.js:268:46)
      at new JSDOM (node_modules/jsdom/lib/api.js:35:15)

Related to #8393

To Reproduce

jest-config.js

const { ResourceLoader } = require("jsdom")

class CustomResourceLoader extends ResourceLoader {}

module.exports = {
  testEnvironment: "jsdom-thirteen",
  testEnvironmentOptions: {
    runScripts: "dangerously",
    resources: new CustomResourceLoader(),
  },
}

package.json:

    "jest": "^24.5.0",
    "jest-environment-jsdom-thirteen": "^1.0.1",
    "jsdom": "^13.1.0",

It should be at least 2 tests and jest cache cleared to trigger parallel build.

Expected behavior

Ideally, there should be another way to pass environment options to jest worker, worker compatible. If it is not possible, jest should at least try to detect attempts to pass classes as testEnvironmentOptions and raise an error or warning clearly stating the problem.

Link to repl or repo (highly encouraged)

Will provide if needed. Seems like issue is well-known.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS Sierra 10.12.6
    CPU: (4) x64 Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz
  Binaries:
    Node: 12.5.0 - /usr/local/bin/node
    Yarn: 1.17.0 - /usr/local/bin/yarn
    npm: 6.9.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^24.5.0 => 24.7.1 
@thymikee
Copy link
Collaborator

Jsdom is supposed to be used as a custom environment, did you try that?

@SimenB
Copy link
Member

SimenB commented Jul 17, 2019

  testEnvironmentOptions: {
    runScripts: "dangerously",
    resources: new CustomResourceLoader(),
  },

This won't work as the config is serialized (we probably should throw an error...) to a string, so your instance is lost. What you have to do, as @thymikee alludes to (and I wrote in the issue you linked to), is create a custom test environment.

I can quickly put together an example (untested)

const JSDOMEnvironment = require('jest-environment-jsdom-thirteen');
const {ResourceLoader} = require('jsdom');

class CustomResourceLoader extends ResourceLoader {}

module.exports = class JSDOMEnvironmentWithResources extends JSDOMEnvironment {
  constructor(config, options) {
    super(
      {
        ...config,
        testEnvironmentOptions: {
          ...config.testEnvironmentOptions,
          resources: new CustomResourceLoader(),
        },
      },
      options,
    );
  }
};

For Jest 28 and after the constructor arguments of environments were changed (in #12461). So new example is:

const {default: JSDOMEnvironment} = require('jest-environment-jsdom');
const {ResourceLoader} = require('jsdom');

class CustomResourceLoader extends ResourceLoader {}

module.exports = class JSDOMEnvironmentWithResources extends JSDOMEnvironment {
  constructor(config, options) {
    super(
      {
        ...config,
        projectConfig: {
          ...config.projectConfig,
          testEnvironmentOptions: {
            ...config.projectConfig.testEnvironmentOptions,
            resources: new CustomResourceLoader(),
          },
        },
      },
      options,
    );
  }
};

jest should at least try to detect attempts to pass classes as testEnvironmentOptions and raise an error or warning clearly stating the problem.

Agreed - it will be (as mentioned) addressed in #7185

@SimenB SimenB closed this as completed Jul 17, 2019
@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 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants