Kamil Naja
Kamil Naja

Reputation: 6692

NO_ERRORS_SCHEMA stops Karma test working in angular

I have working Angular testing setup. When I test component with nested components inside, this gives me errors, that's why, I need to mock those components.

Angular Test documentation show us other method to mock components - using NO_ERRORS_SCHEMA. When I try to use this (both in headless or normal mode using Chrome), my tests just stops working. Info looks like this

24 03 2019 18:48:45.750:INFO [launcher]: Starting browser Chrome
24 03 2019 18:48:49.599:WARN [karma]: No captured browser, open 
http://localhost:9876/    
24 03 2019 18:48:49.644:INFO [HeadlessChrome 73.0.3683 (Ubuntu 
0.0.0)]: Connected on socket cQvumAWhGFfzQWh0AAAA with id 98993378

In mode with browser, I can see only info:

Karma v4.0.1 - connected
Chromium 73.0.3683 (Ubuntu 0.0.0) is idle 

When I run my test without [NO_ERRORS_SCHEMA], I can see my tests results, that's why, error is related with this schema setup.

My component spec setup:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ContactComponent } from './contact.component';
import { NO_ERRORS_SCHEMA } from '@angular/compiler/src/core';

describe('ContactComponent', () => {
  let component: ContactComponent;
  let fixture: ComponentFixture<ContactComponent>;

  beforeEach(async () => {
    TestBed.configureTestingModule({
      declarations: [ContactComponent],
      schemas: [NO_ERRORS_SCHEMA]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(ContactComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeFalsy();
  });
});

Question is - how make tests works with NO_ERRORS_SCHEMA

My machine - Ubuntu 18.04, Node 11.11

"jasmine-core": "~3.3.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "^4.0.1",
"karma-chrome-launcher": "~2.2.0",
"karma-cli": "~2.0.0",
"karma-coverage-istanbul-reporter": "^2.0.5",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",

Upvotes: 2

Views: 3572

Answers (1)

Kamil Naja
Kamil Naja

Reputation: 6692

I't seems, that I found an answer. In Chrome test browser window inside devtools console, I found an tip -

Uncaught Error: Cannot find module 'tslib'
at webpackEmptyContext

Googling this, gives me answer - import declaration of NO_ERRORS_SCHEMA is not correct.

import { NO_ERRORS_SCHEMA } from '@angular/compiler/src/core';

I achieved the expected result after changing this to:

import { NO_ERRORS_SCHEMA } from '@angular/core';

Upvotes: 7

Related Questions