开玩笑:无法解析 TypeScript 配置文件

发布于 2025-01-10 14:07:24 字数 1979 浏览 0 评论 0原文

我正在尝试安装 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 to 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"
  }
}

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 to babel.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

避讳 2025-01-17 14:07:24

这对我有用 来自 Jest 文档

  • 创建并添加文件 jest.config.ts 到您的源目录,内容如下:
    /** @type {import('jest').Config} */
        const config = {
            verbose: true,
        };
      
    module.exports = config;
  • 然后,确保将该文件包含在您的 tsconfig.json 文件中:
    ...
    "include": ["./src/**/*.tsx", "./src/**/*.ts", "src/jest.config.ts"]
  • 在您的 packages.json 中:同时确保您已安装 jest , ts-jestts-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"
      }

并且 jest 转换配置脚本如下所示:

"jest": {
    "transform": {
      ".(ts|tsx)": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ]
  },

This worked for me from Jest docs:

  • create and add file jest.config.ts to your source directory with content like:
    /** @type {import('jest').Config} */
        const config = {
            verbose: true,
        };
      
    module.exports = config;
  • Then, make sure to include the file in your tsconfig.json file:
    ...
    "include": ["./src/**/*.tsx", "./src/**/*.ts", "src/jest.config.ts"]
  • in your packages.json: Ensure also that you have installed jest, ts-jest, and ts-node-dev modules:
    ...
    },
      "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"
      }

and that the jest transform configuration script looks like:

"jest": {
    "transform": {
      ".(ts|tsx)": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)
quot;,
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ]
  },
晚风撩人 2025-01-17 14:07:24

我只需安装 TS Node 就解决了这个问题。

This issue was fixed for me by just installing TS Node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文