我在终端(在gruntfile.js的位置)运行时遇到错误:
grunt karma
此处的解决方案不起作用或不再是一个选项 - https://github.com/vojtajina/ng-directive-testing/issues/2
这是输出:
Running "karma:unit" (karma) task
26 05 2017 13:43:15.838:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:8082/
26 05 2017 13:43:15.840:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
26 05 2017 13:43:15.848:INFO [launcher]: Starting browser PhantomJS
26 05 2017 13:43:15.955:ERROR [phantomjs.launcher]: Fontconfig warning: ignoring C.UTF-8: not a valid language tag
26 05 2017 13:43:16.130:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket 9TxdacwF4rc7mUB0AAAA with id 76282796
PhantomJS 2.1.1 (Linux 0.0.0) ERROR
SyntaxError: Unexpected token '>'
at app/client/app.js:2
PhantomJS 2.1.1 (Linux 0.0.0) ERROR
SyntaxError: Unexpected token '>'
at app/client/paths/home/homeCtrl.js:2
这是我的代码:
karma.config.js
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'app/**/*.js'
],
port: 8082,
browsers: ['PhantomJS'],
plugins: [
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor'
],
logLevel: config.LOG_INFO,
})};
gruntfile.js中的karma grunt测试块
karma: {
unit: {
configFile: 'karma.config.js',
singleRun: true
}
}
homeCtrl.spec.js
describe('HomeCtrl', function() {
beforeEach(module('myApp'));
var HomeCtrl;
beforeEach(inject(function($controller){
HomeCtrl = $controller('HomeCtrl');
}));
describe('message', function() {
it('should read hello', function() {
expect(HomeCtrl.message).toBe('hello');
});
});
});
homeCtrl.js
angular.module('myApp')
.controller('HomeCtrl', ($scope) => {
$scope.message = 'hello';
})
app.js
angular.module('myApp', ['ngRoute', 'ngMaterial'])
.config(($routeProvider) => {
$routeProvider.when("/", { templateUrl : "paths/home/home.html" });
});
这是文件夹结构: https://i.stack.imgur.com/ncQI3.png
非常感谢任何意见,谢谢。
答案 0 :(得分:3)
很可能这个问题是由PhantomJS无法运行ES6代码引起的。
导致此问题的部分是app.js
中的箭头功能:.config(($routeProvider) => {
所以要么
使用ES5有效代码
.config(function($routeProvider) {
提供相应的polyfill ,例如,使用karma-es6-shim
预编译代码,例如使用grunt-babel
希望这有帮助。