簡體   English   中英

custom-cra 為 JEST 編譯 node_modules

[英]customize-cra to compile node_modules for JEST

我正在嘗試在我的 cra 中實現 JEST,但我遇到了一個問題:

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

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

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

我仍然有我的錯誤啟動測試。 如果可能不彈出應用程序,那就太好了。 也許我錯過了一些東西......

謝謝大家!

嘗試將transformIgnorePatterns放在package.json文件中,如下所示,即使純react-scripts沒有自定義任何內容,它也應該可以工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM