这是我第一次运行单元测试。尝试执行笑话测试时出现以下错误
D:\..\src\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Alert from './alert'
^^^^^^
SyntaxError: Cannot use import statement outside a module
6 | import NavbarUser from "./NavbarUser"
7 | import userImg from "../../../assets/img/portrait/small/avatar-s-11.jpg"
> 8 | import {Button} from "bootstrap/js/src";
| ^
9 | import {useTranslation} from "react-i18next";
10 |
11 |
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (src/layouts/components/navbar/Navbar.js:8:1)
这是我的单元测试
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
it('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<App />, div)
ReactDOM.unmountComponentAtNode(div)
})
第二个单元测试,它使用酶来渲染类组件并期望内部有许多组件
import { configure , shallow } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import NewVehicle from "./NewVehicle";
import React from "react";
import Form from "../../../components/form";
configure({adapter : new Adapter})
describe('AddCar' , ()=>{
it('should render a proper component list', function () {
const wrapper = shallow(<NewVehicle/>)
expect(wrapper.find(Form)).toHaveLength(27)
});
})
我尝试添加babel config。这是我的配置
module.exports = function (api) {
api.cache(true)
const presets = [
"@babel/preset-env"
]
return {
presets
}
}
并在package.json中添加“ type”:“ module”给我一个不同的错误
internal/modules/cjs/loader.js:1153
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\..
\config-overrides.js
require() of ES modules is not supported.
require() of D:\..\config-overrides.js from D:\..Frontend\node_modules\react-app-rewired\config-overrides.js is an ES module file as it is
a .js file whose nearest parent package.json contains "type": "module" which defines all .js files
in that package scope as ES modules.
Instead rename D:\..\config-overrides.js to end in .cjs, change t
he requiring code to use import(), or remove "type": "module" from D:\..\F
rontend\package.json.