Testing component in NativeScript Angular2 app

angular2-nativescript, nativescript, unit-testing

Solution

AFAIK TestBed does not work on NativeScript Angular. To mock components you need to instance them with `new`, resolving each dependency.

You should study this repository to be able to do some unit testing on NativeScript.

Problem

I'm having issues doing a very simple component test in a NativeScript Angular 2 application. I can't seem to just call "new Component()" and then test it like was shown in this test example. I'm trying an implementation of the Angular 2 TestBed in order to gain access to the component. This doesn't appear to be working either. Has anybody run into a similar problem or have any experience with this? Component under test: ``` import { Component } from "@angular/core"; @Component({ selector: "links", templateUrl: "./components/links/links.component.html" }) export class LinksComponent { test = "test"; public constructor() { } } ``` Test: ``` import "reflect-metadata"; import { LinksComponent } from "../../components/links/links.component"; import { ComponentFixture, TestBed } from '@angular/core/testing'; describe("Links Component", () => { let comp: LinksComponent; let fixture: ComponentFixture<LinksComponent>; beforeEach(() => { TestBed.configureTestingModule({ declarations: [ LinksComponent ], }); fixture = TestBed.createComponent(LinksComponent); comp = fixture.componentInstance; }); it("will pass because this is a silly test", () => { expect(comp.test).toEqual("test"); }) }); ``` Log output: ``` NativeScript / 10.3 (10.3; iPhone) ../../tests/components/links.spec.js at line 0 FAILED ReferenceError: Can't find variable: Zone NativeScript / 10.3 (10.3; iPhone): Executed 1 of 0 (1 FAILED) (0 secs / 0 seNativeScript / 10.3 (10.3; iPhone): Executed 1 of 0 (1 FAILED) ERROR (0.008 secs / 0 secs) CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:258:24: NSUTR: completeAck May 11 16:16:43 --- last message repeated 1 time --- CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:90:28: NSUTR-socket.io: io server disconnect CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:151:24: NSUTR: disregarding second execution Test run failed. ```

Original source