要转换一些“node_modules”文件,您可以在配置中指定自定义“transformIgnorePatterns”

Lou*_*ost 16 reactjs jestjs enzyme

我运行 jest 测试文件并收到以下错误:

\n
 \xe2\x97\x8f Test suite failed to run\n\n    Jest encountered an unexpected token\n\n    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.\n\n    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.\n\n    By default "node_modules" folder is ignored by transformers.\n\n    Here's what you can do:\n     \xe2\x80\xa2 If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.\n     \xe2\x80\xa2 If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript\n     \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n     \xe2\x80\xa2 If you need a custom transformation specify a "transform" option in your config.\n     \xe2\x80\xa2 If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.\n\n    You'll find more details and examples of these config options in the docs:\n    https://jestjs.io/docs/configuration\n    For information about custom transformations, see:\n    https://jestjs.io/docs/code-transformation\n\n    Details:\n\n    /Users/test/dev/pm/client/node_modules/spacetime/src/index.js:1\n    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import Spacetime from './spacetime.js'\n                                                                                      ^^^^^^\n\n    SyntaxError: Cannot use import statement outside a module\n\n       6 | import search from '../../assets/images/search.svg';\n       7 | import Dropdown from 'react-bootstrap/Dropdown';\n    >  8 | import spacetime from "spacetime";\n         | ^\n       9 | import languages from "../../libraries/languages/language-list";\n      10 | import { Rating } from 'react-simple-star-rating';\n      11 |\n\n      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)\n      at Object.<anonymous> (src/components/Mentor/MentorList.js:8:1)\n\nTest Suites: 1 failed, 1 total\nTests:       0 total\nSnapshots:   0 total\nTime:        2.097 s\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试通过将以下内容添加到 package.json 来修复它,但这不起作用:

\n
  "jest": {\n    "transformIgnorePatterns": [\n      "node_modules/(?!spacetime)"\n    ]\n  },\n
Run Code Online (Sandbox Code Playgroud)\n

有人可以帮忙吗?

\n

sli*_*wp2 9

spacetime包主文件是ES6模块。看node_modules/spacetime/src/index.js,这就是包导出的主文件。spacetime include的package.json "main": "src/index.js"

默认情况下,变压器会忽略“node_modules”文件夹。

您不需要transformIgnorePatterns为 jest config 添加配置,该node_modules文件夹会被转换器忽略。

但这不是问题。

问题是 jest 默认情况下不解析 es6 import/export语句,即使它使用 babel。

开箱即用的 Jest 支持 Babel,它将用于根据您的 Babel 配置将您的文件转换为有效的 JS。

我们需要改造es6importexport语法。

选项1.添加babel配置

选项 2. 使用ts-jest预设进行玩笑。

jest.config.js:

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'jsdom'
};
Run Code Online (Sandbox Code Playgroud)

选项 3. 使用esbuild-jest

jest.config.js:

module.exports = {
  testEnvironment: 'jsdom',
  transform: {
    '\\.[jt]sx?$': 'esbuild-jest',
  },
};
Run Code Online (Sandbox Code Playgroud)

  • @slideshowp2你完全误解了这个问题。因为 `node_modules` 被忽略,所以在测试中导入的任何包都不会转换其语法(即使父文件按照您的建议使用 babel 进行了转换)。因此,如果 `node_modules` 中的包中有一个 `import` 语句,并且它没有被转换,它当然会抛出 `Cannot use import statements Outside a module`。正如OP所说,“transformIgnorePatterns”应该提供一种方法来指定忽略哪些目录,但它不起作用。 (8认同)
  • 我添加了您指定的配置,但我的 jest.config.js 文件被忽略。请帮忙!谢谢。 (2认同)

Hal*_*yer 5

我不确定这个问题是否仍然相关。我发现您的输入有一个小错误transformIgnorePattern

  "jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!(spacetime)/)"
    ]
  }
Run Code Online (Sandbox Code Playgroud)

请注意 周围的括号spacetime。我正在阅读文档transformIgnorePattern中的部分内容Jest,因此,以新的眼光,只需找出缺少的括号即可。

这是 Jest Docs 的引用

在下面的示例中,foo 和 bar 的排除(也称为否定先行断言)相互抵消:

{
 "transformIgnorePatterns": ["node_modules/(?!foo/)", >"node_modules/(?!bar/)"] // not what you want
}``` 
Run Code Online (Sandbox Code Playgroud)