【问题标题】:Grunt Karma PhantomJS - SyntaxError: Unexpected token '>'?Grunt Karma PhantomJS - SyntaxError: Unexpected token '>'?
【发布时间】:2017-05-26 14:16:18
【问题描述】:

在我的终端(在 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

任何意见将不胜感激,谢谢。

【问题讨论】:

  • 你在这里错过了逗号port: 8082
  • 哦,是的,谢谢,但是问题仍然存在。只是复制代码时出错。
  • SyntaxError: Unexpected token '>' at app/client/app.js:2你能展示你的app.js吗?
  • angular.module('myApp', ['ngRoute', 'ngMaterial']) .config(($routeProvider) => { $routeProvider .when("/", { templateUrl : "paths /home/home.html" }); });
  • 你有什么想法吗?

标签: angularjs testing gruntjs karma-jasmine


【解决方案1】:

这个问题很可能是由 PhantomJS 无法运行 ES6 代码引起的。

造成这种情况的部分是app.js 中的箭头函数:.config(($routeProvider) => {

所以

  1. 使用 ES5 有效代码

    .config(function($routeProvider) { 
    
  2. 提供适当的 polyfills,例如,使用 karma-es6-shim

  3. 预编译您的代码,例如使用grunt-babel

希望这会有所帮助。

【讨论】:

  • 已排序,谢谢。在没有额外支持的情况下,PhantomJS 还没有使用 ES6 有点可惜。我认为这是我的 html 代码的问题。
猜你喜欢
  • 2017-04-06
  • 2017-02-27
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 2012-05-17
  • 2019-02-10
  • 2019-11-10
  • 2014-07-08
相关资源
最近更新 更多