[英]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.