你好,我是react和typescript的新手。我创建了一个应用程序react-create-app
并添加了typescript。完成应用module后,我想做一些单元测试,我已经安装了 mocha 和 chai: npm install --save-dev mocha chai
.
在我的 pakacge.json 中,我已将测试脚本更新为"test": "mocha src/test/*"
. 在我的测试文件中使用导入后
import {assert} from 'chai'
import {File} from "../components/File"
我遇到了一个问题
import {assert} from 'chai'
^^^^^^
SyntaxError: Cannot use import statement outside a module
我寻觅了很多,并试图一吨像回答的这一个,但仍然会碰到同样的问题-_-我很困惑我是否需要安装通天塔?这是我的package.json
任何解释
{
...
"dependencies": {
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.8.3",
"@types/classnames": "^2.2.11",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.37",
"@types/react": "^17.0.3",
"@types/react-dom": "^17.0.3",
"classnames": "^2.2.6",
"node-sass": "^5.0.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.3",
"typescript": "^4.2.3",
"web-vitals": "^1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "cross-env TS_NODE_PROJECT=\"tsconfig.testing.json\" mocha src/test/*",
"eject": "react-scripts eject"
},
...
"devDependencies": {
"@types/chai": "^4.2.16",
"@types/mocha": "^8.2.2",
"chai": "^4.3.4",
"cross-env": "^7.0.3",
"mocha": "^8.3.2"
}
}
这是我的 tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
我怎么能正确设置一切?