customize-cra为JEST编译node_modules [英] customize-cra to compile node_modules for JEST

查看:122
本文介绍了customize-cra为JEST编译node_modules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的cra中实现JEST,但是我遇到了一个问题:

I'm trying to implement JEST in my cra but I'm facing a problem :

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,而ES6中则支持fullcalendar. 我找到了一个名为customize-cra的库,该库似乎可以完成任务,但是它没有按预期运行.

So it seems that JEST is not supporting Ecmascript and fullcalendar is in ES6. I found a lib called customize-cra that seems to do the job, but it does not works as I expected.

这是我的文件config-overrides.js

const { override, babelInclude } = require('customize-cra');
const path = require('path');

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

我仍然有我的错误启动测试.如果可能不退出应用程序,那将是非常不错的. 也许我想念一些东西...

And I still have my error launching test. If possible to not eject app it would be really great. Maybe I'm missing something...

谢谢!

推荐答案

也尝试将transformIgnorePatterns放在您的package.json文件中,如下所示,即使是纯react-scripts且未自定义任何内容,它也应能正常工作:

Try to place transformIgnorePatterns in your package.json file too, as following, it's supposed to be working even if pure react-scripts without customizing anything:

"jest": {
    "transformIgnorePatterns": [
      "/!node_modules\\/@fullcalendar/"
    ]
  },

这篇关于customize-cra为JEST编译node_modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆