Why am I getting “TextEncoder is not defined“ in Jest?
·
题意:为什么在Jest中会出现“TextEncoder is not defined”错误
问题背景:
When testing a function that uses either the TextEncoder or the TextDecoder I get:
当测试一个使用TextEncoder或TextDecoder的函数时,我遇到了
ReferenceError: TextEncoder is not defined
ReferenceError: TextDecoder is not defined
I am using jsdom, so why is this not working?
我正在使用jsdom,为什么这不起作用?
问题解决:
While it should be bundled with jsdom, it isn't with jsdom 16. Therefore you can polyfill like so:
虽然它应该与jsdom捆绑在一起,但在jsdom 16中并没有。因此,您可以像这样进行polyfill:
import { TextEncoder, TextDecoder } from 'util';
Object.assign(global, { TextDecoder, TextEncoder });
You will have to add that to the test or to a setupFile like setup.jest.ts.
您需要将其添加到测试中,或添加到像 setup.jest.ts 这样的 setupFile 中。

更多推荐
所有评论(0)