diff --git a/CHANGELOG.md b/CHANGELOG.md index b706156bc4b3..700505ffd8b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-config]` Add `testEnvironmentOptions.html` to apply to jsdom input ([11950](https://github.com/facebook/jest/pull/11950)) + ### Fixes - `[jest-runtime]` Ensure absolute paths can be resolved within test modules ([11943](https://github.com/facebook/jest/pull/11943)) diff --git a/docs/Configuration.md b/docs/Configuration.md index 5b051f3ff450..56131b06edfb 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1170,7 +1170,7 @@ beforeAll(() => { Default: `{}` -Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. +Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{html: "", userAgent: "Agent/007"}`. ### `testFailureExitCode` \[number] diff --git a/e2e/custom-jsdom-html/__tests__/test.js b/e2e/custom-jsdom-html/__tests__/test.js new file mode 100644 index 000000000000..5a14ac9299f3 --- /dev/null +++ b/e2e/custom-jsdom-html/__tests__/test.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +test('jsdom custom html', () => { + /* eslint-disable-next-line no-undef */ + expect(document.getElementById('root')).toBeTruthy(); +}); diff --git a/e2e/custom-jsdom-html/babel.config.js b/e2e/custom-jsdom-html/babel.config.js new file mode 100644 index 000000000000..cea7d0f507a7 --- /dev/null +++ b/e2e/custom-jsdom-html/babel.config.js @@ -0,0 +1,19 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +module.exports = { + presets: [ + [ + '@babel/env', + { + targets: { + node: 'current', + }, + }, + ], + ], +}; diff --git a/e2e/custom-jsdom-html/package.json b/e2e/custom-jsdom-html/package.json new file mode 100644 index 000000000000..ef3ddfb5b0c4 --- /dev/null +++ b/e2e/custom-jsdom-html/package.json @@ -0,0 +1,12 @@ +{ + "dependencies": { + "@babel/core": "^7.2.2", + "@babel/preset-env": "^7.2.2" + }, + "jest": { + "testEnvironment": "jsdom", + "testEnvironmentOptions": { + "html": "
" + } + } +} diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 39b63efc4607..8db58a76a36e 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -31,19 +31,26 @@ class JSDOMEnvironment implements JestEnvironment { moduleMocker: ModuleMocker | null; constructor(config: Config.ProjectConfig, options?: EnvironmentContext) { - this.dom = new JSDOM('', { - pretendToBeVisual: true, - resources: - typeof config.testEnvironmentOptions.userAgent === 'string' - ? new ResourceLoader({ - userAgent: config.testEnvironmentOptions.userAgent, - }) - : undefined, - runScripts: 'dangerously', - url: config.testURL, - virtualConsole: new VirtualConsole().sendTo(options?.console || console), - ...config.testEnvironmentOptions, - }); + this.dom = new JSDOM( + typeof config.testEnvironmentOptions.html === 'string' + ? config.testEnvironmentOptions.html + : '', + { + pretendToBeVisual: true, + resources: + typeof config.testEnvironmentOptions.userAgent === 'string' + ? new ResourceLoader({ + userAgent: config.testEnvironmentOptions.userAgent, + }) + : undefined, + runScripts: 'dangerously', + url: config.testURL, + virtualConsole: new VirtualConsole().sendTo( + options?.console || console, + ), + ...config.testEnvironmentOptions, + }, + ); const global = (this.global = this.dom.window.document .defaultView as unknown as Win);