Template parse error in Jasmine test but not actual app

折月煮酒 提交于 2019-12-04 22:49:47

Well, the module of your test only has MiddleRowComponent declared. So it doesn't know about CircleComponent:

TestBed.configureTestingModule({
    declarations: [MiddleRowComponent], // declare the test component
})

Add all the necessary components in the declarations of the testing module, or add LandingPageModule to the imports of the testing module.

I had a similar problem and found this page, and while JB Nizet's answer led to a solution for me, it didn't work for me as-is. I'm not trying to take away from his solid answer to the original question, just trying to help the next person who comes along.

My problem was exactly like the OP's, except that my custom component (MiddleRowComponent in this example) used a third-party component. The unit test error was given about the third-party tag used in my template, even though it worked just fine in the actual app. The solution for me was to also include an imports for the third-party in my testing module:

TestBed.configureTestingModule({
    declarations: [MiddleRowComponent],
    imports: [TheThirdPartyModule]
})

Then my unit tests ran without the error. Hope that helps!

Another option is to add the NO_ERRORS_SCHEMA schema to the test setup. Any unrecognised components will now not cause an error. I use this a lot when working with third party modules like Angular Material Design.

import { NO_ERRORS_SCHEMA } from '@angular/core';
...
  beforeEach(
    async(() => {
      TestBed.configureTestingModule({
        declarations: [MiddleRowComponent],
        schemas: [NO_ERRORS_SCHEMA]
      }).compileComponents();
    })
  );
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!