Repository navigation
Open
Labels
area: testingIssues related to Angular testing features, such as TestBedIssues related to Angular testing features, such as TestBed
Milestone
Description
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
If a directive is standalone, and provides something, if it's included only (non-transitively) in the imports array of configureTestingModule, then TestBed.overrideProviders will not override the providers for that directive.
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/stackblitz-starters-avb6ge?file=src%2Fdirective%2Fdirective.spec.ts
Please provide the exception or error you saw
yarn run v1.22.19
$ jest
FAIL src/directive/directive.spec.ts (8.55 s)
AppComponent
✕ should have the right overridden token (56 ms)
● AppComponent › should have the right overridden token
Expected value undefined
Received:
undefined
Message:
expect(received).toBe(expected) // Object.is equality
Expected: "overridden"
Received: "not overridden"
Difference:
Compared values have no visual difference.Error:
33 |
34 | it('should have the right overridden token', () => {
> 35 | expect(directive.service.value).toBe('overridden');
| ^
36 | });
37 | });
38 |
at eval (src/directive/directive.spec.ts:35:37)
at _ZoneDelegate.invoke (node_modules/zone.js/bundles/zone.umd.js:412:32)
at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/bundles/zone-testing.umd.js:2164:43)
at _ZoneDelegate.invoke (node_modules/zone.js/bundles/zone.umd.js:411:38)
at ZoneImpl.run (node_modules/zone.js/bundles/zone.umd.js:147:47)
at Object.wrappedFunc (node_modules/zone.js/bundles/zone-testing.umd.js:450:38)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 9.119 s
Ran all test suites.
✨ Done in 11.16s.
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 18.0.4
Node: 18.20.3
Package Manager: yarn 1.22.19
OS: linux x64
Angular: 18.0.3
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1800.4
@angular-devkit/build-angular 18.0.4
@angular-devkit/core 18.0.4
@angular-devkit/schematics 18.0.4
@angular/cli 18.0.4
@schematics/angular 18.0.4
rxjs 7.8.1
typescript 5.4.5
zone.js 0.14.7
Anything else?
No response
Metadata
Metadata
Assignees
Labels
area: testingIssues related to Angular testing features, such as TestBedIssues related to Angular testing features, such as TestBed
Activity
jnizet commentedon Jun 20, 2024
Shouldn't it rather be
See https://stackblitz.com/edit/stackblitz-starters-936vyq?file=src%2Fdirective%2Fdirective.spec.ts,src%2Fdirective%2Fdirective.ts
BobobUnicorn commentedon Jun 20, 2024
It's not clear to me why both shouldn't work?
overrideProvidersis fine for overriding providers in Components (and for Directives transitively imported by Components).atscott commentedon Jun 20, 2024
This only works when the injector the component gets the provider from is the TestBed's
DynamicTestModule. Here's another reproduction showing the override working with a typical setup (not standalone, provider retrieved from test module), and another one that fails (not standalone, but getting the provider from an injector that isn't the test module)https://stackblitz.com/edit/stackblitz-starters-tfxawk?file=src%2Fdirective%2Fdirective.spec.ts,src%2Fdirective%2Fdirective.ts