首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jest抛出错误,"transformIgnorePatterns不工作“

Jest抛出错误,"transformIgnorePatterns不工作“
EN

Stack Overflow用户
提问于 2021-04-20 01:39:20
回答 1查看 204关注 0票数 0

我正在使用Jest测试框架为我的JavaScript/ React JS应用程序编写单元测试。当我运行我的测试时,我得到了以下错误。

代码语言:javascript
复制
  ● Test suite failed to run

    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:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • 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:

    C:\Users\{user}\{project}\app\node_modules\react-date-picker\node_modules\react-calendar\dist\Calendar.css:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){.react-calendar {

错误来自node_modules文件夹中的包,\react-date-picker。

因此,为了解决这个问题,我修改了jest.config.js文件,如下所示。

代码语言:javascript
复制
const esModules = [ 'react-date-picker'].join('|');
module.exports = {
    verbose: true,
    transformIgnorePatterns: [`/node_modules/(?!${esModules})`]
};

这是"npm run unit:test“的命令。

代码语言:javascript
复制
"jest --watchAll --testPathPattern=tests/unit --config jest.config.js",

当我再次运行测试时,我仍然得到相同的错误。我怎么才能修复它?

EN

回答 1

Stack Overflow用户

发布于 2021-04-20 01:53:50

这里有一个非常类似的情况,他通过将"allowJs": true添加到每个lib/apptsconfig.spec.jsoncompilerOptions中(或者添加到根tsconfig.json中),并设置transformIgnorePatterns来修复它。

请参阅来源:https://github.com/nrwl/nx/issues/812

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67166768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档