angularjs - Grunt Karma PhantomJS - 语法错误 : Unexpected token '>' ?

标签 angularjs testing gruntjs karma-jasmine

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

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';    
    })

应用程序.js

angular.module('myApp', ['ngRoute', 'ngMaterial'])
    .config(($routeProvider) => { 
        $routeProvider.when("/", { templateUrl : "paths/home/home.html" });
    }); 

文件夹结构如下: /image/ncQI3.png

任何输入将不胜感激,谢谢。

最佳答案

这个问题很可能是由于 PhantomJS 无法运行 ES6 代码造成的。

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

所以要么

  1. 使用ES5有效代码

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

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

希望这对您有所帮助。

关于angularjs - Grunt Karma PhantomJS - 语法错误 : Unexpected token '>' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44203852/

相关文章:

angularjs - 如何永久运行 Ionic 服务?

javascript - angular-ui-routing 不会改变 View

java - 意式 Espresso 。 NoSuchMethodError UsageTracker

python-3.x - 如何重新运行失败的测试并为 python3 py.test 设置重试

javascript - 如何使用 Mocha、Grunt 测试 NodeJS 应用程序?

javascript - Angular2 无法使用 javascript 导入 FormsModule

javascript - 使用 promise 配置多个功能

reactjs - 如何使用 jest 在 React 功能组件中模拟异步调用

node.js - Node 检查器不会分解对象属性

gruntjs - yeoman grunt-wiredep 不注入(inject)依赖项