我正在按照本教程https://www.youtube.com/watch?v=yG4FH60fhUE和https://angular.io/docs/ts/latest/guide/testing.html在Angular 2中编写简单的单元测试用例并验证Karma的设置。我收到此错误Uncaught SyntaxError:意外的令牌导入--- compute.spec.ts:2。语法非常简单,看起来与教程类似。但我不知道为什么我会收到这个错误。这些是使用的版本
//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",
"@angular/compiler": "2.4.10",
"@angular/core": "2.4.10",
"@angular/flex-layout": "2.0.0-beta.5",
"@angular/forms": "2.4.10",
"@angular/http": "2.4.10",
"@angular/material": "2.0.0-beta.2",
"@angular/platform-browser": "2.4.10",
"@angular/platform-browser-dynamic": "2.4.10",
"@angular/router": "3.4.10",
"@types/c3": "^0.4.39",
"angular-hammer": "^2.2.0",
"angular2-mqtt": "^1.7.1",
"c3": "^0.4.11",
"core-js": "^2.4.1",
"d3": "^3.5.17",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.1.1",
"karma-jasmine": "^1.1.0",
"rxjs": "^5.1.0",
"sw-toolbox": "^3.6.0",
"zone.js": "^0.7.7"
},
"devDependencies": {
"@angular/compiler-cli": "2.4.10",
"@ionic/app-scripts": "1.1.4",
"@types/c3": "^0.4.39",
"@types/jasmine": "^2.5.47",
"c3": "^0.4.11",
"jasmine-core": "^2.6.2",
"karma": "^1.7.0",
"typescript": "2.2.2"
}
My project structure
myproject
-src
-app
-01-fundamentals
-compute.ts
-compute.spec.ts
-karma.conf.js
//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);
})
})
//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']
},
})
}
答案 0 :(得分:0)
使用--sourcemaps = false运行测试,您将收到正确的错误消息。
ng test --sm=false