diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index bd860659e71e..167e08059961 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -31,6 +31,13 @@ export default class JSDOMEnvironment implements JestEnvironment { moduleMocker: ModuleMocker | null; constructor(config: Config.ProjectConfig, options?: EnvironmentContext) { + const anyConsole = options?.console ?? console; + const virtualConsole = new VirtualConsole(); + virtualConsole.sendTo(anyConsole, { omitJSDOMErrors: true }); + virtualConsole.on('jsdomError', (error) => { + anyConsole.error(error); + }); + this.dom = new JSDOM( typeof config.testEnvironmentOptions.html === 'string' ? config.testEnvironmentOptions.html @@ -45,9 +52,7 @@ export default class JSDOMEnvironment implements JestEnvironment { : undefined, runScripts: 'dangerously', url: config.testURL, - virtualConsole: new VirtualConsole().sendTo( - options?.console || console, - ), + virtualConsole, ...config.testEnvironmentOptions, }, );