How to implement unit tests in NativeScript using TestBed and Jasmine? How to implement unit tests in NativeScript using TestBed and Jasmine? angular angular

How to implement unit tests in NativeScript using TestBed and Jasmine?


To use TestBed you have to alter your karma.conf.js to:

    // list of files / patterns to load in the browser    files: [      'src/tests/setup.ts',      'src/tests/**/*.spec.ts'    ],

The file src/tests/setup.ts should look like this for jasmine:

import "nativescript-angular/zone-js/testing.jasmine";import {nsTestBedInit} from "nativescript-angular/testing";nsTestBedInit();

or if using mocha:

import "nativescript-angular/zone-js/testing.mocha";import {nsTestBedInit} from "nativescript-angular/testing";nsTestBedInit();

You'll find a sample here: https://github.com/hypery2k/tns_testbed_sample


I was facing the same issue like you. I finally found a way to make Unit Test in Nativescript-Angular works.

To fix my issue I add beforeAll(() => nsTestBedInit()); and afterAll(() => { });Also change from TestBed to nsTestBed...I just follow the idea on https://github.com/NativeScript/nativescript-angular/blob/master/tests/app/tests/detached-loader-tests.ts

Also add into tsconfig.tns.json file this line:"include": ["src/tests/*.spec.ts"],

My issue now is split all test into multiple file. Like appComponent in a test fileand homeCompenent in a second test file. When the app grow the unit test also grow, we need organize our code.

Here my code (file name: src/tests/test.spec.ts):

    import "reflect-metadata";    import { AppComponent } from "../app/app.component";    import { nsTestBedBeforeEach, nsTestBedAfterEach, nsTestBedRender, nsTestBedInit }     from "nativescript-angular/testing";    import { HomeComponent } from "@src/app/home/home.component";    describe("AppComponent", () => {        beforeAll(() => nsTestBedInit());        afterAll(() => { });        beforeEach(nsTestBedBeforeEach([AppComponent, HomeComponent]));        afterEach(nsTestBedAfterEach());        it("should be: app works!", () => {            return nsTestBedRender(AppComponent).then((fixture) => {                fixture.detectChanges();                const app = fixture.componentInstance;                expect(app.title).toBe("app works!");            });        });        describe("HomeComponent", () => {             it("should contain: Home works!", () => {              return nsTestBedRender(HomeComponent).then((fixture) => {                  fixture.detectChanges();                  const app = fixture.componentInstance;                  expect(app.title).toBe("Home works!");              });            });        });    });

And here the result:

JS: NSUTR: downloading http://192.168.10.169:9876/context.jsonJS: NSUTR: eval script /base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?be3ff9a5e2d6d748de5b900ac3c6d9603e2942a7JS: NSUTR: eval script /base/node_modules/karma-jasmine/lib/boot.js?945a38bf4e45ad2770eb94868231905a04a0bd3eJS: NSUTR: eval script /base/node_modules/karma-jasmine/lib/adapter.js?3098011cfe00faa2a869a8cffce13f3befc1a035JS: NSUTR: eval script /base/src/tests/test.spec.bundle.js?6e0098824123f3edc2bb093fa874b3fdf268841eJS: NSUTR: beginning test runNativeScript / 28 (9; Android SDK built for x86): Executed 1 of 2 SUCCESS (0 secs / 0.545 secs)NativeScript / 28 (9; Android SDK built for x86): Executed 2 of 2 SUCCESS (0.829 secs / 0.735 secs)TOTAL: 2 SUCCESSJS: NSUTR: completeAckNativeScript / 28 (9; Android SDK built for x86) ERROR  DisconnectedClient disconnected from CONNECTED state (transport error)NativeScript / 28 (9; Android SDK built for x86): Executed 2 of 2 SUCCESS (0.829 secs / 0.735 secs)


Your issue is happening because of these lines.

beforeAll(() => nsTestBedInit()); afterAll(() => { });

Each test file tries to initialize test bed. Make sure that you initialize it in your entry component.

Please refer entry file test-main.ts specified in karma.config.js