TestBed

class

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

See more...

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

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

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

Parameters

ngModule

Type: Type | Type[].

platform

Type: PlatformRef.

aotSummaries

Type: () => any[].

Optional. Default is undefined.

Returns

TestBed

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.

static resetTestEnvironment()

Parameters

There are no parameters.

static resetTestingModule(): typeof TestBed

Parameters

There are no parameters.

Returns

typeof TestBed

Allows overriding default compiler providers and settings which are defined in test_injector.js

static configureCompiler(config: { providers?: any[]; useJit?: boolean; }): typeof TestBed

Parameters

config

Type: { providers?: any[]; useJit?: boolean; }.

Returns

typeof TestBed

Allows overriding default providers, directives, pipes, modules of the test injector, which are defined in test_injector.js

static configureTestingModule(moduleDef: TestModuleMetadata): typeof TestBed

Parameters

moduleDef

Type: TestModuleMetadata.

Returns

typeof TestBed

Compile components with a templateUrl for the test's NgModule. It is necessary to call this function as fetching urls is asynchronous.

static compileComponents(): Promise<any>

Parameters

There are no parameters.

Returns

Promise<any>

static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed

Parameters

ngModule

Type: Type.

override

Type: MetadataOverride.

Returns

typeof TestBed

static overrideComponent(component: Type<any>, override: MetadataOverride<Component>): typeof TestBed

Parameters

component

Type: Type.

override

Type: MetadataOverride.

Returns

typeof TestBed

static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): typeof TestBed

Parameters

directive

Type: Type.

override

Type: MetadataOverride.

Returns

typeof TestBed

static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed

Parameters

pipe

Type: Type.

override

Type: MetadataOverride.

Returns

typeof TestBed

static overrideTemplate(component: Type<any>, template: string): typeof TestBed

Parameters

component

Type: Type.

template

Type: string.

Returns

typeof TestBed

Overrides the template of the given component, compiling the template in the context of the TestingModule.

static overrideTemplateUsingTestingModule(component: Type<any>, template: string): typeof TestBed

Parameters

component

Type: Type.

template

Type: string.

Returns

typeof TestBed

Note: This works for JIT and AOTed components as well.

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

static overrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): typeof TestBed

Parameters

token

Type: any.

provider

Type: { useFactory: Function; deps: any[]; }.

Returns

typeof TestBed

static overrideProvider(token: any, provider: { useValue: any; }): typeof TestBed

Parameters

token

Type: any.

provider

Type: { useValue: any; }.

Returns

typeof TestBed

Note: This works for JIT and AOTed components as well.

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

static deprecatedOverrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void

Deprecated as it makes all NgModules lazy. Introduced only for migrating off of it.

Parameters

token

Type: any.

provider

Type: { useFactory: Function; deps: any[]; }.

Returns

void

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

Parameters

token

Type: any.

provider

Type: { useValue: any; }.

Returns

void

static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND)

Parameters

token

Type: any.

notFoundValue

Type: any.

Optional. Default is Injector.THROW_IF_NOT_FOUND.

static createComponent<T>(component: Type<T>): ComponentFixture<T>

Parameters

component

Type: Type.

Returns

ComponentFixture<T>

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[])

Parameters

ngModule

Type: Type | Type[].

platform

Type: PlatformRef.

aotSummaries

Type: () => any[].

Optional. Default is undefined.

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()

Parameters

There are no parameters.

resetTestingModule()

Parameters

There are no parameters.

configureCompiler(config: { providers?: any[]; useJit?: boolean; })

Parameters

config

Type: { providers?: any[]; useJit?: boolean; }.

configureTestingModule(moduleDef: TestModuleMetadata)

Parameters

moduleDef

Type: TestModuleMetadata.

compileComponents(): Promise<any>

Parameters

There are no parameters.

Returns

Promise<any>

get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND)

Parameters

token

Type: any.

notFoundValue

Type: any.

Optional. Default is Injector.THROW_IF_NOT_FOUND.

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

Parameters

tokens

Type: any[].

fn

Type: Function.

context

Type: any.

Optional. Default is undefined.

Returns

any

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

Parameters

ngModule

Type: Type.

override

Type: MetadataOverride.

Returns

void

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

Parameters

component

Type: Type.

override

Type: MetadataOverride.

Returns

void

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

Parameters

directive

Type: Type.

override

Type: MetadataOverride.

Returns

void

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

Parameters

pipe

Type: Type.

override

Type: MetadataOverride.

Returns

void

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

overrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void

Parameters

token

Type: any.

provider

Type: { useFactory: Function; deps: any[]; }.

Returns

void

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

Parameters

token

Type: any.

provider

Type: { useValue: any; }.

Returns

void

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

deprecatedOverrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void

Deprecated as it makes all NgModules lazy. Introduced only for migrating off of it.

Parameters

token

Type: any.

provider

Type: { useFactory: Function; deps: any[]; }.

Returns

void

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

Parameters

token

Type: any.

provider

Type: { useValue: any; }.

Returns

void

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

Parameters

component

Type: Type.

template

Type: string.

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

Parameters

component

Type: Type.

Returns

ComponentFixture<T>

© 2010–2019 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v6.angular.io/api/core/testing/TestBed