Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(core): Add TestBed.runInInjectionContext to help test function…
…s which use `inject`

This commit adds `TestBed.runInInjectionContext` which is equivalent to
`TestBed.inject(EnvironmentInjector).runInContext`. This function will
help make tests for functions which call `inject` from `@angular/core` a
little bit less verbose.
  • Loading branch information
atscott committed Nov 17, 2022
commit 3f45e2d027c322c365e3024fdab04e032931e1a2
1 change: 1 addition & 0 deletions goldens/public-api/core/testing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export interface TestBed {
resetTestEnvironment(): void;
// (undocumented)
resetTestingModule(): TestBed;
runInInjectionContext<T>(fn: () => T): T;
}

// @public
Expand Down
16 changes: 15 additions & 1 deletion packages/core/test/test_bed_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {APP_INITIALIZER, ChangeDetectorRef, Compiler, Component, Directive, ElementRef, ErrorHandler, getNgModuleById, Inject, Injectable, InjectFlags, InjectionToken, InjectOptions, Injector, Input, LOCALE_ID, ModuleWithProviders, NgModule, Optional, Pipe, Type, ViewChild, ɵsetClassMetadata as setClassMetadata, ɵɵdefineComponent as defineComponent, ɵɵdefineInjector as defineInjector, ɵɵdefineNgModule as defineNgModule, ɵɵelementEnd as elementEnd, ɵɵelementStart as elementStart, ɵɵsetNgModuleScope as setNgModuleScope, ɵɵtext as text} from '@angular/core';
import {APP_INITIALIZER, ChangeDetectorRef, Compiler, Component, Directive, ElementRef, ErrorHandler, getNgModuleById, inject, Inject, Injectable, InjectFlags, InjectionToken, InjectOptions, Injector, Input, LOCALE_ID, ModuleWithProviders, NgModule, Optional, Pipe, Type, ViewChild, ɵsetClassMetadata as setClassMetadata, ɵɵdefineComponent as defineComponent, ɵɵdefineInjector as defineInjector, ɵɵdefineNgModule as defineNgModule, ɵɵelementEnd as elementEnd, ɵɵelementStart as elementStart, ɵɵsetNgModuleScope as setNgModuleScope, ɵɵtext as text} from '@angular/core';
import {TestBed, TestBedImpl} from '@angular/core/testing/src/test_bed';
import {By} from '@angular/platform-browser';
import {expect} from '@angular/platform-browser/testing/src/matchers';
Expand Down Expand Up @@ -1893,6 +1893,20 @@ describe('TestBed', () => {
});
});
});

it('should be able to call Testbed.runInInjectionContext in tests', () => {
const expectedValue = 'testValue';
@Injectable({providedIn: 'root'})
class SomeInjectable {
readonly instanceValue = expectedValue;
}

function functionThatUsesInject(): string {
return inject(SomeInjectable).instanceValue;
}

expect(TestBed.runInInjectionContext(functionThatUsesInject)).toEqual(expectedValue);
});
});


Expand Down
21 changes: 21 additions & 0 deletions packages/core/testing/src/test_bed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import {
Component,
Directive,
EnvironmentInjector,
InjectFlags,
InjectionToken,
InjectOptions,
Expand Down Expand Up @@ -104,6 +105,13 @@ export interface TestBed {
/** @deprecated from v9.0.0 use TestBed.inject */
get(token: any, notFoundValue?: any): any;

/**
* Runs the given function in the `EnvironmentInjector` context of `TestBed`.
*
* @see EnvironmentInjector#runInContext
*/
runInInjectionContext<T>(fn: () => T): T;

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

overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): TestBed;
Expand Down Expand Up @@ -325,6 +333,15 @@ export class TestBedImpl implements TestBed {
return TestBedImpl.INSTANCE.inject(token, notFoundValue, flags);
}

/**
* Runs the given function in the `EnvironmentInjector` context of `TestBed`.
*
* @see EnvironmentInjector#runInContext
*/
static runInInjectionContext<T>(fn: () => T): T {
return TestBedImpl.INSTANCE.runInInjectionContext(fn);
}

static createComponent<T>(component: Type<T>): ComponentFixture<T> {
return TestBedImpl.INSTANCE.createComponent(component);
}
Expand Down Expand Up @@ -516,6 +533,10 @@ export class TestBedImpl implements TestBed {
return this.inject(token, notFoundValue, flags);
}

runInInjectionContext<T>(fn: () => T): T {
return this.inject(EnvironmentInjector).runInContext(fn);
}

execute(tokens: any[], fn: Function, context?: any): any {
const params = tokens.map(t => this.inject(t));
return fn.apply(context, params);
Expand Down