node.js - 无法从 'index.js' 中找到模块 'react' - (node_modules/jest-resolve/build/index.js:259:17)
问题描述
我的项目创建过程(这是一个create-react-app
打字稿版本模板:
25 npx create-react-app test --template typescript
26 cd test
27 ls
28 npm install --save-dev react-test-renderer
29 npm test
30 npm test
31 node --version
32 npx create-react-app --version
33 npm install antd -g
34 npm test
我Jest
在 package-lock.json 中的版本
"@jest/core": {
"version": "24.9.0",
我的 node.js 版本是
v12.19.0
create-react-app 版本是
npx create-react-app --version
3.4.1
antd版本是
antd@4.6.6
文件:App.test.tsxtesting file
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';
import renderer from 'react-test-renderer';
import MyButton from './components/button';
it('renders a snapshot', () => {
const tree = renderer.create(<App/>).toJSON();
expect(tree).toMatchSnapshot();
});
it('MyButton', () => {
const tree = renderer.create(<MyButton/>).toJSON();
expect(tree).toMatchSnapshot();
});
我的antd
源文件
import React from 'react'
import { Button } from 'antd';
const MyButton = () => {
const [value, setValue] = React.useState('')
// The event type is a "ChangeEvent"
// We pass in "HTMLInputElement" to the input
function onChange(e: React.ChangeEvent<HTMLInputElement>) {
setValue(e.target.value)
}
return <Button></Button>
}
export default MyButton;
当我跑的时候npm test
,我得到了这个
FAIL src/App.test.tsx
● Test suite failed to run
Cannot find module 'react' from 'index.js'
However, Jest was able to find:
'components/button.tsx'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (../../../node_modules/antd/lib/affix/index.js:26:37)
解决方案
推荐阅读
- r - 在 r 中使用 dplyr 将函数应用于每个组的数据帧
- android - 如何正确处理android intent-filter?
- dart - 为什么在这种情况下使用工厂构造函数
- javafx - JavaFX - 我如何知道 GridPane 中的按钮存在于何处?
- ffmpeg - 如何使用 ffmpeg 为视频添加字幕?
- apache-kafka-streams - Kafka Streaming Suppress Feature 以获取延迟超过宽限期的交易
- ruby-on-rails - Rails:Bundler 找不到 gem“activerecord”的兼容版本:
- html - 如何显示多个标题
- python - 如何在 GradientTape 中将“步”传递给 ExponentialDecay
- reactjs - 如何上传图像和一些数据以及如何在 mern 堆栈中格式化日期