Pio*_*rek 194 unit-testing jasmine typescript angular
我正在关注angular.io这个教程
正如他们所说,我创建了hero.spec.ts文件来创建单元测试:
import { Hero } from './hero';
describe('Hero', () => {
it('has name', () => {
let hero: Hero = {id: 1, name: 'Super Cat'};
expect(hero.name).toEqual('Super Cat');
});
it('has id', () => {
let hero: Hero = {id: 1, name: 'Super Cat'};
expect(hero.id).toEqual(1);
});
});
Run Code Online (Sandbox Code Playgroud)
单元测试就像一个魅力.问题是:我看到一些错误,在教程中提到:
我们的编辑器和编译器可能会抱怨他们不知道是什么
it,expect因为他们缺少描述Jasmine的打字文件.我们现在可以忽略那些烦人的抱怨,因为它们是无害的.
他们确实忽视了它.即使这些错误是无害的,但当我收到大量错误时,我的输出控制台看起来并不好看.
我得到的例子:
找不到名字'describe'.
找不到名字'它'.
找不到名字'期待'.
我该怎么办才能修复它?
ksh*_*fbd 363
我希望你已安装 -
hero.spec.ts
然后将以下导入放在hero.spec.ts文件的顶部-
hero.spec.ts
它应该解决问题.
Jia*_* Hu 154
使用Typescript@2.0或更高版本,您可以使用以下命令安装类型:
npm install -D @types/jasmine
Run Code Online (Sandbox Code Playgroud)
然后使用以下types选项自动导入类型tsconfig.json:
"types": ["jasmine"],
Run Code Online (Sandbox Code Playgroud)
import {} from 'jasmine';每个spec文件中都不需要此解决方案.
len*_*nny 26
npm install @types/jasmine
Run Code Online (Sandbox Code Playgroud)
正如在一些评论中提到的"types": ["jasmine"]那样,不再需要了,所有@types包都自动包含在编译中(因为我认为是v2.1).
在我看来,最简单的解决方案是排除tsconfig.json中的测试文件,如:
"exclude": [
"node_modules",
"**/*.spec.ts"
]
Run Code Online (Sandbox Code Playgroud)
这适合我.
官方tsconfig文档中的更多信息.
使用Typescript@2.0或更高版本,您可以使用npm install安装类型
npm install --save-dev @types/jasmine
Run Code Online (Sandbox Code Playgroud)
然后使用tsconfig.json中的typeRoots选项自动导入类型.
"typeRoots": [
"node_modules/@types"
],
Run Code Online (Sandbox Code Playgroud)
此解决方案不需要来自'jasmine'的import {}; 在每个spec文件中.
为了让 TypeScript 编译器在编译期间使用所有可见的类型定义,types应该从文件compilerOptions中的字段中完全删除选项tsconfig.json。
当字段中存在一些types条目compilerOptions,但同时jest条目丢失时,就会出现此问题。
因此,为了解决问题,compilerOptions您的字段tscongfig.json应该包含jest在区域中或完全types删除:types
{
"compilerOptions": {
"esModuleInterop": true,
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"types": ["reflect-metadata", "jest"], //<-- add jest or remove completely
"moduleResolution": "node",
"sourceMap": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114514 次 |
| 最近记录: |