On this page
TestBed
class
Configures and initializes environment for unit testing and provides methods for creating components and services in unit tests.
class TestBed implements Injector {
  static initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed
  static resetTestEnvironment()
  static resetTestingModule(): typeof TestBed
  static configureCompiler(config: {...}): typeof TestBed
  static configureTestingModule(moduleDef: TestModuleMetadata): typeof TestBed
  static compileComponents(): Promise<any>
  static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed
  static overrideComponent(component: Type<any>, override: MetadataOverride<Component>): typeof TestBed
  static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): typeof TestBed
  static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed
  static overrideTemplate(component: Type<any>, template: string): typeof TestBed
  static overrideTemplateUsingTestingModule(component: Type<any>, template: string): typeof TestBed
  static overrideProvider(token: any, provider: {...}): typeof TestBed
  static deprecatedOverrideProvider(token: any, provider: {...}): typeof TestBed
  static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND)
  static createComponent<T>(component: Type<T>): ComponentFixture<T>
  platform: PlatformRef
  ngModule: Type<any> | Type<any>[]
  initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[])
  resetTestEnvironment()
  resetTestingModule()
  configureCompiler(config: {...})
  configureTestingModule(moduleDef: TestModuleMetadata)
  compileComponents(): Promise<any>
  get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND)
  execute(tokens: any[], fn: Function, context?: any): any
  overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void
  overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void
  overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void
  overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void
  overrideProvider(token: any, provider: {...}): void
  deprecatedOverrideProvider(token: any, provider: {...}): void
  overrideTemplateUsingTestingModule(component: Type<any>, template: string)
  createComponent<T>(component: Type<T>): ComponentFixture<T>
}
   Description
TestBed is the primary api for writing unit tests for Angular applications and libraries.
Static methods
| 
         
          initTestEnvironment() 
          | 
      ||||||
|---|---|---|---|---|---|---|
Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite.  | 
      ||||||
  | 
      
| ngModule | |
| platform | Type:   | 
          
| aotSummaries | Type:  Optional. Default is   | 
          
Returns
This may only be called once, to set up the common providers for the current test suite on the current platform. If you absolutely need to change the providers, first use resetTestEnvironment.
Test modules and platforms for individual platforms are available from '@angular/<platform_name>/testing'.
| 
         
          resetTestEnvironment() 
          | 
      
|---|
Reset the providers for the test injector.  | 
      
  | 
      
| 
         
          resetTestingModule() 
          | 
      
|---|
  | 
      
| 
         
          configureCompiler() 
          | 
      ||
|---|---|---|
Allows overriding default compiler providers and settings which are defined in test_injector.js  | 
      ||
  | 
      
| config | Type:   | 
          
Returns
typeof TestBed
| 
         
          configureTestingModule() 
          | 
      ||
|---|---|---|
Allows overriding default providers, directives, pipes, modules of the test injector, which are defined in test_injector.js  | 
      ||
  | 
      
| moduleDef | Type:   | 
          
Returns
typeof TestBed
| 
         
          compileComponents() 
          | 
      
|---|
Compile components with a   | 
      
  | 
      
| 
         
          overrideModule() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| ngModule | Type:   | 
          
| override | Type:   | 
          
Returns
typeof TestBed
| 
         
          overrideComponent() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| component | Type:   | 
          
| override | Type:   | 
          
Returns
typeof TestBed
| 
         
          overrideDirective() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| directive | Type:   | 
          
| override | Type:   | 
          
Returns
typeof TestBed
| 
         
          overridePipe() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| pipe | Type:   | 
          
| override | Type:   | 
          
Returns
typeof TestBed
| 
         
          overrideTemplate() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| component | Type:   | 
          
| template | Type:   | 
          
Returns
typeof TestBed
| 
         
          overrideTemplateUsingTestingModule() 
          | 
      ||||
|---|---|---|---|---|
Overrides the template of the given component, compiling the template in the context of the TestingModule.  | 
      ||||
  | 
      
| component | Type:   | 
          
| template | Type:   | 
          
Returns
typeof TestBed
Note: This works for JIT and AOTed components as well.
| 
         
          overrideProvider() 
          | 
      ||||
|---|---|---|---|---|
| 
         Overwrites all providers for the given token with the given provider definition.   | 
      
| token | Type:   | 
          
| provider | Type:   | 
          
Returns
typeof TestBed
static overrideProvider(token: any, provider: { useValue: any; }): typeof TestBed
 Parameters
| token | Type:   | 
          
| provider | Type:   | 
          
Returns
typeof TestBed
Note: This works for JIT and AOTed components as well.
| 
         
          deprecatedOverrideProvider() 
          | 
      ||||
|---|---|---|---|---|
| 
         Overwrites all providers for the given token with the given provider definition. 
         | 
      
| token | Type:   | 
          
| provider | Type:   | 
          
Returns
void
static deprecatedOverrideProvider(token: any, provider: { useValue: any; }): void
 Parameters
| token | Type:   | 
          
| provider | Type:   | 
          
Returns
void
| 
         
          get() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| token | Type:   | 
          
| notFoundValue | Type:  Optional. Default is   | 
          
| 
         
          createComponent() 
          | 
      ||
|---|---|---|
  | 
      
| component | Type:   | 
          
Returns
Properties
| Property | Description | 
|---|---|
platform: PlatformRef | 
       |
ngModule: Type<any> | Type<any>[] | 
       
Methods
| 
         
          initTestEnvironment() 
          | 
      ||||||
|---|---|---|---|---|---|---|
Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite.  | 
      ||||||
  | 
      
| ngModule | |
| platform | Type:   | 
          
| aotSummaries | Type:  Optional. Default is   | 
          
This may only be called once, to set up the common providers for the current test suite on the current platform. If you absolutely need to change the providers, first use resetTestEnvironment.
Test modules and platforms for individual platforms are available from '@angular/<platform_name>/testing'.
| 
         
          resetTestEnvironment() 
          | 
      
|---|
Reset the providers for the test injector.  | 
      
  | 
      
| 
         
          resetTestingModule() 
          | 
      
|---|
  | 
      
| 
         
          configureCompiler() 
          | 
      ||
|---|---|---|
  | 
      
| config | Type:   | 
          
| 
         
          configureTestingModule() 
          | 
      ||
|---|---|---|
  | 
      
| moduleDef | Type:   | 
          
| 
         
          compileComponents() 
          | 
      
|---|
  | 
      
| 
         
          get() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| token | Type:   | 
          
| notFoundValue | Type:  Optional. Default is   | 
          
| 
         
          execute() 
          | 
      ||||||
|---|---|---|---|---|---|---|
  | 
      
| tokens | Type:   | 
          
| fn | Type:   | 
          
| context | Type:  Optional. Default is   | 
          
Returns
any
| 
         
          overrideModule() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| ngModule | Type:   | 
          
| override | Type:   | 
          
Returns
void
| 
         
          overrideComponent() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| component | Type:   | 
          
| override | Type:   | 
          
Returns
void
| 
         
          overrideDirective() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| directive | Type:   | 
          
| override | Type:   | 
          
Returns
void
| 
         
          overridePipe() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| pipe | Type:   | 
          
| override | Type:   | 
          
Returns
void
| 
         
          overrideProvider() 
          | 
      ||||
|---|---|---|---|---|
| 
         Overwrites all providers for the given token with the given provider definition.   | 
      
| token | Type:   | 
          
| provider | Type:   | 
          
Returns
void
overrideProvider(token: any, provider: { useValue: any; }): void
 Parameters
| token | Type:   | 
          
| provider | Type:   | 
          
Returns
void
| 
         
          deprecatedOverrideProvider() 
          | 
      ||||
|---|---|---|---|---|
| 
         Overwrites all providers for the given token with the given provider definition. 
         | 
      
| token | Type:   | 
          
| provider | Type:   | 
          
Returns
void
deprecatedOverrideProvider(token: any, provider: { useValue: any; }): void
 Parameters
| token | Type:   | 
          
| provider | Type:   | 
          
Returns
void
| 
         
          overrideTemplateUsingTestingModule() 
          | 
      ||||
|---|---|---|---|---|
  | 
      
| component | Type:   | 
          
| template | Type:   | 
          
| 
         
          createComponent() 
          | 
      ||
|---|---|---|
  | 
      
| component | Type:   | 
          
Returns
© 2010–2019 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
 https://v6.angular.io/api/core/testing/TestBed