[英]Jest configure typescript without webpack
Well I'm writing some NPM module with typescript, but i'm not using Webpack to compile the scripts. 好吧,我正在使用Typescript编写一些NPM模块,但是我没有使用Webpack来编译脚本。
how should I configure jest to run properly with typescript files? 我应该如何配置jest以使其与打字稿文件一起正常运行?
// 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
}
I got this error: 我收到此错误:
testMatch: /__tests__/ /*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 matches testPathIgnorePatterns: /node_modules/ - 9 matches testMatch: / __ tests __ / /*.js?(x),**/?(*.)+(spec|test).js?(x)-0个匹配项testPathIgnorePatterns:/ node_modules /-9个匹配项
Step 1: Install 步骤1:安装
npm i jest @types/jest ts-jest -D
Explanation: 说明:
Install jest framework (jest) 安装Jest框架(Jest)
Install the types for jest (@types/jest) 安装Jest的类型(@ types / jest)
Install the TypeScript preprocessor for jest (ts-jest) which allows jest to transpile TypeScript on the fly and have source-map support built in. 安装用于Jest的TypeScript预处理器(ts-jest),该预处理器允许jest即时转换TypeScript并内置源映射支持。
Save all of these to your dev dependencies (testing is almost always a npm dev-dependency) 将所有这些保存到您的开发依赖项中(测试几乎始终是npm开发依赖项)
Step 2: Configure Jest 步骤2:配置笑话
Add the following jest.config.js
file to the root of your project: 将以下jest.config.js
文件添加到项目的根目录:
module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
}
Explanation: 说明:
We always recommend having all TypeScript files in a src folder in your project. 我们始终建议您将所有TypeScript文件放在项目的src文件夹中。
We assume this is true and specify this using the roots option. 我们假设这是正确的,并使用roots选项指定。 The transform config just tells jest to use ts-jest for ts / tsx files. 转换配置只是告诉jest将ts-jest用于ts / tsx文件。 jest docs: https://jestjs.io/docs/en/configuration#transform-object-string-string 开玩笑的文档: https : //jestjs.io/docs/en/configuration#transform-object-string-string
refernce : https://basarat.gitbooks.io/typescript/docs/testing/jest.html 推荐人 : https : //basarat.gitbooks.io/typescript/docs/testing/jest.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.