1 加入 jest ts-jest @types/jest
jest 为测试框架
tes-jest 用于转换ts代码
@types/jest 用于vscode中防止报错
yarn add jest ts-jest @types/jest
2 前端目录中, 新建 test目录
是 test 前后各有2个 底杠
image.png3 package.json中加入如下配置
"jest": {
// 把ts 结尾的文件转换成 js 文件
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
// 测试文件代码目录
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
// 测试文件支持的后缀
"moduleFileExtensions": [
"js",
"jsx",
"ts",
"tsx"
],
"moduleDirectories": [
"node_modules"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__jest__/__mocks__/fileMock.js",
"\\.(css|scss)$": "identity-obj-proxy",
"^cpn(.*)$": "<rootDir>/src/components$1"
}
}
网友评论