Skip to content

TestBed.overrideProviders does not override providers of imported standalone directives #56528

Open

Description

@BobobUnicorn

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

Activity

  1. jnizet commented on Jun 20, 2024

    @jnizet
    Contributor

    Shouldn't it rather be

    TestBed.overrideDirective(StandaloneDirective, {
          remove: {
            providers: [StandaloneDirectiveService]
          },
          add: {
            providers: [
              { 
                provide: StandaloneDirectiveService, 
                useValue: { value: 'overridden' },
              }
            ]
          }
        });
    

    See https://stackblitz.com/edit/stackblitz-starters-936vyq?file=src%2Fdirective%2Fdirective.spec.ts,src%2Fdirective%2Fdirective.ts

  2. BobobUnicorn commented on Jun 20, 2024

    @BobobUnicorn
    Author

    It's not clear to me why both shouldn't work? overrideProviders is fine for overriding providers in Components (and for Directives transitively imported by Components).

  3. atscott commented on Jun 20, 2024

    @atscott
    Contributor

    overrideProviders is fine for overriding providers in Components (and for Directives transitively imported by Components).

    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

  4. added this to the needsTriage milestone on Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testingIssues related to Angular testing features, such as TestBed

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      Sponsor
      SponsoredKunjungi sekarang
      Promo