I want to test my angular2 application with karma, but I get a typescript error:
/my/path/node_modules/angular2/src/testing/matchers.d.ts
Error: (4, 37) TS2503: Cannot find jasmine namespace.
NPM modules are installed, typescript compiler works well. What's wrong?
myservice.serviceSpec.ts:
import {it, describe, expect, beforeEach, inject} from
'angular2/testing';
import {myService} from "../app/myservice.service";
describe('Tests', () => {
it('should be true', () => {
expect(true).toBe(true);
});
});
package.json:
{
"name": "myapp",
"version": "0.1.0",
"dependencies": {
"angular2": "^2.0.0-beta.2",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.13",
"reflect-metadata": "^0.1.2",
"rxjs": "^5.0.0-beta.0",
"systemjs": "^0.19.17",
"zone.js": "^0.5.10"
},
"devDependencies": {
"jasmine-core": "^2.4.1",
"karma": "^0.13.19",
"karma-chrome-launcher": "^0.2.2",
"karma-coverage": "^0.5.3",
"karma-jasmine": "^0.3.7",
"remap-istanbul": "^0.5.1"
}
}
tsconfig.json:
{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true
},
"exclude": [
"node_modules"
]
}
karma.conf.js:
module.exports = function (config) {
config.set ({
basePath: '',
frameworks: ['jasmine'],
files: [
'node_modules/systemjs/dist/system.src.js',
'node_modules/angular2/bundles/http.dev.js',
'test/karma_test_shim.js',
'test/myservice.serviceSpec.js',
{pattern: 'app/**/*.js', included: false, watched: true},
{pattern: 'test/**/*Spec.js', included: false, watched: true}
],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
karma_test-shim.js:
__karma__.loaded = function() {};
System.config({
packages: {
'base/app': {
defaultExtension: false,
format: 'register',
map: Object.keys(window.__karma__.files).
filter(onlyAppFiles).
reduce(function createPathRecords(pathsMapping, appPath) {
var moduleName = appPath.replace(/^\/base\/app\//, './').replace(/\.js$/, '');
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
return pathsMapping;
}, {})
}
}
});
System.import('angular2/testing').then(function(testing) {
return System.import('angular2/platform/testing/browser').then(function(testing_platform_browser) {
testing.setBaseTestProviders(testing_platform_browser.TEST_BROWSER_PLATFORM_PROVIDERS,
testing_platform_browser.TEST_BROWSER_APPLICATION_PROVIDERS);
});
}).then(function() {
return Promise.all(
Object.keys(window.__karma__.files)
.filter(onlySpecFiles)
.map(function(moduleName) {
return System.import(moduleName);
}));
}).then(function() {
__karma__.start();
}, function(error) {
__karma__.error(error.stack || error);
});
function onlyAppFiles(filePath) {
return /^\/base\/app\/.*\.js$/.test(filePath)
}
function onlySpecFiles(path) {
return /Spec\.js$/.test(path);
}