开玩笑配置没有Webpack的打字稿 [英] Jest configure typescript without webpack
问题描述
好吧,我正在用Typescript编写一些NPM模块,但我没有使用Webpack来编译脚本.
Well I'm writing some NPM module with typescript, but i'm not using Webpack to compile the scripts.
我应该如何配置jest以使其与打字稿文件一起正常运行?
how should I configure jest to run properly with typescript files?
// 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个匹配项
testMatch: /__tests__//*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 matches testPathIgnorePatterns: /node_modules/ - 9 matches
推荐答案
第1步:安装
npm i jest @types/jest ts-jest -D
说明:
-
安装jest框架(jest)
Install jest framework (jest)
安装玩笑的类型(@ types/jest)
Install the types for jest (@types/jest)
安装用于Jest的TypeScript预处理器(ts-jest),这允许 开玩笑地翻译TypeScript并具有源地图支持 内置的.
Install the TypeScript preprocessor for jest (ts-jest) which allows jest to transpile TypeScript on the fly and have source-map support built in.
将所有这些保存到您的开发依赖项中(几乎总是进行测试 npm开发人员依赖性)
Save all of these to your dev dependencies (testing is almost always a npm dev-dependency)
第2步:配置Jest
将以下jest.config.js
文件添加到项目的根目录:
Add the following jest.config.js
file to the root of your project:
module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
}
说明:
-
我们总是建议将所有TypeScript文件放在src文件夹中 您的项目.
We always recommend having all TypeScript files in a src folder in your project.
我们假设这是正确的,并使用roots选项指定.转换配置仅告诉jest将ts-jest用于ts/tsx 文件.开玩笑的文档: https://jestjs.io/docs/zh/configuration #transform-object-string-string
We assume this is true and specify this using the roots option. The transform config just tells jest to use ts-jest for ts / tsx files. jest docs: https://jestjs.io/docs/en/configuration#transform-object-string-string
引荐: https://basarat. gitbooks.io/typescript/docs/testing/jest.html
这篇关于开玩笑配置没有Webpack的打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!