我如何在2019年10月建立Angular with Jest?
作为一名有棱角的开发者,我想摆脱Karma/Jasmine而使用Jest,这样我就可以快速、毫不费力地测试我的应用程序。 不幸的是,我遇到了意想不到的问题 在过去的几个小时里,我浏览了所有出现在SERP顶部的教程:我如何在2019年10月建立Angular with Jest?,angular,typescript,unit-testing,jestjs,Angular,Typescript,Unit Testing,Jestjs,作为一名有棱角的开发者,我想摆脱Karma/Jasmine而使用Jest,这样我就可以快速、毫不费力地测试我的应用程序。 不幸的是,我遇到了意想不到的问题 在过去的几个小时里,我浏览了所有出现在SERP顶部的教程: (由jestjs.io推荐) 以下是我所做的详细说明: 使用ng new my application创建新的角度应用程序 更改目录:cd我的应用程序 运行npm start(ng-serve)和npm-test(ng-test)以查看是否一切正常-一切正常 使用npm安装
- (由jestjs.io推荐)
ng new my application
创建新的角度应用程序cd我的应用程序
npm start
(ng-serve
)和npm-test
(ng-test
)以查看是否一切正常-一切正常package.json
:src
中创建一个包含以下内容的文件setupJest.ts
:当我现在运行
npm测试时
File not found: <rootDir>/tsconfig.spec.json
未找到文件:/tsconfig.spec.json
我不知道我能做些什么,因为上面的所有教程似乎都不能解决这个问题。请帮帮我 提前谢谢你 你为什么不去试试布里格布呢。我在过去几天(2019年11月)刚刚完成了这项工作,效果良好。您只需在VS代码中运行以下内容,原理图就能处理所有问题:
ng add @briebug/jest-schematic
它删除了大部分jasmine,但如果您运行此命令,它将卸载几乎所有与jasmine相关的内容。我没有卸载jasmine弹珠,因为我正在移动到jest弹珠的过程中,但如果有任何不包括的东西,只需将其附加到列表中
我相信它也遗漏了从我的tsconfig.spec.json文件中的类型中删除jasmine,因此我将其更新为以下内容:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"module": "commonjs",
"types": ["jest", "node"],
"emitDecoratorMetadata": true,
"allowJs": true
},
"files": ["polyfills.ts"],
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
我还必须更新package.json中新插入的jest部分的globals部分,并在“/src/**/src/**tsConfig.spec.json”中添加tsConfig值,因为生成的值没有指向我的/src目录
"globals": {
"ts-jest": {
"tsConfig": "<rootDir>/src/tsconfig.spec.json",
"stringifyContentPathRegex": "\\.html$",
"astTransformers": [
"jest-preset-angular/build/InlineFilesTransformer",
"jest-preset-angular/build/StripStylesTransformer"
]
}
“全局”:{
“开玩笑”:{
“tsConfig”:“/src/tsConfig.spec.json”,
“stringifyContentPathRegex”:“\\.html$”,
“AST变压器”:[
“jest预设角度/构建/内联文件转换器”,
“jest预设角度/构建/条带样式转换”
]
}
这对我很有效,非常感谢!我只是想补充一点,在执行ng add@briebug/jest schematic
后,我可以使用npm run test
运行jest测试,但不能使用ng test
运行jest测试,因为它仍然在寻找一些找不到的因果报应内容。看起来jest schematic没有适应他创建了angular.json
文件。我注意到,我们的依赖项中有@briebug/jest schematic
。我们已经在使用jest。你认为我可以删除该依赖项吗?
ng add @briebug/jest-schematic
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"module": "commonjs",
"types": ["jest", "node"],
"emitDecoratorMetadata": true,
"allowJs": true
},
"files": ["polyfills.ts"],
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
"globals": {
"ts-jest": {
"tsConfig": "<rootDir>/src/tsconfig.spec.json",
"stringifyContentPathRegex": "\\.html$",
"astTransformers": [
"jest-preset-angular/build/InlineFilesTransformer",
"jest-preset-angular/build/StripStylesTransformer"
]
}