创建一个新的Angular项目后,*.spec.ts文件允许使用Jasmine函数,如"描述"beforeEach";并期待。我可以点击它们,然后转到它们键入的定义。但是,他们在哪里以及如何与该项目挂钩呢?spec.ts文件中没有针对它们的导入子句。
例如logger.service.spec.ts:
import { TestBed } from '@angular/core/testing';
import { LoggerService } from './logger.service';
describe('LoggerService', () => {
let service: LoggerService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(LoggerService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should be return input string', () => {
let retval = service.calculate("hello");
expect(retval).toBe("hello");
});
});
单击describe将转到nodemodules/jasmine/index.d.ts文件:
/**
* Create a group of specs (often called a suite).
* @param description Textual description of the group
* @param specDefinitions Function for Jasmine to invoke that will define inner suites a specs
*/
declare function describe(description: string, specDefinitions: () => void): void;
是什么魔法使这成为可能?
这就是我所想的。
- @types/jasmine类型定义包需要安装
npm install @types/jasmine --save-dev
但这只会带来文件。
- 为了利用类型定义,tsconfig。{xxx}.json应该包括:
"compilerOptions": {
types : [
...,
"jasmine"
]
}