因果报应和CoffeScript和code覆盖 [英] Karma and CoffeScript and Code Coverage

查看:203
本文介绍了因果报应和CoffeScript和code覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的东西可能是错误的因果报应的CoffeeScript code覆盖preprocessor。或者,我不知道我在做什么错了!所以我来找你的计算器。基本上我想设置我的人缘测试套件给我在LCOV格式code覆盖这样我就可以把该关至codeClimate。我有它,所以我的测试套件(用来)工作,但现在我似乎无法得到错误除外从任何事情。我的项目是一个Rails项目和文件的设置是由资产管pprocessed $ P $(即name.js.coffee文件),但如果我将它们包括在覆盖范围preprocessor它似乎preprocess他们得很好,但它引发的每个文件的错误。例如,一些将被处理为一个功能,它会说功能是一个保留字等,所以它仍然认为他们的CoffeeScript文件,我猜,适当使用这种语法。当我尝试使用CoffeeScript的preprocessor虽然我得到的CoffeeScript错误那里不应该有任何对其进行处理。帮助?

I think something may be wrong with the Karma CoffeeScript code coverage preprocessor. Or I'm not sure what I'm doing wrong! So I come to you StackOverflow. Basically I'm trying to setup my karma test suite to give me code coverage in an lcov format so I can ship that off to CodeClimate. I had it so my test suite (used to) work, but now I can't seem to get anything from it except errors. My project is a Rails project and the files are setup to be preprocessed by the asset pipe (ie. name.js.coffee files) but if I include them in the coverage preprocessor it seems to preprocess them just fine, BUT it throws an error on every file. For example, something will be processed to a function and it'll say function is a reserved word, etc. So it still thinks they are coffeescript files I'm guessing and using that syntax appropriately. When I just try to process them using the coffeescript preprocessor though I get coffeescript errors where there shouldn't be any. Help?

下面是错误的:

npm test

> tracktrain@0.1.6 test /Users/bytenel/workspace/tracktrain
> ./node_modules/.bin/karma start --single-run --browsers Firefox

INFO [karma]: Karma v0.12.24 server started at http://localhost:9876/
INFO [launcher]: Starting browser Firefox
ERROR [preprocessor.coverage]: Syntax error on line 7: indentation is ambiguous
4 :     $document.ready(()->
5 :       $scope.loading = false;
6 :     )
7 :   ])
^ :~~~~^
8 : 
9 : 
  at /Users/bytenel/workspace/tracktrain/app/assets/javascripts/controllers/root_controller.js.coffee
ERROR [preprocessor.coverage]: Syntax error on line 66: indentation is ambiguous
63 :                                                        user.selected = false
64 :                                                        $scope.selectedUsers = []
65 :             ]
66 :            )
^^ :~~~~~~~~~~~~^
  at /Users/bytenel/workspace/tracktrain/app/assets/javascripts/controllers/lecture_ctrl.js.coffee
ERROR [preprocessor.coverage]: Syntax error on line 9: indentation is ambiguous
6 :                            user = $resource "/users/:id.json", { id: '@id' }
7 : 
8 :                            user
9 :           ] )
^ :~~~~~~~~~~~~~~^
  at /Users/bytenel/workspace/tracktrain/app/assets/javascripts/factories/user.js.coffee
ERROR [preprocessor.coverage]: Syntax error on line 5, column 1: unexpected '\n' (\u000A)
2 : 
3 : angular.module( 'trackTrainApp' )
4 : .factory( 'OrgTrainUser',
5 :           [ '$resource', ( $resource ) ->
^ :~^
6 :                            user = $resource "/lectures/:id/users.json",
7 :                                             {
8 :                                               id: "@id"
  at /Users/bytenel/workspace/tracktrain/app/assets/javascripts/factories/orgtrainuser.js.coffee
ERROR [preprocessor.coverage]: Syntax error on line 9: indentation is ambiguous
06 :                                                          if $flashNotice[0] && $flashNotice[0].childNodes && $flashNotice[0].childNodes[3].id != 'flash_alert'
07 :                                                            $flashNotice.slideUp animationTime
08 :                                                          return;
09 :                                                       ), 2000;
^^ :~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
10 :    return;)( jQuery );
  at /Users/bytenel/workspace/tracktrain/app/assets/javascripts/flashes.js.coffee
ERROR [preprocessor.coverage]: Syntax error on line 17: indentation is ambiguous
14 :          ajax: $.fn.dataTable.pipeline( {
15 :                                             url: 'lectures/list.json',
16 :                                             pages: 5
17 :                                           }),
^^ :~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
18 :          columns: [
19 :            { data: 'name' },
20 :            { data: 'start_date' },
  at /Users/bytenel/workspace/tracktrain/app/assets/javascripts/lectures.js.coffee
ERROR [preprocessor.coverage]: Syntax error on line 20, column 2: unexpected '(DEDENT)' (\uEFFE)
17 :     return attachmentEntry;
18 : 
19 : 
20 :   $('#add-attachment').on 'click', (event) ->
^^ :~~^
21 :     event.preventDefault
22 :     attachmentEntryCount = $('.attachment-entry').length
23 : 
  at /Users/bytenel/workspace/tracktrain/app/assets/javascripts/training_history.js.coffee
INFO [Firefox 32.0.0 (Mac OS X 10.9)]: Connected on socket dwjoowbAkz3xKEQ4pVCT with id 85121913
WARN [web-server]: 404: /Users/bytenel/workspace/tracktrain/app/assets/javascripts/controllers/lecture_ctrl.js.js
WARN [web-server]: 404: /Users/bytenel/workspace/tracktrain/app/assets/javascripts/factories/user.js.js
WARN [web-server]: 404: /Users/bytenel/workspace/tracktrain/app/assets/javascripts/controllers/root_controller.js.js
WARN [web-server]: 404: /Users/bytenel/workspace/tracktrain/app/assets/javascripts/factories/orgtrainuser.js.js
WARN [web-server]: 404: /Users/bytenel/workspace/tracktrain/app/assets/javascripts/controllers/root_controller.js.js
WARN [web-server]: 404: /Users/bytenel/workspace/tracktrain/app/assets/javascripts/controllers/lecture_ctrl.js.js
WARN [web-server]: 404: /Users/bytenel/workspace/tracktrain/app/assets/javascripts/factories/user.js.js
WARN [web-server]: 404: /Users/bytenel/workspace/tracktrain/app/assets/javascripts/factories/orgtrainuser.js.js
Firefox 32.0.0 (Mac OS X 10.9) ERROR
  SyntaxError: missing ; before statement
  at /Users/bytenel/workspace/tracktrain/spec/javascripts/controllers/leture_ctrl_spec.coffee:1

Firefox 32.0.0 (Mac OS X 10.9) ERROR
  SyntaxError: missing ; before statement
  at /Users/bytenel/workspace/tracktrain/spec/javascripts/factories/org_train_user_factory_spec.coffee:1

Firefox 32.0.0 (Mac OS X 10.9) ERROR
  SyntaxError: missing ; before statement
  at /Users/bytenel/workspace/tracktrain/spec/javascripts/factories/user_factory_spec.coffee:1


npm ERR! weird error 1
npm ERR! not ok code 0

下面是我的karma.conf.js文件:

Here is my karma.conf.js file:

// Karma configuration
// Generated on Sat Aug 02 2014 00:12:00 GMT-0600 (MDT)

module.exports = function (config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: __dirname,


        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['jasmine'],

        plugins: [
            'karma-jasmine',
            'karma-firefox-launcher',
            'karma-coffee-preprocessor',
            'karma-coverage'
        ],

        // list of files / patterns to load in the browser
        // TODO: this is not requiring files correctly, need to fix TR-335
        files: [
            'vendor/assets/javascripts/karma-jasmine/lib/jasmine.js',
            'vendor/assets/javascripts/karma-jasmine/src/adapter.js',
            'vendor/assets/javascripts/angular/angular.js',
            'vendor/assets/javascripts/angular-resource.min.js',
            'vendor/assets/javascripts/angular-mocks/angular-mocks.js',
            'vendor/assets/javascripts/jquery-2.1.1.min.js',
            'vendor/assets/javascripts/jquery.dataTables.min.js',
            'vendor/assets/javascripts/d3/d3.js',
            'vendor/assets/javascripts/nvd3/nv.d3.js',
            'vendor/assets/javascripts/angularjs-nvd3-directives/dist/angularjs-nvd3-directives.js',
            'vendor/assets/javascripts/ui-bootstrap-tpls-0.11.0.min.js',
            'vendor/assets/javascripts/*.*js',
            'spec/javascripts/*_spec.js*',
            'spec/javascripts/**/*_spec.js*',
            'app/assets/javascripts/app.js.coffee',
            'app/assets/javascripts/controllers/root_controller.js.coffee',
            'app/assets/javascripts/controllers/lecture_ctrl.js.coffee',
            'app/assets/javascripts/factories/user.js.coffee',
            'app/assets/javascripts/factories/orgtrainuser.js.coffee',
            {   pattern: 'app/assets/javascripts/*.js.*',
                watched: true,
                included: false,
                served: false },
            'spec/javascripts/**/*_spec.coffee'
        ],


        // list of files to exclude
        exclude: [
            'app/assets/javascripts/application.js.coffee.erb'
        ],


        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors: {
            'app/**/*.*.coffee': ['coverage']
        },

        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress', 'coverage'],

        coverageReporter: {
            type : 'lcovonly',
            dir : 'coverage/',
            subdir: 'javascript'
        },

        'scripts': {
            'test': './node_modules/.bin/karma start --single-run --browsers Firefox'
        },

        // web server port
        port: 9876,


        // enable / disable colors in the output (reporters and logs)
        colors: true,


        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,


        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,


        // start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        browsers: ['Firefox'],


        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false
    });
};

这是一个例子规范和js文件:

And here is an example spec and js file:

describe 'OrgTrainUser', () ->
                           factory = undefined
                           $httpBackend = undefined
                           beforeEach module( 'trackTrainApp' )
                           beforeEach inject( ( $injector ) ->
                                                $httpBackend = $injector.get( '$httpBackend' );
                                                factory = $injector.get( 'OrgTrainUser' );
                                            )
                           it 'queries with username',
                              () ->
                                $httpBackend.expectGET( '/lectures/123/users.json' )
                                .respond( [ {
                                            id: '123',
                                            first_name: 'test',
                                            last_name: 'bob'
                                          } ] )
                                result = factory.query( { id: '123' } )

                                $httpBackend.flush()

                                expect( result[0].id ).toEqual( '123' )

'use strict'

angular.module( 'trackTrainApp' )
.factory( 'OrgTrainUser',
          [ '$resource', ( $resource ) ->
                           user = $resource "/lectures/:id/users.json",
                                            {
                                              id: "@id"
                                            },
                                            {
                                              query:
                                                method: "GET"
                                                isArray: true
                                              show:
                                                method: 'GET'
                                                isArray: true
                                            }

                           user
          ] )

和相关的错误:

ERROR [preprocessor.coverage]: Syntax error on line 9: indentation is ambiguous
06 :                                                          if $flashNotice[0] && $flashNotice[0].childNodes && $flashNotice[0].childNodes[3].id != 'flash_alert'
07 :                                                            $flashNotice.slideUp animationTime
08 :                                                          return;
09 :                                                       ), 2000;
^^ :~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
10 :    return;)( jQuery );

难道在这些文件中使用严格模式涉及到我吗?

Could it be related to me using strict mode in these files?

推荐答案

噶code覆盖率由项目供电。 编译和文书的CoffeeScript code。通过使用的编译器是不是标准的CoffeeScript编译器,而是使用非100%兼容的编译器叫的CoffeeScript -redux

Karma code coverage is powered by brick project. brick compiles and instruments the coffeescript code. The compiler used by brick is not the standard coffeescript compiler, instead, brick uses a non 100% compatible compiler called coffeescript-redux

我们的项目是完全由CoffeeScript的1.8编写,但有很多的错误,当与终极版

Our project is perfectly compiled by coffeescript 1.8 but has a lot of errors when compiled with redux

ibrik 项目和终极版编译器已经在一段时间相对不活跃,因此,我们根本都忘了我们的尝试让我们code的封面。也许在未来,随着CoffeeScript的,终极版的重新启动,我们可以再次使用 karma- code覆盖插件。

The ibrik project and the redux compilers have been relatively inactive in a while, thus we simply forgot our attempts to get the cover of our code. Maybe in a future, with the relaunch of coffeescript-redux, we can use again the karma-code-coverage plugin.

---------更新--------------

--------- UPDATE --------------

该ibrik项目已更新为使用coffescript,但果报报道还没有。你能砍你的package.json得到它的工作,或等待因缘覆盖修复它,但我有我的项目中的两个人缘配置文件,先不覆盖(快 - 3秒)运行,二来覆盖的JavaScript(小慢 - 10秒)

The ibrik project has been updated to use coffescript, but karma-coverage not yet. You can hack your package.json to get it working or wait to karma-coverage fix it, however I have two karma config files in my project, first to run without coverage (fast - 3 seconds) and second to coverage the javascript (little slower - 10 seconds)

检查karma-coverage.conf.coffee

Check karma-coverage.conf.coffee

wiredep = require('wiredep')
path = require('path')

module.exports = (config) ->
  config.set
    frameworks: ['jasmine']

    files: wiredep(devDependencies: true)['js'].map (file) ->
      path.relative(process.cwd(), file)
    .concat [
      '.tmp/common/app.js'
      '.tmp/common/services/helpers/**/*.js'
      '.tmp/common/**/*.js'
      '.tmp/app/**/*.js'
      'src/common/**/*.spec.coffee'
      'src/app/**/*.spec.coffee'
      'test/spec/**/*.coffee'
      # directives templates
      'src/common/directives/**/*.html'
      'src/app/components/**/*.html'
      'bower_components/angular-strap/src/**/*.tpl.html'
      # fixtures
      {
        pattern: 'test/fixtures/**/*.json'
        watched: true
        served: true
        included: false
      }
    ]

    exclude: []

    preprocessors:
      'src/app/components/**/*.html': 'ng-html2js'
      'bower_components/angular-strap/src/**/*.tpl.html': 'ng-html2js'
      'src/common/**/*.spec.coffee': 'coffee'
      'src/app/**/*.spec.coffee': 'coffee'
      'test/spec/**/*.coffee': 'coffee'
      '.tmp/**/*.js': 'coverage'

    reporters: ['progress', 'html', 'coverage']

    ngHtml2JsPreprocessor:
      stripPrefix: 'src/'

    coverageReporter:
      type: 'html',
      dir: '.tmp/test/coverage/'

    htmlReporter:
      outputDir: '.tmp/test/html/'

    coffeePreprocessor:
      options:
        bare: true
        sourceMap: true
      transformPath: (path) ->
        return path.replace(/.js$/, '.coffee')

    port: 9876
    colors: true
    logLevel: config.LOG_INFO
    autoWatch: true
    browsers: ['PhantomJS']
    singleRun: false

这篇关于因果报应和CoffeScript和code覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆