我从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);
  });
});

单元测试工作起来很有魅力。问题是:我看到了一些错误,在教程中提到过:

我们的编辑和编译器可能会抱怨他们不知道它是什么 和期望,因为他们缺乏打字文件描述 茉莉花。我们可以暂时忽略那些恼人的抱怨 无害的。

他们确实忽略了这一点。尽管这些错误是无害的,但当我收到一堆错误时,在输出控制台中看起来并不好。

我得到的例子是:

无法找到名称“describe”。 找不到名字“it”。 找不到名称“expect”。

我能做些什么来弥补呢?


当前回答

使用Typescript@2.0或更高版本,您可以安装类型:

npm install -D @types/jasmine

然后使用tsconfig.json中的types选项自动导入类型:

"types": ["jasmine"],

这个解决方案不需要import {} from 'jasmine';在每个规范文件中。

其他回答

在Typescript@2.0或更高版本中,你可以使用npm install来安装类型

npm install --save-dev @types/jasmine

然后使用tsconfig.json中的typeRoots选项自动导入类型。

"typeRoots": [
      "node_modules/@types"
    ],

这个解决方案不需要import {} from 'jasmine';在每个规范文件中。

我希望你已经安装了

npm install --save-dev @types/jasmine

然后把下面的导入放在hero.spec.ts文件的顶部

import 'jasmine';

这应该能解决问题。

只需添加到您的tsconfig。Json,确保没有"**/*.spec.ts" 在排除

  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]

我的工作tsconfig.json

只需要做以下事情来在Lerna Mono-repo中获取@types 其中存在几个node_module。

npm install -D @types/jasmine

然后在每个tsconfig中。每个模块或应用程序的文件

"typeRoots": [
  "node_modules/@types",
  "../../node_modules/@types" <-- I added this line
],

您需要为jasmine安装类型。假设你使用的是相对较新的typescript 2版本,你应该能够做到:

npm install --save-dev @types/jasmine