UNPKG

6.63 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { Component, Directive, Injector, NgModule, OpaqueToken, Pipe, PlatformRef, SchemaMetadata, Type } from '@angular/core';
9import { ComponentFixture } from './component_fixture';
10import { MetadataOverride } from './metadata_override';
11/**
12 * An abstract class for inserting the root test component element in a platform independent way.
13 *
14 * @experimental
15 */
16export declare class TestComponentRenderer {
17 insertRootElement(rootElementId: string): void;
18}
19/**
20 * @experimental
21 */
22export declare const ComponentFixtureAutoDetect: OpaqueToken;
23/**
24 * @experimental
25 */
26export declare const ComponentFixtureNoNgZone: OpaqueToken;
27/**
28 * @experimental
29 */
30export declare type TestModuleMetadata = {
31 providers?: any[];
32 declarations?: any[];
33 imports?: any[];
34 schemas?: Array<SchemaMetadata | any[]>;
35};
36/**
37 * @whatItDoes Configures and initializes environment for unit testing and provides methods for
38 * creating components and services in unit tests.
39 * @description
40 *
41 * TestBed is the primary api for writing unit tests for Angular applications and libraries.
42 *
43 * @stable
44 */
45export declare class TestBed implements Injector {
46 /**
47 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
48 * angular module. These are common to every test in the suite.
49 *
50 * This may only be called once, to set up the common providers for the current test
51 * suite on the current platform. If you absolutely need to change the providers,
52 * first use `resetTestEnvironment`.
53 *
54 * Test modules and platforms for individual platforms are available from
55 * '@angular/<platform_name>/testing'.
56 *
57 * @experimental
58 */
59 static initTestEnvironment(ngModule: Type<any>, platform: PlatformRef): TestBed;
60 /**
61 * Reset the providers for the test injector.
62 *
63 * @experimental
64 */
65 static resetTestEnvironment(): void;
66 static resetTestingModule(): typeof TestBed;
67 /**
68 * Allows overriding default compiler providers and settings
69 * which are defined in test_injector.js
70 */
71 static configureCompiler(config: {
72 providers?: any[];
73 useJit?: boolean;
74 }): typeof TestBed;
75 /**
76 * Allows overriding default providers, directives, pipes, modules of the test injector,
77 * which are defined in test_injector.js
78 */
79 static configureTestingModule(moduleDef: TestModuleMetadata): typeof TestBed;
80 /**
81 * Compile components with a `templateUrl` for the test's NgModule.
82 * It is necessary to call this function
83 * as fetching urls is asynchronous.
84 */
85 static compileComponents(): Promise<any>;
86 static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed;
87 static overrideComponent(component: Type<any>, override: MetadataOverride<Component>): typeof TestBed;
88 static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): typeof TestBed;
89 static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed;
90 static get(token: any, notFoundValue?: any): any;
91 static createComponent<T>(component: Type<T>): ComponentFixture<T>;
92 private _instantiated;
93 private _compiler;
94 private _moduleRef;
95 private _moduleWithComponentFactories;
96 private _compilerOptions;
97 private _moduleOverrides;
98 private _componentOverrides;
99 private _directiveOverrides;
100 private _pipeOverrides;
101 private _providers;
102 private _declarations;
103 private _imports;
104 private _schemas;
105 private _activeFixtures;
106 /**
107 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
108 * angular module. These are common to every test in the suite.
109 *
110 * This may only be called once, to set up the common providers for the current test
111 * suite on the current platform. If you absolutely need to change the providers,
112 * first use `resetTestEnvironment`.
113 *
114 * Test modules and platforms for individual platforms are available from
115 * '@angular/<platform_name>/testing'.
116 *
117 * @experimental
118 */
119 initTestEnvironment(ngModule: Type<any>, platform: PlatformRef): void;
120 /**
121 * Reset the providers for the test injector.
122 *
123 * @experimental
124 */
125 resetTestEnvironment(): void;
126 resetTestingModule(): void;
127 platform: PlatformRef;
128 ngModule: Type<any>;
129 configureCompiler(config: {
130 providers?: any[];
131 useJit?: boolean;
132 }): void;
133 configureTestingModule(moduleDef: TestModuleMetadata): void;
134 compileComponents(): Promise<any>;
135 private _initIfNeeded();
136 private _createCompilerAndModule();
137 private _assertNotInstantiated(methodName, methodDescription);
138 get(token: any, notFoundValue?: any): any;
139 execute(tokens: any[], fn: Function, context?: any): any;
140 overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void;
141 overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void;
142 overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void;
143 overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void;
144 createComponent<T>(component: Type<T>): ComponentFixture<T>;
145}
146/**
147 * @experimental
148 */
149export declare function getTestBed(): TestBed;
150/**
151 * Allows injecting dependencies in `beforeEach()` and `it()`.
152 *
153 * Example:
154 *
155 * ```
156 * beforeEach(inject([Dependency, AClass], (dep, object) => {
157 * // some code that uses `dep` and `object`
158 * // ...
159 * }));
160 *
161 * it('...', inject([AClass], (object) => {
162 * object.doSomething();
163 * expect(...);
164 * })
165 * ```
166 *
167 * Notes:
168 * - inject is currently a function because of some Traceur limitation the syntax should
169 * eventually
170 * becomes `it('...', @Inject (object: AClass, async: AsyncTestCompleter) => { ... });`
171 *
172 * @stable
173 */
174export declare function inject(tokens: any[], fn: Function): () => any;
175/**
176 * @experimental
177 */
178export declare class InjectSetupWrapper {
179 private _moduleDef;
180 constructor(_moduleDef: () => TestModuleMetadata);
181 private _addModule();
182 inject(tokens: any[], fn: Function): () => any;
183}
184/**
185 * @experimental
186 */
187export declare function withModule(moduleDef: TestModuleMetadata): InjectSetupWrapper;
188export declare function withModule(moduleDef: TestModuleMetadata, fn: Function): () => any;