karma-runner / karma-coverage

A Karma plugin. Generate code coverage.
MIT License
770 stars 247 forks source link

Karma coverage empty (no data) with Rails/sprockets #337

Open rhymes opened 7 years ago

rhymes commented 7 years ago

Hi,

Karma seems to work fine running the tests but I can't get any coverage output. I am using a Rails app with yarn installed modules, sprockets (asset pipeline) and karma + phantomjs.

Through Google (and this 4 year old article) I came up with the following karma.conf.js:

module.exports = function(config) {
  config.set({
    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '.',

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

    // // list of files / patterns to load in the browser
    files: [
      'http://localhost:3000/assets/application.js',
      {
        pattern: 'app/assets/javascripts/**/*.js',
        watched: true,
        included: false,
        served: false
      },
      'test/javascripts/**/*_test.js'
    ],

    // list of files to exclude
    exclude: [
    ],

    // preprocess matching files before serving them to the browser
    // available preprocessors:
    // https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      // source files, that you wanna generate coverage for
      // do not include tests or libraries
      // (these files will be instrumented by Istanbul)
      'http://localhost:3000/assets/application.js': ['coverage'],
      'app/assets/javascripts/**/*.js': ['coverage']
    },

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

    // 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_DEBUG,

    // 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: ['PhantomJS'],

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

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity,

    coverageReporter: {
      dir: 'coverage-js',
      reporters: [
        { type: 'html', subdir: 'default' },
        { type: 'text' }
      ]
    }
  })
}

I've tried with both:

    preprocessors: {
      'app/assets/javascripts/**/*.js': ['coverage']
    }

and

    preprocessors: {
      'http://localhost:3000/assets/application.js': ['coverage'],
    }

but nothing. Even both at the same time.

application.js is the file that Rails bundles with all the JS dependencies and which karma picks up fine to run the tests.

17 08 2017 09:47:06.703:WARN [karma]: No captured browser, open http://localhost:9876/
17 08 2017 09:47:06.720:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
17 08 2017 09:47:06.721:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
17 08 2017 09:47:06.728:INFO [launcher]: Starting browser PhantomJS
17 08 2017 09:47:07.915:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket bIcFbM8QsDpIbNgjAAAA with id 52351923
........
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 8 of 8 SUCCESS (0.015 secs / 0.029 secs)
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|

screen shot 2017-08-17 at 9 47 47 am

As you can see from the debug output, the preprocessor is actually seeing the project files:

17 08 2017 09:49:03.018:DEBUG [plugin]: Loading karma-* from /project/node_modules
17 08 2017 09:49:03.023:DEBUG [plugin]: Loading plugin /project/node_modules/karma-coverage.
17 08 2017 09:49:03.234:DEBUG [plugin]: Loading plugin /project/node_modules/karma-jasmine.
17 08 2017 09:49:03.235:DEBUG [plugin]: Loading plugin /project/node_modules/karma-phantomjs-launcher.
17 08 2017 09:49:03.240:DEBUG [plugin]: Loading plugin /project/node_modules/karma-sprockets-mincer.
17 08 2017 09:49:03.487:DEBUG [web-server]: Instantiating middleware
17 08 2017 09:49:03.496:DEBUG [reporter]: Trying to load reporter: coverage
17 08 2017 09:49:03.497:DEBUG [reporter]: Trying to load color-version of reporter: coverage (coverage_color)
17 08 2017 09:49:03.497:DEBUG [reporter]: Couldn't load color-version.
17 08 2017 09:49:03.536:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/activeadmin/jsoneditor.js".
17 08 2017 09:49:03.560:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/activeadmin/selectize.js".
17 08 2017 09:49:03.562:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/analytics.js".
17 08 2017 09:49:03.568:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/application.js".
17 08 2017 09:49:03.571:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/analytics/app_versions.js".
17 08 2017 09:49:03.578:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/analytics/geo.js".
17 08 2017 09:49:03.581:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/base.js".
17 08 2017 09:49:03.583:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/bee.js".
17 08 2017 09:49:03.606:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/data_table.js".
17 08 2017 09:49:03.613:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/date_time_picker.js".
17 08 2017 09:49:03.617:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/image_crop.js".
17 08 2017 09:49:03.623:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/image_upload.js".
17 08 2017 09:49:03.635:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/landing_page.js".
17 08 2017 09:49:03.642:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/main.js".
17 08 2017 09:49:03.660:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/map.js".
17 08 2017 09:49:03.670:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/preview.js".
17 08 2017 09:49:03.699:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/query_builder.js".
17 08 2017 09:49:03.709:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/static_list.js".
17 08 2017 09:49:03.713:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/notification_templates/utils.js".
17 08 2017 09:49:03.715:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/business/utils.js".
17 08 2017 09:49:03.718:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/devices.js".
17 08 2017 09:49:03.720:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/landing_pages/edit.js".
17 08 2017 09:49:03.722:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/landing_pages/index.js".
17 08 2017 09:49:03.725:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/landing_pages/new.js".
17 08 2017 09:49:03.727:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/main.js".
17 08 2017 09:49:03.728:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/notification_template_landing_page.js".
17 08 2017 09:49:03.730:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/notification_templates/index.js".
17 08 2017 09:49:03.732:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/notification_templates/new_create.js".
17 08 2017 09:49:03.733:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/push_notifications/index.js".
17 08 2017 09:49:03.736:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/push_notifications/new_edit_create.js".
17 08 2017 09:49:03.738:DEBUG [preprocessor.coverage]: Processing "/project/app/assets/javascripts/touch_points.js".
17 08 2017 09:49:03.742:DEBUG [karma]: List of files has changed, trying to execute
17 08 2017 09:49:03.742:WARN [karma]: No captured browser, open http://localhost:9876/
17 08 2017 09:49:03.746:DEBUG [watcher]: Watching "/project/app/assets/javascripts"
17 08 2017 09:49:03.746:DEBUG [watcher]: Watching "/project/test/javascripts"
17 08 2017 09:49:03.756:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
17 08 2017 09:49:03.760:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
17 08 2017 09:49:03.782:INFO [launcher]: Starting browser PhantomJS
17 08 2017 09:49:03.782:DEBUG [temp-dir]: Creating temp dir at /var/folders/qc/c_sksnns54q13ympksl8x4h00000gp/T/karma-12582815
17 08 2017 09:49:03.786:DEBUG [launcher]: /usr/local/bin/phantomjs /var/folders/qc/c_sksnns54q13ympksl8x4h00000gp/T/karma-12582815/capture.js
17 08 2017 09:49:04.985:DEBUG [web-server]: serving: /project/node_modules/karma/static/client.html
17 08 2017 09:49:04.995:DEBUG [web-server]: serving: /project/node_modules/karma/static/karma.js
17 08 2017 09:49:05.051:DEBUG [karma]: A browser has connected on socket 6nWs9u3NKcvOYYWkAAAA
17 08 2017 09:49:05.070:DEBUG [web-server]: upgrade /socket.io/?EIO=3&transport=websocket&sid=6nWs9u3NKcvOYYWkAAAA
17 08 2017 09:49:05.097:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket 6nWs9u3NKcvOYYWkAAAA with id 12582815
17 08 2017 09:49:05.097:DEBUG [launcher]: PhantomJS (id 12582815) captured in 1.336 secs
17 08 2017 09:49:05.098:DEBUG [karma]: All browsers are ready, executing
17 08 2017 09:49:05.098:DEBUG [karma]: Captured 1 browsers
17 08 2017 09:49:05.102:DEBUG [phantomjs.launcher]:

17 08 2017 09:49:05.104:DEBUG [middleware:karma]: custom files null null null
17 08 2017 09:49:05.104:DEBUG [middleware:karma]: Serving static request /context.html
17 08 2017 09:49:05.106:DEBUG [web-server]: serving: /project/node_modules/karma/static/context.html
17 08 2017 09:49:05.109:DEBUG [middleware:source-files]: Requesting /base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?a462fdca9c003f57f07430376abd0e8850056f99 /
17 08 2017 09:49:05.110:DEBUG [middleware:source-files]: Fetching /project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
17 08 2017 09:49:05.111:DEBUG [web-server]: serving (cached): /project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
17 08 2017 09:49:05.113:DEBUG [middleware:source-files]: Requesting /base/node_modules/karma-jasmine/lib/boot.js?945a38bf4e45ad2770eb94868231905a04a0bd3e /
17 08 2017 09:49:05.117:DEBUG [middleware:source-files]: Fetching /project/node_modules/karma-jasmine/lib/boot.js
17 08 2017 09:49:05.117:DEBUG [middleware:source-files]: Requesting /base/node_modules/karma-jasmine/lib/adapter.js?7a813cc290d592e664331c573a1a796192cdd1ad /
17 08 2017 09:49:05.117:DEBUG [middleware:source-files]: Fetching /project/node_modules/karma-jasmine/lib/adapter.js
17 08 2017 09:49:05.118:DEBUG [middleware:source-files]: Requesting /base/test/javascripts/business/utils_test.js?34c8090e5c6dfc9b61c71a496b9af750aae4ae9a /
17 08 2017 09:49:05.118:DEBUG [middleware:source-files]: Fetching /project/test/javascripts/business/utils_test.js
17 08 2017 09:49:05.118:DEBUG [web-server]: serving (cached): /project/node_modules/karma-jasmine/lib/boot.js
17 08 2017 09:49:05.119:DEBUG [web-server]: serving (cached): /project/node_modules/karma-jasmine/lib/adapter.js
17 08 2017 09:49:05.120:DEBUG [web-server]: serving (cached): /project/test/javascripts/business/utils_test.js
17 08 2017 09:49:05.121:DEBUG [web-server]: serving: /project/node_modules/karma/static/context.js
........
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 8 of 8 SUCCESS (0.015 secs / 0.025 secs)
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|

17 08 2017 09:49:05.469:DEBUG [coverage]: Writing coverage to /project/coverage-js/default

I had a look in https://github.com/karma-runner/karma-coverage/issues/87 but I couldn't come up with a solution.

These are my package.json devDependencies (the app is a regular web site with jQuery and some JS business logic)

  "devDependencies": {
    "eslint": "^4.4.1",
    "hot-builder": "^1.0.4",
    "istanbul": "^0.4.5",
    "jasmine-core": "^2.7.0",
    "karma": "^1.7.0",
    "karma-coverage": "^1.1.1",
    "karma-jasmine": "1.1.0",
    "karma-phantomjs-launcher": "^1.0.4"
  },

Any ideas?

Thank you!

richistron commented 6 years ago

I'm having the same issue :(

wkaskie commented 5 years ago

I'm also having the exact same issue.

johnjbarton commented 5 years ago

The coverage mode causes the preprocessor to add lines to count usage, causes the test run to record the counts, and writes the data out. Just investigate each step to see where you go off the track.

wkaskie commented 5 years ago

I just fixed it. For me, the issue was in the preprocessors section. The way that I fixed it was to repeat the same code that generates the "files" section of my config. This code is the part that uses sprockets. Once I did that, it worked like a charm. The bad news is that I'm < 50% coverage everywhere.

For me, the problem was that I wasn't including all of the files. For you, it seems that you need to add this line:

'test/javascripts/**/*_test.js': ['coverage']

to the end of your preprocessors list.