下記の対応と内容はまあまあ被る。
環境
- Next.js v15
- React v18
- Node v22
- Jest v29
解決方法その1
$ npm install jest-fixed-jsdom --save-dev
jest.config.ts
const config: Config = {
testEnvironment: "jest-fixed-jsdom",
}
解決方法その2
jest-fixed-jsdom/index.js - GitHub を参考に自分で書く。
ファイル名は適当。
jest.config.ts と同じ階層に置く。
CustomJSDOMEnvironment.ts
import JSDOMEnvironment from "jest-environment-jsdom"
export default class CustomJSDOMEnvironment extends JSDOMEnvironment {
constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
super(...args)
this.customExportConditions = args.customExportConditions || ['']
this.global.TextDecoder = TextDecoder
this.global.TextEncoder = TextEncoder
this.global.TextDecoderStream = TextDecoderStream
this.global.TextEncoderStream = TextEncoderStream
this.global.ReadableStream = ReadableStream
}
}
jest.config.ts
const config: Config = {
testEnvironment: "./CustomJSDOMEnvironment.ts",
}
解決方法その3
Vitest に移行する。