繁体   English   中英

未定义 Jest 测试中的 FileReader、File 和 TextDecoder

[英]FileReader, File and TextDecoder in Jest Test not defined

在我只用 Jasmine 进行测试之前,我现在尝试 Jest,但面临的问题是我的测试中没有定义FileReader, File and TextDecoder的东西。 我如何使用这些类和他们真正的 function 来测试我使用它们的 class?

至少对于TextDecoder ,我在@josephting 的评论的帮助下找到了解决方法。 这真的很hacky,因为它需要您安装一个也已弃用且不再维护的开发依赖项,称为text-encoding

首先,您需要在jest.config.js setupFiles

[...]
setupFiles: ['./tests/setupEnv.js'],
[...]

而且您还需要在setupEnv.js中全局要求提到的包:

TextDecoder = require('text-encoding').TextDecoder;

我找到了解决这个问题的方法,我开玩笑地添加了 TextEncoder 和 TextDecoder 作为全局变量。

我将此添加到我的jest.config

const { TextDecoder, TextEncoder } = require('util')

module.exports = {
  globals: {
    TextDecoder: TextDecoder,
    TextEncoder: TextEncoder,
  }
}

这对我有用,谢谢! 亚瑟·梅代罗斯

  const { TextDecoder, TextEncoder } = require('util')
    
    module.exports = {
      globals: {
        TextDecoder: TextDecoder,
        TextEncoder: TextEncoder,
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM