Utk*_*nos 7 npm typescript jestjs babeljs
我正在尝试安装 Jest 以与 Babel 和 Typescript 一起使用。我已严格按照此处显示的说明进行操作,但我得到:
错误:Jest:无法解析 TypeScript 配置文件 C:...jest.config.js`
...当我跑步时npm run test。
的内容jest.config.ts是:
export default {
//lots of commented-out stuff in here - nothing at all uncommented
}
Run Code Online (Sandbox Code Playgroud)
这是我所做的具体步骤:
npm init -ynpm -i --save-dev jest总和.js:
function sum(a, b) {
return a + b;
}
module.exports = sum;
Run Code Online (Sandbox Code Playgroud)
总和测试.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Run Code Online (Sandbox Code Playgroud)
test命令到package.json:包.json:
{
"name": "jest-learn",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.17.5",
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"babel-jest": "^27.5.1",
"jest": "^27.5.1"
}
}
Run Code Online (Sandbox Code Playgroud)
此时,如果我运行yarn test,我的测试就会正常运行并通过。问题是当我尝试引入 Babel 和 Typescript 时。步骤继续:
npm -i --dev babel-jest @babel/core @babel/preset-envbabel.config.jsnpm -i --dev @babel/preset-typescript@babel/preset-typescriptbabel.config.jsbabel.config.js:
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
Run Code Online (Sandbox Code Playgroud)
现在,当我运行时,npm run test出现上述错误。我做错了什么?
这对 Jest 文档中的我有用:
jest.config.ts其添加到源目录,内容如下: /** @type {import('jest').Config} */
const config = {
verbose: true,
};
module.exports = config;
Run Code Online (Sandbox Code Playgroud)
tsconfig.json文件中: ...
"include": ["./src/**/*.tsx", "./src/**/*.ts", "src/jest.config.ts"]
Run Code Online (Sandbox Code Playgroud)
jest、ts-jest和ts-node-dev模块: ...
},
"devDependencies": {
"@faker-js/faker": "^7.5.0",
"@types/express": "^4.17.14",
"@types/jest": "^29.0.3",
"@types/uuid": "^8.3.4",
"jest": "^29.0.3",
"nodemon": "^2.0.20",
"ts-jest": "^29.0.2",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.3"
}
Run Code Online (Sandbox Code Playgroud)
jest 转换配置脚本如下所示:
"jest": {
"transform": {
".(ts|tsx)": "ts-jest"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11670 次 |
| 最近记录: |