首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >customize-cra为JEST编译node_modules

customize-cra为JEST编译node_modules
EN

Stack Overflow用户
提问于 2020-09-03 19:53:24
回答 1查看 292关注 0票数 0

我正试图在我的cra中实现JEST,但我面临着一个问题:

代码语言:javascript
复制
FAIL  src/App.test.js
  ● 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:
     • 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:

    /path/to/project/node_modules/@fullcalendar/react/dist/main.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { __assign, __extends } from "tslib";
                                                                                                    ^

    SyntaxError: Unexpected token {

      2 | import PropTypes from 'prop-types';
      3 | import { useUpdate, useDelete, useNotify, useRedirect, useDataProvider } from 'react-admin';
    > 4 | import FullCalendar from '@fullcalendar/react';

因此,JEST似乎不支持Ecmascript,而fullcalendar则是在ES6中。我找到了一个名为customize-cra的库,它似乎可以完成这项工作,但它并不像我预期的那样工作。

这是我的文件config-overrides.js

代码语言:javascript
复制
const { override, babelInclude } = require('customize-cra');
const path = require('path');

module.exports = override(
  babelInclude([path.resolve('src'), path.resolve('node_modules/@fullcalendar')]),
);

我仍然有我的错误启动测试。如果可以不弹出应用程序,那就太好了。也许我错过了什么..。

谢谢大家!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-04 00:39:54

尝试在你的package.json文件中放置transformIgnorePatterns,如下所示,即使是纯react-scripts,它也应该可以工作,而不需要自定义任何东西:

代码语言:javascript
复制
"jest": {
    "transformIgnorePatterns": [
      "/!node_modules\\/@fullcalendar/"
    ]
  },
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63723097

复制
相关文章

相似问题

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