Help Angular by taking a 1 minute survey!Go to surveyHome

TestBed

Configures and initializes environment for unit testing and provides methods for creating components and services in unit tests.

See more...

interface TestBed { platform: PlatformRef ngModule: Type<any> | Type<any>[] initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void resetTestEnvironment(): void resetTestingModule(): void configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void configureTestingModule(moduleDef: TestModuleMetadata): void compileComponents(): Promise<any> get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any 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: { useFactory: Function; deps: any[]; }): void deprecatedOverrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void overrideTemplateUsingTestingModule(component: Type<any>, template: string): void createComponent<T>(component: Type<T>): ComponentFixture<T> }
      
      
  1. interface TestBed {
  2. platform: PlatformRef
  3. ngModule: Type<any> | Type<any>[]
  4. initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void
  5. resetTestEnvironment(): void
  6. resetTestingModule(): void
  7. configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void
  8. configureTestingModule(moduleDef: TestModuleMetadata): void
  9. compileComponents(): Promise<any>
  10. get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any
  11. execute(tokens: any[], fn: Function, context?: any): any
  12. overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void
  13. overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void
  14. overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void
  15. overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void
  16. overrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void
  17. deprecatedOverrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void
  18. overrideTemplateUsingTestingModule(component: Type<any>, template: string): void
  19. createComponent<T>(component: Type<T>): ComponentFixture<T>
  20. }

Description

TestBed is the primary api for writing unit tests for Angular applications and libraries.

Note: Use TestBed in tests. It will be set to either TestBedViewEngine or TestBedRender3 according to the compiler used.

Properties

Property Description
platform: PlatformRef
ngModule: Type<any> | Type<any>[]

Methods

Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite.

initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void
      
      initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void
    
Parameters
ngModule Type | Type[]
platform PlatformRef
aotSummaries () => any[]

Optional. Default is undefined.

Returns

void

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'.

Reset the providers for the test injector.

resetTestEnvironment(): void
      
      resetTestEnvironment(): void
    
Parameters

There are no parameters.

Returns

void

resetTestingModule(): void
      
      resetTestingModule(): void
    
Parameters

There are no parameters.

Returns

void

configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void
      
      configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void
    
Parameters
config object
Returns

void

configureTestingModule(moduleDef: TestModuleMetadata): void
      
      configureTestingModule(moduleDef: TestModuleMetadata): void
    
Parameters
moduleDef TestModuleMetadata
Returns

void

compileComponents(): Promise<any>
      
      compileComponents(): Promise<any>
    
Parameters

There are no parameters.

Returns

Promise<any>

get(token: any, notFoundValue?: any): any
      
      get(token: any, notFoundValue?: any): any
    

Deprecated from v8.0.0 use Type or InjectionToken

Parameters
token any
notFoundValue any

Optional. Default is undefined.

Returns

any

execute(tokens: any[], fn: Function, context?: any): any
      
      execute(tokens: any[], fn: Function, context?: any): any
    
Parameters
tokens any[]
fn Function
context any

Optional. Default is undefined.

Returns

any

overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void
      
      overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void
    
Parameters
ngModule Type
override MetadataOverride
Returns

void

overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void
      
      overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void
    
Parameters
component Type
override MetadataOverride
Returns

void

overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void
      
      overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void
    
Parameters
directive Type
override MetadataOverride
Returns

void

overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void
      
      overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void
    
Parameters
pipe Type
override MetadataOverride
Returns

void

Overwrites all providers for the given token with the given provider definition.

overrideProvider(token: any, provider: { useValue: any; }): void
      
      overrideProvider(token: any, provider: { useValue: any; }): void
    
Parameters
token any
provider { useValue: any; }
Returns

void

overrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; }): void
      
      overrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; }): void
    
Parameters
token any
provider object
Returns

void

Overwrites all providers for the given token with the given provider definition.

deprecatedOverrideProvider(token: any, provider: { useValue: any; }): void
      
      deprecatedOverrideProvider(token: any, provider: { useValue: any; }): void
    
Parameters
token any
provider { useValue: any; }
Returns

void

deprecatedOverrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; }): void
      
      deprecatedOverrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; }): void
    
Parameters
token any
provider object
Returns

void

overrideTemplateUsingTestingModule(component: Type<any>, template: string): void
      
      overrideTemplateUsingTestingModule(component: Type<any>, template: string): void
    
Parameters
component Type
template string
Returns

void

createComponent<T>(component: Type<T>): ComponentFixture<T>
      
      createComponent<T>(component: Type<T>): ComponentFixture<T>
    
Parameters
component Type
Returns

ComponentFixture<T>