NativeScript Unit Test Runner

0.7.0 · active · verified Tue Apr 21

The `nativescript-unit-test-runner` package provides the core infrastructure for executing unit tests within NativeScript applications. It is primarily consumed by the NativeScript CLI (`tns test`) and integrates with standard JavaScript testing frameworks like Karma, Jasmine, and Mocha. The current stable version is `4.0.1`, which includes support for NativeScript 9 and improved ES Module syntax handling. This package acts as the bridge between your test code, the NativeScript runtime, and the underlying test runner environment, facilitating the execution of tests directly on emulators, simulators, or physical devices. It addresses the complexities of running tests in a device-native context, a key differentiator from web-based test runners, and has an active release cadence with major version updates supporting new NativeScript versions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a new NativeScript project, setting up the unit test runner with `tns test init`, and executing example tests on Android or iOS devices/simulators.

npx create-nativescript-app my-test-app --template @nativescript/template-blank-typescript
cd my-test-app
tns test init

# The 'tns test init' command will guide you through setting up Jasmine or Mocha.
# For example, select Jasmine during the interactive prompt.

# Now, create a simple test file. For instance, add `app/tests/example.ts` with the following content:
/*
describe('Example Test Suite', () => {
  it('should verify true is true', () => {
    expect(true).toBe(true);
  });

  it('should correctly concatenate strings', () => {
    const greeting = 'Hello';
    const name = 'NativeScript';
    expect(`${greeting} ${name}`).toBe('Hello NativeScript');
  });
});
*/

# To run tests on an Android emulator or connected device:
tns test android

# To run tests on an iOS simulator or connected device (requires Xcode):
tns test ios

# For continuous testing that watches for file changes:
tns test android --watch

view raw JSON →