1

I'm trying to use the test host pattern described in the Angular docs, where you create a super lightweight component in your spec whose template is a literal that just includes the component under test.

@Component({
    selector: "simple-test",
    template: "<div>Hi</div>",
})
class SimpleComponent {}

@Component({
    template: `<simple-test></simple-test>`
})
class NgModelTestHost {}

describe("test host example", () => {
    let ngmFixture: ComponentFixture<NgModelTestHost>;

    beforeEach(waitForAsync(() => {
        return TestBed.configureTestingModule({
            imports: [
                CommonModule,
            ],
            declarations: [
                SimpleComponent,
                NgModelTestHost,
            ],
            schemas: [NO_ERRORS_SCHEMA],
        }).compileComponents().then(() => {
            ngmFixture = testBed.createComponent(NgModelTestHost);
        });
    }));

    it("exists", () => {
        expect(ngmFixture).toBeDefined();
    });
});

As far as I can tell, this is exactly like the example linked above from the Angular docs. When I run the test, it succeeds immediately, but a few seconds thereafter I get a spurious error NG8001: 'simple-test' is not a known element: error on the console.

The tests are built and run using karma-webpack. I think Webpack is running a template compiler (checker?) in the background asynchronously, and for some reason it's compiling this component without being part of the test module, if that makes any sense. (If it were respecting the test module config, it would also see NO_ERRORS_SCHEMA, right?)

I'm not sure how to proceed from here. I haven't put together a complete minimal repro because getting karma-webpack set up was a pain and my operational webpack config is huge and complex. I'm hoping this is enough information to at least get pointed in the right direction to tweak my karma or webpack config and stop this from happening.

Coderer
  • 25,844
  • 28
  • 99
  • 154
  • I set `forkTypeChecker: false` in my AngularCompilerPlugin config so now at least the test run fails when this happens, but I still don't know what's causing it. If I move the component declaration into the module under test -- declared alongside the component, included in the `declarations` array for the project module -- everything works fine. It's like I can't create components in the spec file at all. – Coderer Nov 02 '20 at 15:10

0 Answers0