我正在運行測驗
span class="hljs-keyword">import { render } from '@testing-library/react'/span>。
import MyComponent from './././components/MyComponent'/span>;
test('renders MyComponent', () => {
render(<MyComponent /> )。
});
在一個使用d3的React應用組件上,其匯入方式如下:
import React from react';
import * as d3 from 'd3'/span>;
使用命令
react-scripts test
這在匯入d3時產生了一個錯誤:
這在匯入d3時產生了一個錯誤。
Jest遇到了一個意外的token
這通常意味著你試圖匯入一個Jest無法決議的檔案,例如g。它不是純JavaScript。
默認情況下,如果Jest看到一個Babel配置,它將使用它來轉換你的檔案,忽略 "node_modules"。
下面是你可以做的事情。
- 如果你試圖使用ECMAScript Modules,請參閱https://jestjs.io/docs/en/ecmascript-modules了解如何啟用它。
- 要有一些的你的"node_modules"檔案被改造。你可以指定一個自定義的"transformIgnorePatterns" 在你的配置。
- 如果你需要一個自定義的轉換,指定一個"transform"選項在你的配置。
- 如果你只是想模擬你的非JS模塊(如g. 二進制資產),你可以用與的"moduleNameMapper"配置選項將它們存根。
你將在檔案中找到這些配置選項的更多細節和例子。
https://jestjs.io/docs/en/configuration.html
詳細資訊。
/Users/wogsland/Projects/sepia/node_modules/d3/src/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from "d3-array";
^^^^^^
SyntaxError: 未預期的標記'export'
我錯過了什么?該組件在瀏覽器中作業正常,只是在最基本的測驗中失敗。
uj5u.com熱心網友回復:
為了讓你的一些 "node_modules "檔案被轉化,你可以在你的配置中指定一個自定義的 "transformIgnorePatterns"。
根據上述建議,我們可以告訴Jest不要決議node_modules中的ES模塊。
在你的jest.config.js檔案中,你可以添加以下幾行。你可以在陣列中添加任何你想要的ES模塊。
const esModules = ['d3'/span>, 'd3-array', 'other-d3-module-if-need'] 。 join('|') 。
module.exports = {
// ....
transformIgnorePatterns: [`/node_modules/(?!${esModules})``]。
//...。
};
uj5u.com熱心網友回復:
根據Tiep Phan的答案提示,我在package.json檔案中添加了以下內容:
"jest"/span>: {
"transformIgnorePatterns": ["/node_modules/(?!d3|d3-array|internmap|delaunator|robust-redicates)"]
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/320654.html
標籤:
