Jest transformIgnorePatterns无法与外部React NPM软件包一起使用

时间:2020-06-10 14:32:58

标签: reactjs jestjs snapshot

从具有共享React组件的自定义模式npm包中导入React组件后,我遇到以下错误:

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

/Users/mrks/Development/farmer-components/dist/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import DesktopHeader from "./components/DesktopHeader/DesktopHeader";
                                                                                         ^^^^^^

SyntaxError: Cannot use import statement outside a module

   7 | import { sha3_512 } from "js-sha3";
>  9 | import { DesktopHeader, IconQRCode } from "farmer-components";
     | ^
  10 | import { toast } from "react-toastify";
  11 | import "react-toastify/dist/ReactToastify.min.css";
  12 | import i18n from "i18next";

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1172:14)
  at Object.<anonymous> (src/App.tsx:9:1)

我试图用package.json中的以下Jest设置忽略这个新的npm软件包:

"transform": {
  "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
  "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
  "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
  "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx|scss|css)$",
  "^.+\\.module\\.(css|sass|scss|ts|js|tsx)$",
  "node_modules/farmer-components",
  "<rootDir>/node_modules/farmer-components",
  "<rootDir>/node_modules/",
  "/^[^\/]+\/farmer-components\/?(?:[^\/]+\/?)*$/gm"
]

我的Index.test.snap仍然有问题,有人可以帮我吗?

0 个答案:

没有答案