Jestjs 测试套件无法运行。找不到模块';反应dom';从';reactBatchedUpdate.js';
我正在尝试开始在我的CreateReact应用程序项目上运行测试。我一直收到这样的错误“无法从ReactBatchedUpdate.js中找到模块ReactDOM” 我试图通过将此代码粘贴到package.json中来编辑它Jestjs 测试套件无法运行。找不到模块';反应dom';从';reactBatchedUpdate.js';,jestjs,create-react-app,react-testing-library,Jestjs,Create React App,React Testing Library,我正在尝试开始在我的CreateReact应用程序项目上运行测试。我一直收到这样的错误“无法从ReactBatchedUpdate.js中找到模块ReactDOM” 我试图通过将此代码粘贴到package.json中来编辑它 "jest": { "collectCoverageFrom": [ "src/**/*.{js,jsx,ts,tsx}", "!<rootDir>/node_modules/", "!<rootDir>
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!<rootDir>/node_modules/",
"!<rootDir>/path/to/dir/"
],
"coverageThreshold": {
"global": {
"branches": 90,
"functions": 90,
"lines": 90,
"statements": 90
}
},
"coverageReporters": ["text"],
"snapshotSerializers": ["my-serializer-module"]
}
你能添加你的测试代码吗?@GiorgioPolvara Gpx我添加了我的测试代码。我开始意识到这是由于redux的实现,我的redux测试设置可能不正确。我试图遵循我在上面添加的SO链接,但我似乎仍然有问题。请确定,如果只渲染一个空的
div
,测试是否通过?
const Header = ({clicked, open}) => {
return (
<div className="Header__Wrapper">
<DrawerToggleButton clicked={clicked} open={open} />
<SubHeader subHeadClass={'Header__Left'} urls={URLS.slice(0, 2)} />
<div className="Logo">
<Lock />
</div>
<SubHeader subHeadClass={'Header__Right'} urls={URLS.slice(2)} />
<LogInButton className="right LogInButton__Wrapper"/>
</div>
);
};
export default Header;
import React from 'react'
import '@testing-library/jest-dom/extend-expect'
import { render } from '@testing-library/react'
import { createStore } from 'redux'
import {NAVIGATE_PAGES, SAVE_ROUTE} from '../../../../actions/types';
import SubHeader from '../SubHeader';
import Navigate from '../../../../actions/index';
const INITIAL_STATE = {
page: null,
route: null,
};
function reducer(state = INITIAL_STATE, action){
switch (action.type) {
case NAVIGATE_PAGES: {
return {...state, page: action.payload};
}
case SAVE_ROUTE: {
return {...state, route: action.payload};
}
default:
return state;
}
};
function renderWithRouter(
ui,
{
route = '/',
history = createMemoryHistory({ initialEntries: [route] }),
initialState, store = createStore(reducer, initialState) ,
} = {}
) {
return {
...render(<Provider store={store}><Router history={history}>{ui}</Router></Provider>),
// adding `history` to the returned utilities to allow us
// to reference it in our tests (just try to avoid using
// this to test implementation details).
history,
}
}
test('it renders lock', async () => {
const store = createStore(() => ({ reducer: reducer ,initialState:INITIAL_STATE}))
renderWithRouter(<SubHeader/>,{store,});
})
Cannot find module 'react-dom' from 'reactBatchedUpdates.js'
However, Jest was able to find:
'utils/reactBatchedUpdates.js'
'utils/reactBatchedUpdates.native.js'
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'].
However, Jest was able to find:
'../Login/LogInButton.js'