好吧,我正在用 typescript 编写一些 NPM 模块,但我没有使用 Webpack 来编译脚本。
我应该如何配置 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
}
我收到这个错误:
测试匹配:/__测试__//*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 个匹配
testPathIgnorePatterns:/node_modules/ - 9 个匹配
第 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"
},
}
解释:
我们始终建议将所有 TypeScript 文件放在 src 文件夹中
你的项目。
我们假设这是真的,并使用 root 选项指定这一点。转换配置只是告诉 jest 对 ts / tsx 使用 ts-jest
文件。笑话文档: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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)