Giter VIP home page Giter VIP logo

Comments (14)

bendrucker avatar bendrucker commented on June 12, 2024

For sure! I use browserify-istanbul to instrument code and the plain old karma-coverage reporter to generate reports. Works like a charm.

from karma-browserify.

dsuckau avatar dsuckau commented on June 12, 2024

Do you have an example of a working config?

from karma-browserify.

bendrucker avatar bendrucker commented on June 12, 2024

Sure, here's slightly amended version of the JSON I pass to Karma:

https://gist.github.com/0bdc4c6d0c5a2c6ec203

from karma-browserify.

dsuckau avatar dsuckau commented on June 12, 2024

Uhm, I tried "istanbul" in the transform array, not "browserify-istanbul". Now it really works like a charm. Thank you very much!

from karma-browserify.

bendrucker avatar bendrucker commented on June 12, 2024

That would do it. b.transform can take either functions that return Transform streams or strings that represent a module name that exports the same to be required.

from karma-browserify.

bendrucker avatar bendrucker commented on June 12, 2024

That's distinct, by the way, from the way you configure Karma, where it will automatically load modules that match /^karma\-/ and you can omit the prefix. bro's config options get passed straight off to browserify.

from karma-browserify.

db avatar db commented on June 12, 2024

In that gist - isn't it instrumenting all the test files, and only instrumenting the source files that are required by tests?

from karma-browserify.

bendrucker avatar bendrucker commented on June 12, 2024

@db https://github.com/devongovett/browserify-istanbul#usage

from karma-browserify.

db avatar db commented on June 12, 2024

So the gist is wrong. Thanks for the link.

from karma-browserify.

bendrucker avatar bendrucker commented on June 12, 2024

No it's 100% correct, please read the usage example for the browserify-istanbul that I linked to. The browserify API doesn't provide (nor should it) a way to selectively apply transforms. Your choices are normal (all files you require from your own code) or global (all files required everywhere, including from node_modules). Individual transforms are required to implement their own mechanisms for ignoring files, e.g. not trying to minify json. browserify-istanbul ignores common test directories (test and tests) but you could provide your own globs to exclude __test__, *.spec.js, or whatever you want.

from karma-browserify.

bendrucker avatar bendrucker commented on June 12, 2024

As for not instrumenting files that aren't required by tests, that's true. That's just a function of how browserify works. If there's no require path to your code it's not gonna get bundled.

from karma-browserify.

db avatar db commented on June 12, 2024

My bad.
I didn't suggest selective transforms - thanks for the detail though.

from karma-browserify.

Naxus28 avatar Naxus28 commented on June 12, 2024

I am having issues getting karma-browserify to work with karma-coverage. I have spent a lot of time trying to figure out what is wrong but didn't find a solution.

Here is my .js file (the functions don't do anything, they are just mocks to test code coverage)

// src/js/utilities/form-validation.js

let includedInTest = () => true;

let alsoIncludedInTest = () => true;

let notIncludedInTest = () => true;

let alsoNotIncludedInTest = () => true;

export default {
  validateInput,
  browserifyTest
};

This is my test file

// src/spec/utilities/form-validation.spec.js

import formUtilities from '../../js/utilities/form-validation';

describe('Form validation functions', function () {

  it('Should return "true"', function () {
    expect(formUtilities.includedInTest()).toBe(true);
  });

  it('Should return "true"', function () {
    expect(formUtilities.alsoIncludedInTest()).toBe(true);
  });

});

Finally, this is my karma.conf

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['browserify', 'jasmine-jquery', 'jasmine'],
    files: [
      'bower_components/jquery/dist/jquery.js',
      'bower_components/jquery-validation/dist/jquery.validate.js',
      'src/js/**/*.js',
      'src/spec/**/*.spec.js'
    ],
    exclude: [
      'src/js/index.js'
    ],
    preprocessors: {
      'src/js/**/*.js': ['browserify', 'coverage'],
      'src/spec/**/*.spec.js': ['browserify']
    },
    browserify: {
      debug: true,
      transform: [
        ['babelify', { presets: ['es2015'] }]
      ]
    },
    reporters: ['mocha', 'coverage'],
    mochaReporter: {
      colors: {
        success: 'green',
        info: 'bgBlue',
        warning: 'cyan',
        error: 'bgRed'
      },
      symbols: {
        success: '√',
        info: '#',
        warning: '!',
        error: 'x'
      }
    },

    coverageReporter: {
      instrumenters: { isparta: require('isparta') },
      instrumenter: {
        'src/**/*.js': 'isparta'
      },
      dir: 'coverage',
      subdir: '.',
      reporters: [
        { type: 'html', dir: 'coverage' },
        { type: 'text-summary' }
      ],
      check: {
        global: {
          statements: 90,
          branches: 90,
          functions: 90,
          lines: 90
        },
        each: {
          statements: 90,
          branches: 90,
          functions: 90,
          lines: 90
        }
      },
      watermarks: {
        statements: [50, 75],
        functions: [50, 75],
        branches: [50, 75],
        lines: [50, 75]
      }
    },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false,
    concurrency: Infinity
  });
};

This config yields this result

==== Coverage summary ====
Statements   : 100% ( 1/1 )
Branches     : 100% ( 2/2 )
Functions    : 100% ( 0/0 )
Lines        : 100% ( 1/1 )
=============

Which is obviously wrong.

This line from coverage/index.html reveals only one line is being parsed by karma-coverage:

screen shot 2017-08-18 at 3 39 39 pm

I also tried 'browserify-istanbul' in the transform array (and removed instrumenters from "coverageReport")

  transform: [
        ['babelify', { presets: ['es2015'] }],
        'browserify-istanbul'
      ]

but this generates an error

18 08 2017 15:50:14.617:ERROR [karma]: TypeError: Cannot read property 'start' of undefined
    at /Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/object-utils.js:59:44
    at Array.forEach (native)
    at Object.addDerivedInfoForFile (/Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/object-utils.js:58:37)
    at Collector.fileCoverageFor (/Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/collector.js:94:15)
    at /Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/collector.js:108:30
    at Array.forEach (native)
    at Collector.getFinalCoverage (/Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/collector.js:107:22)
    at checkCoverage (/Users/gferraz/Sites/OAA-Refactor/node_modules/karma-coverage/lib/reporter.js:148:33)
    at /Users/gferraz/Sites/OAA-Refactor/node_modules/karma-coverage/lib/reporter.js:257:32
    at Array.forEach (native)
    at Collection.forEach (/Users/gferraz/Sites/OAA-Refactor/node_modules/karma/lib/browser_collection.js:93:21)
    at /Users/gferraz/Sites/OAA-Refactor/node_modules/karma-coverage/lib/reporter.js:247:16
    at Array.forEach (native)
    at CoverageReporter.onRunComplete (/Users/gferraz/Sites/OAA-Refactor/node_modules/karma-coverage/lib/reporter.js:246:15)
    at Server.<anonymous> (/Users/gferraz/Sites/OAA-Refactor/node_modules/karma/lib/events.js:13:22)
    at emitTwo (events.js:111:20)

Any suggestion on how to fix the config file?

Thank you.

from karma-browserify.

nikku avatar nikku commented on June 12, 2024

@Naxus28 I've spotted a common mistake: Don't add source and tests to the watched files. Watching the tests with coverage should be enough.

from karma-browserify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.