par*_*cer 3 javascript node.js jsdom
我有这两个文件:
测试.js:
const jsdom = require("jsdom");
const hello = () => {
console.log("hello!");
};
module.exports = {
hello
};
Run Code Online (Sandbox Code Playgroud)
和
服务器.js:
const hello = require('./stuff/test');
hello.hello();
Run Code Online (Sandbox Code Playgroud)
目录结构:
myprojectfolder
backend
src
stuff
test.js
server.js
Run Code Online (Sandbox Code Playgroud)
当我运行时,server.js
我收到ReferenceError: TextEncoder is not defined
错误:
/home/myusername/projects/myprojectfolder/node_modules/whatwg-url/lib/encoding.js:2
const utf8Encoder = new TextEncoder();
^
ReferenceError: TextEncoder is not defined
Run Code Online (Sandbox Code Playgroud)
如果我const jsdom = require("jsdom");
从 中删除行test.js
,server.js
则运行正常并且没有任何错误(输出hello
)。
为什么会发生这种情况以及如何修复它(同时仍然能够jsdom
在内部导入和使用test.js
?)。
小智 7
好吧,我花了一整天的时间,但最后我终于把它整合起来了。我的问题是由于该错误,测试未运行。
因此,在“scripts”下的 package.json 中,我添加了以下内容:
"test": "jest --verbose --runInBand --detectOpenHandles --forceExit"
Run Code Online (Sandbox Code Playgroud)
在 jest.config.js 中,我添加了以下内容:
globals: {
"ts-jest": {
tsConfigFile: "tsconfig.json"
},
TextEncoder: require("util").TextEncoder,
TextDecoder: require("util").TextDecoder
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9280 次 |
最近记录: |