繁体   English   中英

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

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

我正在尝试使用 Jest 测试一个 React 组件,该组件导入一个名为“Siwe”的模块,该模块使用 Ethereum 登录。

我的 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)/"'],
};

我的测试代码导入了一个反应组件,它具有以下导入:

import { SiweMessage } from "siwe";

如果我删除导入,测试运行正常,但保留导入我得到以下堆栈跟踪的错误

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)

对于我的一生,我无法弄清楚问题是什么。

模块node_modules/apg-js/src/apg-conv-api/transformers.js通过 jest 的转换器。 Jest 将代码包装在破坏apg-conv-api/transformers.js的自执行函数中,因为它依赖于正确this模块内部引用

但是根本问题在于不正确的transformIgnorePatterns ,因为我猜你想忽略除@wagmi之外的所有node_modules 的转换,如果是,只需将其替换为:

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

暂无
暂无

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

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