我正在为一些已编写的服务编写单元测试。我的服务和组件均以Typescript编写。 我的理解是它无法解析我的打字稿。 我正在单元测试中导入一项这样的服务。我在开玩笑进行单元测试。 没有导入,测试工作正常
我的
package.json
{
"name": "web",
"version": "0.1.0",
"private": true,
"dependencies": {
"@date-io/core": "^1.3.6",
"@material-ui/core": "^4.0.1",
"@material-ui/icons": "^4.9.1",
"@testing-library/dom": ">=5",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "7.2.1",
"@types/jest": "^24.0.0",
"@types/js-cookie": "^2.2.6",
"@types/jwt-decode": "^2.2.1",
"@types/lodash": "^4.14.150",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-router-dom": "^5.1.4",
"@types/reactstrap": "^8.4.2",
"@types/recharts": "^1.8.9",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
"axios": "^0.19.2",
"bootstrap": "4.4.1",
"clsx": "^1.1.0",
"cross-env": "^7.0.2",
"eslint": "6.8.0",
"eslint-config-airbnb": "18.1.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "2.5.0",
"jquery": "3.0.0",
"js-cookie": "^2.2.1",
"jwt-decode": "^2.2.0",
"lodash": "^4.17.15",
"material-table": "1.57.2",
"moment": "^2.24.0",
"popper.js": "^1.16.0",
"prettier": "^2.0.4",
"prop-types": "^15.7.2",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"reactstrap": "^8.4.1",
"recharts": "^1.8.5",
"typescript": "~3.7.2"
},
"scripts": {
"start": "CI=true react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "tsc --noEmit && eslint \"**/*.{js,jsx,ts,tsx}\" --fix"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {}
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
],
"files": [
"src/types.d.ts"
]
}
我创建的测试
import axios from 'axios';
test("Check if Company Details exists", () => {
expect(false).toBe(false);
});
我收到的问题
此外,很明显,测试套件尚未运行
我尝试了不同的方法,但到目前为止没有任何帮助。
答案 0 :(得分:0)
您是否拥有Jest配置文件(https://jestjs.io/docs/en/configuration)?如果不是,您应该拥有一个,这似乎与:
中的问题相同您应该首先添加一个Jest配置文件(假设您的测试文件名为Component.test.ts):
module.exports = {
rootDir: '..',
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
},
};
答案 1 :(得分:0)
对package.json进行了相应更改,并且效果很好 @Florian的建议实际上给出了一个想法。谢谢队友
"jest": {
"moduleFileExtensions": [
"js",
"jsx",
"tsx",
"ts"
],
"rootDir": "src/tests/",
"transform": {
"^.+\\.(ts|tsx)?$": "ts-jest"
}
},```
答案 2 :(得分:0)
解决我的问题是简单地将“YourFileName.test.js”更改为“YourFileName.test.ts”。 我一直在使用 ts-jest 但使用的是 .js 扩展名...