简体   繁体   English

Jest 导入模块,TypeError:无法读取未定义的属性(读取“utf16le”)

[英]Jest import module, TypeError: Cannot read properties of undefined (reading 'utf16le')

I am trying to test a React component with Jest that imports a module called "Siwe", which is sign in with Ethereum.我正在尝试使用 Jest 测试一个 React 组件,该组件导入一个名为“Siwe”的模块,该模块使用 Ethereum 登录。

My Jest config looks like this:我的 Jest 配置如下所示:

module.exports = {
  moduleNameMapper: {
    "module_name_(.*)":
      "<rootDir>/node_modules/apg-js/src/apg-conv-api/transformers.js",
  },
  transform: {
    "\\.[jt]sx?$": "babel-jest",
  },
  testEnvironment: "jsdom",
  transformIgnorePatterns: ['node_modules/(?!@wagmi)/"'],
};

My test code imports a react component, which has the following import:我的测试代码导入了一个反应组件,它具有以下导入:

import { SiweMessage } from "siwe";

If I remove the import, the test runs fine, but leaving the import in I get an error with the following stack trace如果我删除导入,测试运行正常,但保留导入我得到以下堆栈跟踪的错误

TypeError: Cannot read properties of undefined (reading 'utf16le')

  18 | import { signIn } from "next-auth/react";
  19 | import { AnimatePresence, motion } from "framer-motion";
> 20 | import { SiweMessage } from "siwe";
     | ^
  21 |
  22 | type Props = {};
  23 |

  at Object.utf16le [as decode] (node_modules/apg-js/src/apg-conv-api/transformers.js:801:21)
  at decode (node_modules/apg-js/src/apg-conv-api/converter.js:380:27)
  at Object.decode (node_modules/apg-js/src/apg-conv-api/converter.js:391:10)
  at new decode (node_modules/apg-js/src/apg-api/api.js:327:28)
  at Function.generateApi (node_modules/@spruceid/siwe-parser/dist/abnf.js:154:21)
  at Object.generateApi (node_modules/@spruceid/siwe-parser/dist/abnf.js:166:28)
  at Object.require (node_modules/@spruceid/siwe-parser/dist/parsers.js:18:16)
  at Object.require (node_modules/siwe/dist/client.js:37:23)
  at Object.require (node_modules/siwe/dist/siwe.js:17:14)
  at Object.<anonymous> (pages/playground.page.tsx:20:1)
  at Object.<anonymous> (__tests__/playground.test.tsx:2:1)

For th life of me, I cannot figure out what the problem is.对于我的一生,我无法弄清楚问题是什么。

Module node_modules/apg-js/src/apg-conv-api/transformers.js goes through jest's transformer.模块node_modules/apg-js/src/apg-conv-api/transformers.js通过 jest 的转换器。 Jest wraps code in self executed functions that break apg-conv-api/transformers.js because it relies on correct this reference inside the module . Jest 将代码包装在破坏apg-conv-api/transformers.js的自执行函数中,因为它依赖于正确this模块内部引用

But the root problem in incorrect transformIgnorePatterns as I guess you want to ignore transform for all node_modules except @wagmi if yes just replace it to:但是根本问题在于不正确的transformIgnorePatterns ,因为我猜你想忽略除@wagmi之外的所有node_modules 的转换,如果是,只需将其替换为:

{
  ...
  transformIgnorePatterns: ['node_modules/(?!@wagmi/)'],
}

暂无
暂无

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

相关问题 Jest Vuejs 测试 - 类型错误:无法读取未定义的属性(读取“参数”) - Jest Vuejs Test - TypeError: Cannot read properties of undefined (reading 'params') TypeError:无法读取未定义(读取“长度”)JEST 测试的属性 - TypeError: Cannot read properties of undefined (reading 'length') JEST test 笑话:- TypeError:无法读取未定义的属性(读取“params”)。 开玩笑时出错 - Jest:- TypeError: Cannot read properties of undefined (reading 'params'). Error coming in jest 开玩笑:无法读取未定义的属性(读取“0”) - Jest: Cannot read properties of undefined (reading '0') 类型错误:无法读取未定义的属性(读取“createdTimestamp”) - TypeError: Cannot read properties of undefined (reading 'createdTimestamp') TypeError:无法读取未定义的属性(读取“forEach”) - TypeError: Cannot read properties of undefined (reading 'forEach') TypeError:无法读取未定义的属性(读取“学生”) - TypeError: Cannot read properties of undefined (reading 'students') TypeError:无法读取未定义的属性(读取“addEventListener”) - TypeError: Cannot read properties of undefined (reading 'addEventListener') TypeError:无法读取未定义的属性(读取&#39;indexOf&#39;) - TypeError: Cannot read properties of undefined (reading 'indexOf') TypeError:无法读取未定义的属性(读取“国家”) - TypeError: Cannot read properties of undefined (reading 'country')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM