Jest: jsdom integration with custom resource loader doesn't work in parallel mode

Created on 17 Jul 2019  路  2Comments  路  Source: facebook/jest

馃悰 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 https://github.com/facebook/jest/issues/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 
Bug Report Needs Repro Needs Triage

Most helpful comment

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

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

All 2 comments

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

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

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

Was this page helpful?
0 / 5 - 0 ratings