jest我正在使用打字稿的简单 NodeJS 应用程序中运行测试。我的测试抛出一个错误:ReferenceError: structuredClone is not defined。
我没有收到任何 linter 错误,并且代码可以正常编译。
const variableForValidation = structuredClone(variableForValidationUncloned);
Run Code Online (Sandbox Code Playgroud)
包.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"
}
Run Code Online (Sandbox Code Playgroud)
这个github问题向我表明问题已经解决:https://github.com/facebook/jest/issues/12628 - 或者也许我误解了?
我见过类似的 Stack 问题,但使用 Mocha: mocha not recognize StructuredClone is not Defined
所述结构化的克隆算法是使用的,除其他事物的序列化算法,通过传递窗口之间的数据window.postMessage。它支持递归对象(与 JSON 不同),但不支持 DOM 节点、函数和错误等
我想要的是一种检查给定对象是否可以通过结构化克隆算法序列化的简单方法。我可以递归遍历对象并检查每个属性是 DOM 节点、函数还是错误,但这不是一个完整的答案,我想知道是否有更好的方法。