【问题标题】:Jest configure typescript without webpackJest 在没有 webpack 的情况下配置 typescript
【发布时间】:2019-10-31 00:50:38
【问题描述】:

好吧,我正在用 typescript 编写一些 NPM 模块,但我没有使用 Webpack 来编译脚本。

我应该如何配置 jest 以使用 typescript 文件正常运行?

// test.spec.ts
import {calc} from './index'

test('shoud...', () => {
  expect(calc()).toBeDefined()
})
// index.ts
import {calc} from './index'

const calc = (a, b) => {
  return a + b
}

我收到了这个错误:

testMatch: /__tests__//*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 个匹配项 testPathIgnorePatterns: /node_modules/ - 9 个匹配项

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    第 1 步:安装

    npm i jest @types/jest ts-jest -D
    

    说明:

    • 安装 jest 框架(jest)

    • 为 jest 安装类型 (@types/jest)

    • 为 jest (ts-jest) 安装 TypeScript 预处理器,它允许 开玩笑地即时转换 TypeScript 并支持源映射 内置。

    • 将所有这些保存到您的开发依赖项中(测试几乎总是 npm 开发依赖)

    第 2 步:配置 Jest

    将以下jest.config.js 文件添加到项目的根目录:

    module.exports = {
      "roots": [
        "<rootDir>/src"
      ],
      "transform": {
        "^.+\\.tsx?$": "ts-jest"
      },
    }
    

    说明:

    参考https://basarat.gitbooks.io/typescript/docs/testing/jest.html

    【讨论】:

      猜你喜欢
      • 2018-10-01
      • 2017-06-24
      • 1970-01-01
      • 2023-03-08
      • 2017-12-02
      • 2023-03-17
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      相关资源
      最近更新 更多