question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestBed overrideModule doesn't work in ivy

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package angular/core

Is this a regression?

Yes, the previous version in which this bug was not present was: 9.0.0-rc.1 without ivy.

Description

I want to mock a child component in the parent component unit test by overriding.

child component:

@Component({
  selector: 'test-cmp',
  template: `<div>some text</div>`
})
export class TestComponent {
  testField = 'default';
}

parent component:

@Component({
  selector: 'app-root',
  template: `<test-cmp #testCmpCtrl></test-cmp>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  @ViewChild('testCmpCtrl', {static: true}) testCmpCtrl: TestComponent;
}

parent component unit test:

@Component({
  selector: 'test-cmp',
  template: ``
})
class MockedTestComponent {
  testField = 'overwritten';
}

describe('AppComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [AppModule],
    }).overrideModule(TestModule, {
      remove: { declarations: [TestComponent], exports: [TestComponent] },
      add: { declarations: [MockTestComponent], exports: [MockTestComponent] }
    });
  });

  it('issue example', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;

    expect(app.testCmpCtrl.testField).toBe('overwritten');
  });
});

Without ivy the test works correct, but with ivy the test fails with error: “Error: Expected ‘default’ to be ‘overwritten’”, which means that the override has not occurred.

🔬 Minimal Reproduction

github: https://github.com/nmatushevskiy/ivy-overrideModule-issue

🌍 Your Environment

Angular CLI: 9.0.0-rc.1 Node: 10.16.0 OS: darwin x64 Angular: 9.0.0-rc.1 … animations, cli, common, compiler, compiler-cli, core, forms … language-service, platform-browser, platform-browser-dynamic … router

Package Version

@angular-devkit/architect 0.900.0-rc.1 @angular-devkit/build-angular 0.900.0-rc.1 @angular-devkit/build-optimizer 0.900.0-rc.1 @angular-devkit/build-webpack 0.900.0-rc.1 @angular-devkit/core 9.0.0-rc.1 @angular-devkit/schematics 9.0.0-rc.1 @ngtools/webpack 9.0.0-rc.1 @schematics/angular 9.0.0-rc.1 @schematics/update 0.900.0-rc.1 rxjs 6.5.3 typescript 3.6.4 webpack 4.41.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
gkalpakcommented, Nov 19, 2019

@kirillNikolaichuk, the built packages should be in dist/packages-dist/... (not dist/packages/...).

But, there is an even better option 🎉 Recently, we made sure all PRs publish their build artifacts, so anyone can grab them from CI and verify the change in a local project. You can read more about it here.

I.e., you can head over to the latest publish_packages_as_artifacts job for PR #33787 and get the build artifacts from the “Artifacts” tab: artifacts

1reaction
nmatushevskiycommented, Nov 12, 2019

Your variant really work. But if wrap this into beforeEach

beforeEach(() => {
    // 4. TestBed setup...
    TestBed.configureTestingModule({imports: [AppModule]});

    // 5. Override module step...
    TestBed.overrideModule(TestModule, {
      remove: {declarations: [TestComponent], exports: [TestComponent]},
      add: {declarations: [MockTestComponent], exports: [MockTestComponent]}
    });
  });

This variant wouldn’t work.

“It’s be great if we can repro the problem where Components/Modules are packaged in a single test - it’s simplify further investigation” - done

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to override provider in angular 9 - Stack Overflow
When trying to override provider in test case using, Testbed.overrideProvider it doesn't actualy override anything. It used to work fine ...
Read more >
TestBed - Angular
Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the...
Read more >
Component testing scenarios - Angular
For a use case in which TestBed.inject() does not work, see the Override component providers section that explains when and why you must...
Read more >
Troubleshooting | jest-preset-angular - GitHub Pages
beforeEach(async(() => { · TestBed.configureTestingModule({ declarations: [PizzaItemComponent] }) ·.overrideComponent(PizzaItemComponent, { · : { ...
Read more >
ng-mocks/community - Gitter
It seems that assigning ngModule.schemas = [CUSTOM_ELEMENTS_SCHEMA]; and then passing this ngModule to TestBed.configureTestingModule doesn't work.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found