Skip to content

Commit b2e389b

Browse files
committed
fix: log JSDOM errors more cleanly
1 parent c8a8456 commit b2e389b

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- `[expect]` Move typings of `.not`, `.rejects` and `.resolves` modifiers outside of `Matchers` interface ([#12346](https://github.com/facebook/jest/pull/12346))
1919
- `[expect]` Expose `AsymmetricMatchers` and `RawMatcherFn` interfaces ([#12363](https://github.com/facebook/jest/pull/12363))
2020
- `[jest-environment-jsdom]` Make `jsdom` accessible to extending environments again ([#12232](https://github.com/facebook/jest/pull/12232))
21+
- `[jest-environment-jsdom]` Log JSDOM errors more cleanly ([#12386](https://github.com/facebook/jest/pull/12386))
2122
- `[jest-config]` Correctly detect CI environment and update snapshots accordingly ([#12378](https://github.com/facebook/jest/pull/12378))
2223
- `[jest-jasmine2, jest-types]` [**BREAKING**] Move all `jasmine` specific types from `@jest/types` to its own package ([#12125](https://github.com/facebook/jest/pull/12125))
2324

packages/jest-environment-jsdom/src/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
3131
moduleMocker: ModuleMocker | null;
3232

3333
constructor(config: Config.ProjectConfig, options?: EnvironmentContext) {
34+
const anyConsole = options?.console ?? console;
35+
const virtualConsole = new VirtualConsole();
36+
virtualConsole.sendTo(anyConsole, {omitJSDOMErrors: true});
37+
virtualConsole.on('jsdomError', error => {
38+
anyConsole.error(error);
39+
});
40+
3441
this.dom = new JSDOM(
3542
typeof config.testEnvironmentOptions.html === 'string'
3643
? config.testEnvironmentOptions.html
@@ -45,9 +52,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
4552
: undefined,
4653
runScripts: 'dangerously',
4754
url: config.testURL,
48-
virtualConsole: new VirtualConsole().sendTo(
49-
options?.console || console,
50-
),
55+
virtualConsole,
5156
...config.testEnvironmentOptions,
5257
},
5358
);

0 commit comments

Comments
 (0)