I've been exploring this insightful tutorial on https://www.youtube.com/watch?v=yG4FH60fhUE and also referencing https://angular.io/docs/ts/latest/guide/testing.html to create basic unit tests in Angular 2 and ensure the proper setup of Karma. I encountered an issue with the error message "Uncaught SyntaxError: Unexpected token import --- compute.spec.ts:2". The syntax appears straightforward and aligns closely with the tutorial, but for some reason, this error persists. Here are the versions being used:
//package.json
"scripts": {
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build",
"serve": "ionic-app-scripts serve",
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve",
"test": "karma start karma.conf.js"
},
"dependencies": {
"@angular/animations": "4.0.3",
"@angular/common": "2.4.10",
...
Here is a glimpse into my project structure:
- myproject -src -app -01-fundamentals -compute.ts -compute.spec.ts -karma.conf.js
The contents of compute.spec.ts:
import {compute} from './compute';
describe('compute',() => {
it('should return 0 if input is negative', () => {
const result= compute(-1);
expect(result).toBe(0);
})
})
The configuration in karma.conf.js:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: ["src/app/**/*.spec.ts"],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_DEBUG,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity,
plugins: [ 'karma-jasmine', 'karma-chrome-launcher'],
mime: {
'text/x-typescript': ['ts','tsx']
},
})
}