Skip to content

Commit

Permalink
feat(environment-jsdom): allow passing html content to jsdom environm…
Browse files Browse the repository at this point in the history
…ent (#11950)
  • Loading branch information
MarvelSQ authored Oct 16, 2021
1 parent ae1f04b commit 7f39f0a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
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

- `[jest-config]` Add `testEnvironmentOptions.html` to apply to jsdom input ([11950](https://github.com/facebook/jest/pull/11950))

### Fixes

- `[expect]` Tweak and improve types ([#11949](https://github.com/facebook/jest/pull/11949))
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

0 comments on commit 7f39f0a

Please sign in to comment.