我正在使用jest
在一个使用类型记录的简单NodeJS应用程序中运行测试。我的测试是抛出一个错误:ReferenceError: structuredClone is not defined
。
我没有得到任何链接错误,代码编译正常。
const variableForValidation = structuredClone(variableForValidationUncloned);
package.json:
"dependencies": {
...
},
"devDependencies": {
"@types/jest": "^29.0.0",
"@types/node": "^18.7.15",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"eslint": "^8.23.0",
"jest": "^28.0.1",
"nodemon": "^2.0.19",
"serverless-plugin-typescript": "^2.1.2",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
}
这个github问题告诉我,这个问题已经解决了:https://github.com/facebook/jest/issues/12628 --或者我误解了?
我见过类似的堆栈问题,但使用Mocha:未定义不识别structuredClone的摩卡
发布于 2022-09-08 06:20:09
我不明白,所以我建立了我自己的全球:
// globals.ts
if(!global.structuredClone){
global.structuredClone = function structuredClone(objectToClone: any) {
const stringified = JSON.stringify(objectToClone);
const parsed = JSON.parse(stringified);
return parsed;
}
}
// entry point of app, eg index.ts:
import './globals.ts'
// ...
我认为这可能是因为我的tsconfig
中的tsconfig
在添加structuredClone之前正在将我的类型记录文件转换成javascript/node版本?
发布于 2022-09-13 14:19:30
structuredClone
为在Node 17中添加。
如果您无法更新,JSON
hack (stringify然后解析)可以工作,但是可能与您相关的有一些短讯:
编辑
我最近了解到了json-stringify安全,这有助于循环问题。
发布于 2022-09-12 22:00:52
https://stackoverflow.com/questions/73607410
复制相似问题