开玩笑:无法解析 TypeScript 配置文件
我正在尝试安装 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
}
这是我执行的具体步骤:
- init project -
npm init -y
- install Jest -
npm -i --save -dev jest
- 制作一个脚本(sum.js)和一个测试(sum.test.js)文件:
sum.js:
function sum(a, b) {
return a + b;
}
module.exports = sum;
sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
- 将
test
命令添加到package.json
:
package.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"
}
}
此时,如果我运行yarn test
,我的测试就会正常运行并通过。问题是当我尝试引入 Babel 和 Typescript 时。 步骤继续:
- 安装 Babel deps:
npm -i --dev babel-jest @babel/core @babel/preset-env
- create
babel.config.js
- 安装 Typescript:
npm -i --dev @babel/preset-typescript
- 添加
@babel/preset-typescript
到babel.config.js:
babel.config.js:
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
现在,当我运行 npm run test 时,出现上述错误。我做错了什么?
I'm trying to install Jest for use with Babel and Typescript. I've followed the instructions shown here to the letter but I'm getting:
Error: Jest: Failed to parse the TypeScript config file C:...jest.config.js`
...when I run npm run test
.
The contents of jest.config.ts
is:
export default {
//lots of commented-out stuff in here - nothing at all uncommented
}
Here's the exact steps I did:
- init project -
npm init -y
- install Jest -
npm -i --save-dev jest
- make a script (sum.js) and a test (sum.test.js) file:
sum.js:
function sum(a, b) {
return a + b;
}
module.exports = sum;
sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
- add
test
command topackage.json
:
package.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"
}
}
At this point, if I run yarn test
, my test runs and passes just fine. The problem is when I try to introduce Babel and Typescript. Steps continue:
- install Babel deps:
npm -i --dev babel-jest @babel/core @babel/preset-env
- create
babel.config.js
- install Typescript:
npm -i --dev @babel/preset-typescript
- add
@babel/preset-typescript
tobabel.config.js
:
babel.config.js:
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
Now when I run npm run test
I get the above error. What did I do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用 来自 Jest 文档:
jest.config.ts
到您的源目录,内容如下:tsconfig.json
文件中:jest ,
ts-jest
和ts-node-dev
模块:并且 jest 转换配置脚本如下所示:
This worked for me from Jest docs:
jest.config.ts
to your source directory with content like:tsconfig.json
file:jest
,ts-jest
, andts-node-dev
modules:and that the jest transform configuration script looks like:
我只需安装 TS Node 就解决了这个问题。
This issue was fixed for me by just installing TS Node.