1

When trying to override provider in test case using, Testbed.overrideProvider it doesn't actualy override anything.

It used to work fine angular 9 with jasmine, not sure what has changed in angular 9 which has broke it.

Any help will be appriciated. in my app html component I am removing .bg-image when layout$ contains 'mobile'.

Here is what I have:

app.component.spec.ts

    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
      ],
      providers: [
        {
          provide: BreakpointService,
          useValue: {
            layout$: of('web')
          }
        }
      ],
      declarations: [AppComponent]
    }).compileComponents();

and in my one of test cases it(), I am doing

    TestBed.overrideProvider(BreakpointService, {
      useValue: { layout$: of('mobile') }
    });
    fixture.detectChanges();
    const imageElem = fixture.debugElement.query(By.css('.bg-image'));
    expect(imageElem).toBeFalsy();

I used to overrideProvider like this with angular 6 and hadn't been using angular for a while now. Today I started working on a new project with angular 9 and even tho it has these same methods mocking with overrideProvider doesn't work anymore. I know there is actually lot changed since angular 6, like default test framework has changed from jasmine to jest and new ivy render and many more. But my question remains, I am not sure if this is a bug, or I am just being dumb. I just wanted to ask someone with more experience here before I go ahead and open an issue in repo.

Rushi patel
  • 522
  • 7
  • 17
  • Please provide code and possible errors that pop up. – Philipp Meissner Apr 16 '20 at 15:53
  • If it used to work, what did you change now that it doesn't? We will need a bit more info to help out – Ash Apr 16 '20 at 15:56
  • Added more description now – Rushi patel Apr 16 '20 at 16:13
  • I experience the same issue after upgrade from angular 7 to 9 it stopped working. There is no specific error it's just the fact that Provider doesn't have the overridden value. Is this a bug in NG9? – Rambou May 15 '20 at 11:27
  • seems like that, and no one has come across it but me? No one seems to talking about it – Rushi patel May 15 '20 at 11:42
  • As discussed here https://stackoverflow.com/questions/48811091/how-override-provider-in-angular-5-for-only-one-test, TestBed is frozen after calling `compileComponents()` and overrideProvider has no effect. – schaenk May 27 '20 at 08:31

0 Answers0