Abdul Waheed Khan
Abdul Waheed Khan

Reputation: 51

Error: No provider for Compiler! DI Exception Angular 2 Testing

The given unit test throws error when executed using npm test command. It says there was a DI Exception with error message "Error: No provider for compiler!"

import {TestComponentBuilder} from "@angular/compiler/testing";

import {
    expect,
    it,
    describe,
    async,
    inject,
    beforeEach,
    beforeEachProviders
} from "@angular/core/testing";

import {provide} from '@angular/core';
import {TestService} from "../services/Test.service";
import {TestComponent} from "./Test.component";

describe('Component: TestComponent', () => {

    let tcb;

    beforeEachProviders(() => [
        TestComponentBuilder,
        TestComponent,
        TestService
    ]);

    beforeEach(inject([TestComponentBuilder], (testComponentBuilder) => {
        tcb = testComponentBuilder;
    }));

    it('getTest', testGetTest => {
        **tcb.createAsync(TestComponent)**
            .then(fixture => {
                let TestComponent = fixture.componentInstance;
                TestComponent.selectedUserId = 3;
                expect(TestComponent.selectedUserId).beEqual(3);

            });
    });
});

The error is thrown most probably on tcb.createAsync

Following dependencies were added to project

"jasmine-core": "~2.4.1",
"karma": "1.x || ^0.13.0",
"karma-chrome-launcher": "1.x || ~0.2.2",
"karma-firefox-launcher": "1.x || ~0.1.7",
"karma-cli": "*",
"karma-jasmine": "1.x || ^0.3.6",
"karma-spec-reporter": "0.0.13",
"browserify": "latest",
"karma-browserify":  "latest" 

Upvotes: 3

Views: 2191

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657356

I think you need

import {setBaseTestProviders} from '@angular/core/testing';
import {
  TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
  TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
} from '@angular/platform-browser-dynamic/testing';
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
                     TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);

https://github.com/angular/angular/blob/master/CHANGELOG.md#200-rc0-2016-05-02

Upvotes: 1

Related Questions