diff --git a/frontend/jest.config.js b/frontend/jest.config.js index 619e91a..8d6e2fc 100644 --- a/frontend/jest.config.js +++ b/frontend/jest.config.js @@ -8,7 +8,8 @@ const createJestConfig = nextJest({ // Add any custom config to be passed to Jest const customJestConfig = { setupFilesAfterEnv: ['/jest.setup.js'], - testEnvironment: 'jest-environment-jsdom', + // Custom environment that suppresses jsdom VirtualConsole XMLHttpRequest errors + testEnvironment: '/jest.environment.js', moduleNameMapper: { '^next-intl$': '/tests/__mocks__/next-intl.tsx', '^next-intl/routing$': '/tests/__mocks__/next-intl-routing.tsx', diff --git a/frontend/jest.environment.js b/frontend/jest.environment.js new file mode 100644 index 0000000..a46138f --- /dev/null +++ b/frontend/jest.environment.js @@ -0,0 +1,54 @@ +/** + * Custom Jest environment that suppresses jsdom VirtualConsole XMLHttpRequest errors + * + * These errors occur when jsdom tries to make network requests during tests + * (e.g., XMLHttpRequest to localhost:8000) and they fail. They're harmless + * noise that clutters test output. + */ + +// This file is executed by Jest in a CommonJS context; using require() here is intentional. +// eslint-disable-next-line @typescript-eslint/no-require-imports +const JSDOMEnvironment = require('jest-environment-jsdom').default; + +class CustomJSDOMEnvironment extends JSDOMEnvironment { + constructor(config, context) { + // Customize virtualConsole options to suppress specific errors + const customConfig = { + ...config, + projectConfig: { + ...config.projectConfig, + testEnvironmentOptions: { + ...config.projectConfig?.testEnvironmentOptions, + // Custom error handling via virtualConsole.sendTo + }, + }, + }; + + super(customConfig, context); + } + + async setup() { + await super.setup(); + + // After setup, intercept console.error to filter XMLHttpRequest noise + // This is called by jsdom's VirtualConsole when errors occur + const originalConsoleError = this.global.console.error; + this.global.console.error = (...args) => { + const message = args[0]?.toString() || ''; + const errorType = args[0]?.type || ''; + + // Filter out XMLHttpRequest/AggregateError noise from jsdom + if ( + message.includes('AggregateError') || + message.includes('XMLHttpRequest') || + errorType === 'XMLHttpRequest' + ) { + return; + } + + originalConsoleError.apply(this.global.console, args); + }; + } +} + +module.exports = CustomJSDOMEnvironment;