Skip to content

Commit bc26924

Browse files
committed
fix(core): define better typings for TestModuleMetadata.
To improve compiler errors on `TestModuleMetadata`, lets replace `any` for `providers`, `declarations` and `imports` fixes angular#51765
1 parent d91618f commit bc26924

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

packages/core/test/acceptance/change_detection_signals_in_zones_spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@ import {TestBed} from '@angular/core/testing';
1313
import {signal} from '../../src/signals';
1414

1515
describe('CheckAlways components', () => {
16+
it('should properly remove stale dependencies from the signal graph', () => {
17+
@Component({
18+
template: `{{show() ? name() + ' aged ' + age() : 'anonymous'}}`,
19+
standalone: true,
20+
})
21+
class CheckAlwaysCmp {
22+
name = signal('John');
23+
age = signal(25);
24+
show = signal(true);
25+
}
26+
27+
const fixture = TestBed.createComponent(CheckAlwaysCmp);
28+
const instance = fixture.componentInstance;
29+
30+
fixture.detectChanges();
31+
expect(fixture.nativeElement.textContent.trim()).toEqual('John aged 25');
32+
33+
instance.show.set(false);
34+
fixture.detectChanges();
35+
expect(fixture.nativeElement.textContent.trim()).toEqual('anonymous');
36+
37+
instance.name.set('Bob');
38+
39+
fixture.detectChanges();
40+
expect(fixture.nativeElement.textContent.trim()).toEqual('anonymous');
41+
42+
instance.show.set(true);
43+
44+
fixture.detectChanges();
45+
expect(fixture.nativeElement.textContent.trim()).toEqual('Bob aged 25');
46+
});
47+
1648
it('can read a signal', () => {
1749
@Component({
1850
template: `{{value()}}`,

packages/core/testing/src/test_bed_common.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {InjectionToken, SchemaMetadata, ɵDeferBlockBehavior as DeferBlockBehavior} from '@angular/core';
9+
import {ModuleWithProviders} from '@angular/compiler-cli/src/ngtsc/testing/fake_core';
10+
import {EnvironmentProviders, InjectionToken, Provider, SchemaMetadata, Type, ɵDeferBlockBehavior as DeferBlockBehavior} from '@angular/core';
1011

1112

1213
/** Whether test modules should be torn down by default. */
@@ -42,9 +43,9 @@ export const ComponentFixtureNoNgZone = new InjectionToken<boolean>('ComponentFi
4243
* @publicApi
4344
*/
4445
export interface TestModuleMetadata {
45-
providers?: any[];
46-
declarations?: any[];
47-
imports?: any[];
46+
providers?: Array<Provider|EnvironmentProviders>;
47+
declarations?: Array<Type<any>>;
48+
imports?: Array<Type<any>|ModuleWithProviders<any>>;
4849
schemas?: Array<SchemaMetadata|any[]>;
4950
teardown?: ModuleTeardownOptions;
5051
/**

packages/core/testing/src/test_bed_compiler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {ResourceLoader} from '@angular/compiler';
10-
import {ApplicationInitStatus, Compiler, COMPILER_OPTIONS, Component, Directive, Injector, InjectorType, LOCALE_ID, ModuleWithComponentFactories, ModuleWithProviders, NgModule, NgModuleFactory, NgZone, Pipe, PlatformRef, Provider, provideZoneChangeDetection, resolveForwardRef, StaticProvider, Type, ɵclearResolutionOfComponentResourcesQueue, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID, ɵDEFER_BLOCK_CONFIG as DEFER_BLOCK_CONFIG, ɵDeferBlockBehavior as DeferBlockBehavior, ɵdepsTracker as depsTracker, ɵDirectiveDef as DirectiveDef, ɵgenerateStandaloneInDeclarationsError, ɵgetAsyncClassMetadata as getAsyncClassMetadata, ɵgetInjectableDef as getInjectableDef, ɵInternalEnvironmentProviders as InternalEnvironmentProviders, ɵisComponentDefPendingResolution, ɵisEnvironmentProviders as isEnvironmentProviders, ɵNG_COMP_DEF as NG_COMP_DEF, ɵNG_DIR_DEF as NG_DIR_DEF, ɵNG_INJ_DEF as NG_INJ_DEF, ɵNG_MOD_DEF as NG_MOD_DEF, ɵNG_PIPE_DEF as NG_PIPE_DEF, ɵNgModuleFactory as R3NgModuleFactory, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵNgModuleType as NgModuleType, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵRender3ComponentFactory as ComponentFactory, ɵRender3NgModuleRef as NgModuleRef, ɵresolveComponentResources, ɵrestoreComponentResolutionQueue, ɵsetLocaleId as setLocaleId, ɵtransitiveScopesFor as transitiveScopesFor, ɵUSE_RUNTIME_DEPS_TRACKER_FOR_JIT as USE_RUNTIME_DEPS_TRACKER_FOR_JIT, ɵɵInjectableDeclaration as InjectableDeclaration} from '@angular/core';
10+
import {ApplicationInitStatus, Compiler, COMPILER_OPTIONS, Component, Directive, EnvironmentProviders, Injector, InjectorType, LOCALE_ID, ModuleWithComponentFactories, ModuleWithProviders, NgModule, NgModuleFactory, NgZone, Pipe, PlatformRef, Provider, provideZoneChangeDetection, resolveForwardRef, StaticProvider, Type, ɵclearResolutionOfComponentResourcesQueue, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID, ɵDEFER_BLOCK_CONFIG as DEFER_BLOCK_CONFIG, ɵDeferBlockBehavior as DeferBlockBehavior, ɵdepsTracker as depsTracker, ɵDirectiveDef as DirectiveDef, ɵgenerateStandaloneInDeclarationsError, ɵgetAsyncClassMetadata as getAsyncClassMetadata, ɵgetInjectableDef as getInjectableDef, ɵInternalEnvironmentProviders as InternalEnvironmentProviders, ɵisComponentDefPendingResolution, ɵisEnvironmentProviders as isEnvironmentProviders, ɵNG_COMP_DEF as NG_COMP_DEF, ɵNG_DIR_DEF as NG_DIR_DEF, ɵNG_INJ_DEF as NG_INJ_DEF, ɵNG_MOD_DEF as NG_MOD_DEF, ɵNG_PIPE_DEF as NG_PIPE_DEF, ɵNgModuleFactory as R3NgModuleFactory, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵNgModuleType as NgModuleType, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵRender3ComponentFactory as ComponentFactory, ɵRender3NgModuleRef as NgModuleRef, ɵresolveComponentResources, ɵrestoreComponentResolutionQueue, ɵsetLocaleId as setLocaleId, ɵtransitiveScopesFor as transitiveScopesFor, ɵUSE_RUNTIME_DEPS_TRACKER_FOR_JIT as USE_RUNTIME_DEPS_TRACKER_FOR_JIT, ɵɵInjectableDeclaration as InjectableDeclaration} from '@angular/core';
1111

1212
import {ComponentDef, ComponentType} from '../../src/render3';
1313

@@ -57,7 +57,7 @@ export class TestBedCompiler {
5757
// Testing module configuration
5858
private declarations: Type<any>[] = [];
5959
private imports: Type<any>[] = [];
60-
private providers: Provider[] = [];
60+
private providers: (Provider|EnvironmentProviders)[] = [];
6161
private schemas: any[] = [];
6262

6363
// Queues of components/directives/pipes that should be recompiled.

0 commit comments

Comments
 (0)