Nativescript-cli: The test init command to add a sample test in TypeScript for TypeScript/Angular projects

Created on 2 Jun 2016  路  26Comments  路  Source: NativeScript/nativescript-cli

The tns test init command configures a project for unit testing, creates a _tests_ folder in the _app_ directory and adds an example.js file in it with a sample test.

It would be great the sample test to be in TypeScript (example.ts) for TypeScript/Angular projects.

feature unit testing

Most helpful comment

In a project of entirely typescript files, it seems backwards to write JavaScript tests.
Is there any update to this, or is anyone aware of how to get my tests compiled along with tns test <platform>?

Adding karma-typescript to the karma.conf.js causes issues (Javascript running out of memory, tsconfig related conflicts, generally seems to be the wrong way to achieve this) - I can't seem to figure out how other people are writing tests in typescript (example: nativescript-angular's tests are all in typescript)

All 26 comments

This came up as a limitation while writing a guide on unit testing today, so I鈥檒l toss in my vote for this feature.

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

@vchimev or @tjvantoll, can we get an update on this issue? Will this be worked on any time soon? Thanks!

+1

For now, here's an template/example of a .spec.ts file that works. It tests a component that requires a REST service, but that is mocked by mockito.

import {mock} from 'ts-mockito';
import * as utils from 'utils/utils';
import {ContactDetailsNSComponent} from '../contact-details/contact-details.component';
import {GtDirectoryService, ContactViewModel} from '@private/library';

describe('Contact details test', function () {
  let contactDetailsComponent;

  beforeEach(function () {

    const directoryService = mock(GtDirectoryService);
    contactDetailsComponent = new ContactDetailsNSComponent(new ContactViewModel('4799999999'), directoryService);
  });

  it('should create tel-uri when tapping call', function () {

    const spy = spyOn(utils, 'openUrl');
    contactDetailsComponent.onTapCall();
    expect(spy.calls.count()).toBe(1);
    expect(spy.calls.first().args[0]).toBe('tel:+4799999999');
  });

  it('should create sms-uri when tapping sms', function () {

    const spy = spyOn(utils, 'openUrl');
    contactDetailsComponent.onTapMessage();
    expect(spy.calls.count()).toBe(1);
    expect(spy.calls.first().args[0]).toBe('sms:+4799999999');
  });
});

+2 :)

In a project of entirely typescript files, it seems backwards to write JavaScript tests.
Is there any update to this, or is anyone aware of how to get my tests compiled along with tns test <platform>?

Adding karma-typescript to the karma.conf.js causes issues (Javascript running out of memory, tsconfig related conflicts, generally seems to be the wrong way to achieve this) - I can't seem to figure out how other people are writing tests in typescript (example: nativescript-angular's tests are all in typescript)

+2

Currently you should be able to achieve this using the following steps:

  1. Replace example.js file with example.ts inside tests folder
  2. Change the files property in karma.conf.js
files: [
      'app/tests/**/*.ts'
],
  1. Install the required definitions for the selected testing framework
    When mocha is selected: npm i --save-dev @types/chai && npm i --save-dev @types/mocha && npm i --save-dev @types/karma-chai

We're working on improving our unit testing story and planned this issue for one of our next releases.

This worked, but the test init command put the files in src/tests/, so we made this line in the karma.conf.js src/tests/**/*.ts

Was this page helpful?
0 / 5 - 0 ratings