jeudi 5 janvier 2017

angular 2 testing - Can't create a component

My app has lots of modules with many components and services and other Angular2 stuff.

Now I'm trying to use TestBed approach to create a unit tests with jasmine + karma.

I've faced an error during revealing proof of concept. I've created a test for one of my components, which looks like this:

let myCompServiceStub = {} as MyComponentService;
let routerStub = {} as Router;
let globalServiceStub  = {} as MyGlobalService;

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

    beforeEach(async(() => {
        TestBed
            .configureTestingModule({
                imports: [MyModule],
                providers: [
                    { provide: MyComponentService, useValue: myCompServiceStub },
                    { provide: Router, useValue: routerStub },
                    { provide: MyGlobalService, useValue: globalServiceStub }
                ]
            })
            .compileComponents()
            .then(() => {
                fixture = TestBed.createComponent(ListPRDComponent);
                // component = fixture.componentInstance;
            });
    }));


    it('test it', () => {
        expect(true).toBe(true);
    });
});

MyModule has a bunch of imports of other modules (most of them mine, but there are also CommonModule, MaterializeModule, FormsModule modules form Angular and Materialize), that define some global components. It also has a MyComponentService in providers. The imported custom modules has no provided services.

MyGlobalService provided in AppComponent, which is main component.

When I try to run the tests, I get an error:

PhantomJS 2.1.1 (Windows 8 0.0.0) MyComponent test it FAILED
        invokeTask@karma-shim.js:8833:41
        onInvokeTask@karma-shim.js:10715:50
        invokeTask@karma-shim.js:8832:53
        runTask@karma-shim.js:8709:58
        drainMicroTaskQueue@karma-shim.js:8976:43
        run@karma-shim.js:4098:31
        karma-shim.js:4111:33
        flush@karma-shim.js:4466:13
        resolvePromise@karma-shim.js:9045:79
        karma-shim.js:9081:32

And I'm really stuck on it, have no good ideas what am I doing wrong?

My tests dependencies looks like:

"@angular/common": "~2.3.0",
"@angular/compiler": "~2.3.0",
"@angular/compiler-cli": "^2.3.1",
"@angular/core": "~2.3.0",
"@angular/forms": "~2.3.0",
"@angular/http": "~2.3.0",
"@angular/platform-browser": "~2.3.0",
"@angular/platform-browser-dynamic": "~2.3.0",
"@angular/platform-server": "^2.3.1",
"@angular/router": "~3.3.0",
"angular2-materialize": "6.3.0",
"karma": "1.1.2",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.0.0",
"karma-jasmine": "^1.0.2",
"karma-phantomjs-launcher": "^1.0.0",
"karma-remap-istanbul": "0.1.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "1.7.0",
"@types/jasmine": "^2.5.36",
"jasmine-core": "^2.3.4",
"jasmine-spec-reporter": "^2.4.0",

Aucun commentaire:

Enregistrer un commentaire