我已经设置了一个 jest.env.js 文件 - 我将其添加到 jest.config.jstestEnvironment: './jest.env.js'
- 处理TextEncoder
and TextDecoder
。但是根据我的配置,我在测试中确实遇到了这个错误(jest@28):
TypeError: Class extends value #<Object> is not a constructor or null
> 7 | module.exports = class CustomTestEnvironment extends NodeEnvironment {
| ^
8 | async setup() {
9 | await super.setup()
10 | if (typeof this.global.TextEncoder === 'undefined') {
我想了解错误本身,当然我想知道如何修复它
如果我添加,我的测试会起作用
global.TextEncoder = require('util').TextEncoder
global.TextDecoder = require('util').TextDecoder
在我的测试文件的开头而不是使用testEnvironment
.
笑话.env.js
const NodeEnvironment = require('jest-environment-node')
module.exports = class CustomTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup()
if (typeof this.global.TextEncoder === 'undefined') {
const { TextEncoder } = require('util')
this.global.TextEncoder = TextEncoder
}
if (typeof this.global.TextDecoder === 'undefined') {
const { TextDecoder } = require('util')
this.global.TextDecoder = TextDecoder
}
}
}
您必须导入TestEnvironment
像这样:
const NodeEnvironment = require("jest-environment-node").TestEnvironment;
文档中提到这里 https://jestjs.io/docs/configuration
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)