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

feat(environment-jsdom): allow passing html content to jsdom environment #11950

Merged
merged 7 commits into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

SimenB marked this conversation as resolved.
Show resolved Hide resolved
- `[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))
Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<html lang="zh-cmn-Hant"></html>", userAgent: "Agent/007"}`.

### `testFailureExitCode` \[number]

Expand Down
11 changes: 11 additions & 0 deletions e2e/custom-jsdom-html/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -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();
});
19 changes: 19 additions & 0 deletions e2e/custom-jsdom-html/babel.config.js
Original file line number Diff line number Diff line change
@@ -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',
},
},
],
],
};
12 changes: 12 additions & 0 deletions e2e/custom-jsdom-html/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"dependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.2"
},
"jest": {
"testEnvironment": "jsdom",
"testEnvironmentOptions": {
"html": "<div id=\"root\"></div>"
}
}
}
33 changes: 20 additions & 13 deletions packages/jest-environment-jsdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@ class JSDOMEnvironment implements JestEnvironment<number> {
moduleMocker: ModuleMocker | null;

constructor(config: Config.ProjectConfig, options?: EnvironmentContext) {
this.dom = new JSDOM('<!DOCTYPE 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,
});
this.dom = new JSDOM(
typeof config.testEnvironmentOptions.html === 'string'
? config.testEnvironmentOptions.html
: '<!DOCTYPE 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);

Expand Down