gpt4 book ai didi

javascript - "TypeError: cannot read property ' 原型(prototype) ' of undefined"当 Jest 测试包含 c3 图表的 react 组件时

转载 作者:行者123 更新时间:2023-12-04 02:35:41 24 4
gpt4 key购买 nike

我有一个导入 c3 并构建 c3 图表的 react 组件。工作正常,我可以看到c3图表。

我正在尝试使用 Jest 测试我的 react 代码,所以我开始对我的 react 组件进行简单测试,只是对组件的导入和验证组件是否“真实”(不是 null/不是未定义)的测试,但是在尝试运行测试时出现以下错误:

● Test suite failed to run

TypeError: Cannot read property 'prototype' of undefined


at node_modules/c3/c3.js:3578:29
at node_modules/c3/c3.js:4266:5
at node_modules/c3/c3.js:3:83
at Object.<anonymous> (node_modules/c3/c3.js:6:2)
at Object.<anonymous> (src/ChartPanel.tsx:2075:49)
at Object.<anonymous> (src/module.ts:131:25)
at Object.<anonymous> (src/module.test.ts:1:1)

有什么想法吗?我在这里错过了什么?

示例:下面是使用 react 示例应用程序的代码,只是对其进行了修改以包含一个带有简单 c3 图表的自定义组件:

App.js

import React from 'react';
import './App.css';
import { Chart } from './Chart';

function App() {
return (
<div className="App">
<div className="App-link-container">
<a className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer">
Learn React
</a>
</div>
<Chart />
</div>
);
}
export default App;

Chart.js(非常简单的c3图表组件)

import React, { PureComponent } from 'react';
import c3 from 'c3';

export class Chart extends PureComponent {
_chart;

componentDidMount() {
this._renderChart();
}

componentDidUpdate() {
this._renderChart();
}

_renderChart() {
this._chart = c3.generate({
bindto: '#chart',

data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 50, 20, 10, 40, 15, 25]
]
}
});
}

render() {
return <div id="chart">hi</div>;
}
}

测试(只是验证文档上的“Learn React”链接。似乎在 App.js 的 Chart 组件的导入中已经触发了错误)

import React from 'react';
import { render } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
const { getByText } = render(<App />);
const linkElement = getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});

在 package.json 中配置的 Jest 如下

  "jest": {
"roots": [
"<rootDir>/src"
],
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"setupFiles": [
"react-app-polyfill/jsdom"
],
"setupFilesAfterEnv": [
"<rootDir>/src/setupTests.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"testEnvironment": "jest-environment-jsdom-fourteen",
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
"modulePaths": [],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
],
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
]
}

而 setupTests.js 除了下面这行没有什么特别的

import '@testing-library/jest-dom/extend-expect';

最佳答案

有一个类似的问题:https://github.com/thymikee/jest-preset-angular/issues/113

这是因为jsdom没有提供svg相关api的实现。如果你跟随 c3.js 的堆栈跟踪,你会发现它正指向

c3 adding prototype

你只需要在setupTests.js中提供 stub c3需要:

window.SVGPathElement = jest.fn();

旁注:最好将 setupTests.js 放在其他地方,这样它就不会被编译或发布。

关于javascript - "TypeError: cannot read property ' 原型(prototype) ' of undefined"当 Jest 测试包含 c3 图表的 react 组件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61986136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com