Skip to content

NativeScript - how to get unit tests to work properly? #5281

Open
@aubrey-fowler

Description

@aubrey-fowler

I'm writing an app with NativeScript 6+ and Angular 8+.

I'm trying to write some unit tests and get them up and running.

I have read the documentation on unit testing: https://docs.nativescript.org/tooling/testing/testing

I followed the directions there for setting up the tests and using TestBed. My tests are not working and throwing errors.

Here is my repository: https://github.com/aubrey-fowler/NativeScriptUnitTests

Questions:

  1. The documentation only shows an example of how to write a test for a component. How do I write a test for a service? I also need to use TestBed for this because my service has dependency injection.
  2. My tests are throwing errors. Why and how can I fix them?

Errors:

no reachable hosts
on my Android phone

code snippet:

import { ItemsComponent } from '../app/item/items.component';

import {
    nsTestBedAfterEach,
    nsTestBedBeforeEach,
    nsTestBedRender
} from 'nativescript-angular/testing';

describe('ItemsComponent Test', () => {

    beforeEach(nsTestBedBeforeEach([ItemsComponent]));
    afterEach(nsTestBedAfterEach(false));

    it('should be defined', () => {

        nsTestBedRender(ItemsComponent).then((fixture) => {
            fixture.detectChanges();
            const component = fixture.componentInstance;
            expect(component).toBeTruthy;
        });

    });

});

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions