I'm facing a challenge while trying to create tests for an Ionic2 App using Karma + Jasmine. I encountered a runtime error and, given my lack of experience, I'm having trouble pinpointing the actual issue.
Here is my setup:
test.ts
This file contains a simple class with a basic constructor and a dummy function:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-testing',
templateUrl: 'testing.html'
})
export class TestingPage {
constructor(public navCtrl: NavController) {}
isTrue(): boolean {
return true;
}
}
test.spec.ts
import { TestingPage } from "./test.ts"
import { NavController } from 'ionic-angular';
let navCtrl: NavController;
let test = new TestingPage(navCtrl);
describe('Dummy test:', () => {
it("Should be defined", () => {
expect(test.isTrue()).toBeTruthy();
});
});
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine', 'karma-typescript'],
files: [
//'./src/pages/testing/test.ts',
'./src/pages/testing/test.spec.ts'
],
exclude: [
],
preprocessors: {
//'./src/pages/testing/test.ts': ['karma-typescript'],
'./src/pages/testing/test.spec.ts': ['karma-typescript']
},
typescriptPreprocessor: {
options: {
sourceMap: false,
target: 'ES5',
module: 'amd',
noImplicitAny: true,
noResolve: true,
removeComments: true,
concatenateOutput: false
},
transformPath: function(path) {
return path.replace(/\.ts$/, '.js');
}
},
reporters: ['progress', 'karma-typescript'],
port: 9876,
colors: true,
logLevel: config.LOG_DEBUG,
autoWatch: true,
browsers: ['Chrome', 'PhantomJS'],
singleRun: false,
concurrency: Infinity
})
}
Additional information that may be relevant...
Ionic version: 2.1.13
Karma version: 1.3.0
TypeScript: 2.0.6
jasmine-core: 2.4.1
When running karma start
, the error message I receive is:
PhantomJS 2.1.1 (Linux 0.0.0) ERROR
TypeError: undefined is not a constructor (evaluating 'new test_ts_1.TestingPage(navCtrl)')
at src/pages/testing/test.spec.ts:5:0 <- src/pages/testing/test.spec.js:4
Chrome 54.0.2840 (Linux 0.0.0) ERROR
Uncaught TypeError: test_ts_1.TestingPage is not a constructor
at src/pages/testing/test.spec.ts:5:0 <- src/pages/testing/test.spec.js:4
I have tried including the test.ts in both karma files and preprocessors sections (commented out in the above karma.conf.js), but this changes the error to:
PhantomJS 2.1.1 (Linux 0.0.0) ERROR
ReferenceError: Can't find variable: Map
at /tmp/karma-typescript-bundle-2311r0Dc9vytYCw1.js:5007
Chrome 54.0.2840 (Linux 0.0.0) ERROR
Uncaught TypeError: Cannot read property '__symbol__' of undefined
at /tmp/karma-typescript-bundle-2311r0Dc9vytYCw1.js:26028
which seems even less informative.
Additionally, after downgrading Jasmine from 2.5.X following advice from this question, the error still persists.
EDIT:
Simple tests like the one below work fine:
expect(2+2).toEqual(4);
without importing any class. Any suggestions on what might be missing or incorrect?