Jest transformIgnorePatterns无效

Hea*_*rts 12 javascript reactjs jest babel-jest

我花了很长时间查看有关此问题的其他问题并查看Github上的其他项目,但似乎没有任何答案对我有用.

我正在我的项目中加载第三方库,当运行Jest测试时,我收到错误

export default portalCommunication;
^^^^^^

SyntaxError: Unexpected token export

> 1 | import portalCommunication from 'mathletics-portal-communication-service';
Run Code Online (Sandbox Code Playgroud)

我已尝试以多种方式更新我的Jest配置以使其转换此库但我总是得到相同的错误.

这是我当前的jest.config.js文件:

module.exports = {
    moduleNameMapper: {
        '\\.(css|scss)$': 'identity-obj-proxy',
        '\\.svg$': '<rootDir>/test/mocks/svg-mock.js'
    },
    setupFiles: ['./test/test-setup.js'],
    transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!mathletics-portal-communication-service)'
    ]
};
Run Code Online (Sandbox Code Playgroud)

我还尝试添加transform属性来对这个mathletics-portal-communication-service目录运行babel-jest.

请帮忙!

NJC*_*key 26

在我将 .babelrc 更改为 babel.config.js 之前,transformIgnorePatterns 对我不起作用,如下所示:

module.exports = {
    "presets": [
        "@babel/preset-env"
    ]
};
Run Code Online (Sandbox Code Playgroud)

如本评论所示:https : //github.com/facebook/jest/issues/6229#issuecomment-403539460

  • 你知道“.babelrc”或“babel.config.js”文件是否绝对必要吗?我试图让我的程序只使用“package.json”中的“transformIgnorePatterns”声明 (5认同)
  • 这个回复被低估了!!我已经撞了好几个小时的头了,直到我看到这个!哎呀,这应该在文档中! (4认同)
  • 奇怪的是 .babelrc 不起作用,而 babel.config,js 却起作用。 (2认同)

Roh*_*chi 13

我组织的另一个团队添加了一些通用模块。这没有被转译,所以出现了问题。

我遵循这个: https: //babeljs.io/docs/en/configuration#whats-your-use-case

  1. 将我的转换.babelrcbabel.config.json
  2. 在玩笑配置中添加了以下内容: transformIgnorePatterns: ['/node_modules/(?!(@companyName)/).*/'],

这解决了我的问题。


Hea*_*rts 5

作为现在的解决方法,我已更改我的配置以使用该moduleNameMapper选项为该库加载模拟类。我宁愿使用它,transformIgnorePatterns所以仍然会欣赏任何想法。

新配置:

module.exports = {
    moduleNameMapper: {
        '\\.(css|scss)$': 'identity-obj-proxy',
        '\\.svg$': '<rootDir>/test/mocks/svg-mock.js',
        'mathletics-portal-communication-service': '<rootDir>/test/mocks/mathletics-portal-communication-service-mock.js'
    },
    setupFiles: ['./test/test-setup.js']
};
Run Code Online (Sandbox Code Playgroud)


Raz*_*ton 5

添加尾部斜杠为我解决这个问题:

{
    // ...
    transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!mathletics-portal-communication-service/)'
    ]
};
Run Code Online (Sandbox Code Playgroud)