Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## main

### Features

Comment thread
SimenB marked this conversation as 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
4 changes: 4 additions & 0 deletions e2e/custom-jsdom-html/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test('jsdom custom html', () => {
/* eslint-disable-next-line no-undef */
expect(document.getElementById('root')).toBeTruthy();
});
12 changes: 12 additions & 0 deletions e2e/custom-jsdom-html/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/env',
{
targets: {
node: 'current',
},
},
],
],
};
13 changes: 13 additions & 0 deletions e2e/custom-jsdom-html/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"dependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.2"
},
"jest": {
"testEnvironment": "jsdom",
"testEnvironmentOptions": {
"html": "<div id=\"root\"></div>"
}
}
}

Comment thread
SimenB marked this conversation as resolved.
Outdated
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