The test init command to add a sample test in TypeScript for TypeScript/Angular projects
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.
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:
- Replace
example.jsfile withexample.tsinsidetestsfolder - Change the files property in
karma.conf.js
files: [
'app/tests/**/*.ts'
],
- 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