Jest.js 要转换某些“node_modules”文件,可以在配置中指定自定义的“transformIgnorePatterns

9q78igpj  于 6个月前  发布在  Jest
关注(0)|答案(3)|浏览(94)

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

● Test suite failed to run

    Jest encountered an unexpected token

    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.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/test/dev/pm/client/node_modules/spacetime/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import Spacetime from './spacetime.js'
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

       6 | import search from '../../assets/images/search.svg';
       7 | import Dropdown from 'react-bootstrap/Dropdown';
    >  8 | import spacetime from "spacetime";
         | ^
       9 | import languages from "../../libraries/languages/language-list";
      10 | import { Rating } from 'react-simple-star-rating';
      11 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (src/components/Mentor/MentorList.js:8:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.097 s

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

"jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!spacetime)"
    ]
  },


有人能帮帮忙吗?

cgvd09ve

cgvd09ve1#

spacetime包的主文件是ES6模块。参见node_modules/spacetime/src/index.js,这是包导出的主文件。时空的package.json包括"main": "src/index.js"
默认情况下,transformers会忽略“node_modules”文件夹。
您不需要为jest config添加transformIgnorePatterns配置,transformers会忽略node_modules文件夹。
但这不是问题所在。
问题是jest默认情况下不会解析es6 import/export语句,即使它使用了babel。
开箱即用的Jest支持Babel,它将用于根据Babel配置将文件转换为有效的JS。
我们需要转换es6 importexport的语法。
选项1.添加babel配置
选项2.使用ts-jest预置jest。
jest.config.js

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'jsdom'
};

字符串
选项3.使用esbuild-jest
jest.config.js

module.exports = {
  testEnvironment: 'jsdom',
  transform: {
    '\\.[jt]sx?$': 'esbuild-jest',
  },
};

bogh5gae

bogh5gae2#

我不确定这个问题是否仍然相关。我看到的是你的transformIgnorePattern条目上有一个小错误。

"jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!(spacetime)/)"
    ]
  }

字符串
请注意spacetime周围的括号。我正在阅读Jest文档中的transformIgnorePattern部分,所以有一个新的眼睛,只要发现缺少的括号。
这是一个很好的例子。
在下面的示例中,foo和bar的排除(也称为负先行Assert)相互抵消:

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

n6lpvg4x

n6lpvg4x3#

我发现了一些可能导致这个问题的事情,并导致transformIgnorePatterns似乎不工作:

  • transformIgnorePatterns正则表达式的语法错误。你试图告诉jest忽略(即 * 不 * transform)* 几乎 * node_modules中的所有内容,* 除了 * 问题依赖项,可能还有它的传递依赖项。所以这是一个复杂的正则表达式。我使用了在线正则表达式检查器来正确处理我的正则表达式。
  • 在jest配置中删除transform,忘记包含默认的babel-jest
  • 在jest命令行上运行--config=,所以我修改了错误的文件。这是一个愚蠢的错误,但我犯了这个错误-所以其他人也可能犯这个错误!
  • 没有babel.config.js。它必须是babel.config.js.babelrc不行,即使它应该是等价的。

一旦我修复了所有这些问题,我发现错误四处移动,我不得不在正则表达式中添加一些额外的模块,因为它们是由原始依赖项导入的。

相关问题