Coder Social home page Coder Social logo

grunt-blanket-mocha's Introduction

grunt-blanket-mocha

Headless Blanket.js code coverage and Mocha testing via PhantomJS

Wat?

Other plugins look similar, but are different in that they:

  • Only test server-side code
  • Create new instrumented copies of your source code for coverage detection
  • Generate coverage reports in HTML or JSON formats requiring a separate step to parse and evaluate coverage
  • Do not enforce coverage thresholds, but just report on it

This plugin, however:

  • Runs client-side mocha specs
  • Performs code coverage "live" using BlanketJS, without creating separate instrumented copies
  • Reports coverage info directly to the Grunt task
  • Will fail the build if minimum coverage thresholds are not defined

Parent Plugin

This plugin is based on kmiyashiro/grunt-mocha v1.0.2 and supports most of the configurations of that plugin as of that version. Please see that repo for more options on configuration.

Changes from the upstream plugin will be merged periodically.

Getting Started

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-blanket-mocha --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-blanket-mocha');

Dependencies

  • Blanket.js (tested with v1.1.6)
  • Mocha (tested with v2.4.5)

The "blanket_mocha" task

Setup

Demo

See the example and example-requires directories for a fully-working examples of the setup, including some of the scaffolding required to get all the pieces to fit together. The README in that directory will walk you through it.

Gruntfile

In your project's Gruntfile, add a section named blanket_mocha to the data object passed into grunt.initConfig().

grunt.initConfig({
  blanket_mocha: {
    test: {
      src: ['specs/test.html'],
      options : {
          threshold : 70
      }
    }
  }
})

Use the all param to specify where your mocha browser spec HTML file lives. This works the same way as it does in the base grunt-mocha plugin.

NOTE: Be sure to include the blanketJS script tag in your test html file

Blanket Adapter

To allow Blanket to communicate with the parent Grunt process, add this snippet in your test HTML, after all the other scripts:

<script>
    if (window.PHANTOMJS) {
        blanket.options("reporter", "../node_modules/grunt-blanket-mocha/support/grunt-reporter.js");
    }
</script>

NOTE: The above path is assuming that the specs are being run from a directory one deeper than the root directory. Adjust the path accordingly.

NOTE 2: The conditional if (window.PHANTOMJS) statement is there because of the hacky way that messages are passed between an HTML page and the PhantomJS process (using alerts). Without this condition, you would get bombarded with alert messages in your in-browser mocha report.

BlanketJS HTML Report

If you want to see blanketJS coverage reports in the browser as well (useful for visually scanning which lines have coverage and which do not) include this snippet it in your test html blanket and mocha.

<script type="text/javascript" src="../node_modules/grunt-blanket-mocha/support/mocha-blanket.js"></script>

NOTE: The above path is assuming that the specs are being run from a directory one deeper than the root directory. Adjust the path accordingly.

Options

options.threshold

Type: Number Default value: 60

The minimum percent coverage per-file. Any files that have coverage below this threshold will fail the build. By default, only the failing files will be output in the console. To show passing files as well, use the grunt --verbose option.

options.moduleThreshold

Type: Number Default value: undefined

The minimum percent coverage per-module. Any modules that have coverage below this threshold will fail the build. Both passing and failing module statistics will be shown in the output.

This option requires that the modulePattern property is also set (see below).

options.modulePattern

Type: RegEx Default value: undefined

A regular expression defining how to extract a module name from the path of a covered file. The regular expression should include a single parenthetical expression which will be matched as the module name. For example, to define the module name as the text in between the first two slashes, you could use:

modulePattern: "./(.*?)/"

options.globalThreshold

Type: Number Default value: undefined

The minimum percent coverage overall, averaged for all files. An average coverage percentage below this value will fail the build.Both passing and failing module statistics will be shown in the output.

options.excludedFiles

Type: Array Default value: undefined

List filenames that need to be excluded. This will inform the Grunt Task to not mark these files as failed. The result will be printed as, SKIP: [..%] filename

Example:

excludedFiles: [
  "./src/my/file1.js",
  "./src/my/project/file2.js"
]

options.customThreshold

Type: Object Default value: undefined

List filenames that should have their own special threshold. This is useful for the case when there are a few files with poor coverage in your project, and you don't want them to hold you back from enforcing an overall high threshold. Or you may have certain files that you want to hold to a higher standard than others, using a higher threshold.

Example:

customThreshold: {
  "./src/my/file1.js" : 33,
  "./src/my/project/file2.js" : 45
}

options.customModuleThreshold

Type: Object Default value: undefined

List module names that should have their own special threshold. This is useful for the case when there are a few modules with poor coverage in your project, and you don't want them to hold you back from enforcing an overall high threshold. Or you may have certain modules that you want to hold to a higher standard than others, using a higher threshold.

Example:

customModuleThreshold: {
  "users" : 60,
  "security" : 90
}

Command Line Options

threshold

Override the threshold specified in the Gruntfile.

For example, if you wanted to test your files using a 90% threshold, and the Gruntfile had a different threshold specified, you could override it like so:

grunt --threshold=90

moduleThreshold

Override the moduleThreshold specified in the Gruntfile.

For example, if you wanted to test your files using a 90% module threshold, and the Gruntfile had a different module threshold specified, you could override it like so:

grunt --moduleThreshold=90

globalThreshold

Override the globalThreshold specified in the Gruntfile.

For example, if you wanted to test your files using a 90% global threshold, and the Gruntfile had a different global threshold specified, you could override it like so:

grunt --globalThreshold=90

excludedFiles

List the files to be excluded as an array. Example, grunt --excludedFiles=["./src/my/file1.js", "./src/my/project/file2.js"]

grep

Only run test specs that match a certain pattern.

For example, if you only wanted to run specs that match the word "login" you could run:

grunt --grep="login"

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

0.4.1

Released 17 June 2014

  • Better filename matching, and other misc fixes.

0.4.0

Released 2 February 2014

  • Pull upstream changes from grunt-mocha 0.4.10

0.3.4

Released 31 January 2014

  • Fix compatibility with latest mocha versions, and some reporter issues

0.3.3

Released 12 November 2013

  • Bump dependencies for PhantomJS, Mocha, JSHint

0.3.2

Released 30 October 2013

  • Add ability to define custom thresholds for files and modules.
  • Fix bug where module thresholds were not being reported or enforced correctly.

0.3.1

Released 6 September 2013

  • Fix keyword listing so the plugin shows up in Grunt plugins repo

0.3.0

Released 25 August 2013

  • Add ability to manually exclude files(shows as 'skipped')

0.2.0

Released 1 August 2013

  • Fix issue where failing mocha test did not fail the build

0.1.3

Released 31 July 2013

  • Initial release

grunt-blanket-mocha's People

Contributors

ambar avatar bartvds avatar byk avatar e7h4n avatar gamtiq avatar geekdave avatar givankin avatar iammerrick avatar juriejan avatar kmiyashiro avatar michaelxavier avatar naganowl avatar nschonni avatar okcoker avatar paulirish avatar peter-mouland avatar philschatz avatar puigcerber avatar rash-mi avatar rayshih avatar rmurphey avatar rohni avatar romanchyla avatar salange avatar sboudrias avatar sessio avatar sindresorhus avatar strycore avatar studiomohawk avatar tony avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-blanket-mocha's Issues

Add blanket as a dependency

The README mentions that this package has a dependency upon an unreleased version Blanket (v 1.1.5), however that version has now been released and so blanket should be added as decency to package.json.

Cannot set Phantom.JS page settings

Presently, there's no way to switch-off web-security and allow cross origin requests during unit testing.

Phantom.JS offers parameters on the page object for allowing these calls. grunt-mocha recently merged a pull request allowing to set these parameters in the grunt file. Please consider merging these changes into main.js:

kmiyashiro/grunt-mocha#133

Undefined coverage result possibly caused by browser security

I can seem to get this plugin working. My simple test and coverage setup works in a Google Chrome browser with the --allow-file-access-from-files flag set to true. When the flag is not set it returns undefined for the coverage percentage, which seems to be what's happening when I try to run the task:

Running "blanket_mocha:all" (blanket_mocha) task
Testing: tests/all.html

  โ€ค

  1 passing (107ms)

FAIL [100% = undefined% ] : file:///home/juriejan/Projects/grunt-book/tests/src/one.js (2 / 2)

Per-File Coverage Results: (undefined% minimum)
FAIL : 1/1 files failed coverage

I've tried turning off web security at both the CLI and page levels for the PhantomJS process that get's spawned, but the issue persists.

Since there's nothing in the documentation regarding this, I assume that I'm might b missing something fairly obvious.

Fix JSHint errors

$ grunt jshint
Running "jshint:all" (jshint) task
Linting tasks/blanket_mocha.js...ERROR
[L393:C23] W004: 'failMsg' is already defined.
          var failMsg = stats.failures + '/' + stats.tests + ' tests failed (' +

Warning: Task "jshint:all" failed. Use --force to continue.

Aborted due to warnings.

server task

Hi @geekdave,

Well I started to work on a tests platform and quickly went "off course". Specifically, I started to notice that I was going to be implementing something that wanted to easily switch the configuration of the server that serves the test html in order to give the plugin a workout.

Having an easily modified (from grunt) server for tests was also something that I needed for my own stuff -- because I wanted to be able to control which files are tested and which tests are run and stuff like that.

I also harkened back to the part where I was trying to set up mocha and chai and the reporter and remembered that it felt like more of a hurdle to do that than it was to install the grunt-blanket-mocha plugin.

So.... I've implemented a blanket_mocha_server task. Basically its goal is to bundle mocha.js, mocha.css, blanket.js and chai.js, and to let you specify the system under test files and test files and go. It's quite configurable but if you do nothing special you get a runner that supports chai's should and expect. Here's a simple sample configuration:

  grunt.loadNpmTasks('grunt-blanket-mocha');
  grunt.loadNpmTasks('grunt-blanket-mocha-server');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.registerTask('test', ['blanket_mocha_server', 'blanket_mocha', 'watch']);

  grunt.initConfig({
    blanket_mocha_server: {

      server: {
        options: {
          port: 3001,
          testConfig: {
            htmlFile: './test/test-runner.html',
            blanketOptions: {
              'data-cover-only': '//funcs/',
            },
            sutFiles: [
              '/funcs1.js',
              '/funcs2.js'
            ],
            testFiles: [
              '/tests.js'
            ],
          }
        }
      }
    },

    blanket_mocha: {

      unit: {
        options: {
          urls: [ 'http://localhost:3001/test/test-runner.html' ],
          reporter: 'Nyan',
          threshold: 80,
          moduleThreshold: 80
        }
      }
    },

    watch: {
    }

  });

And here are the (overrideable) default options (as in, you don't need to use chai and you can replace the files that are served to your hearts content, etc.):

      var defaultOptions = {
        testConfig: {
          htmlFile:               null,
          sutFiles:               null,
          testFiles:              null,
          blanketOptions:         null,
          blanketPhantomOptions:  null,
          mochaPath:              '/node_modules/grunt-blanket-mocha-server/tasks/lib/mocha.js',
          mochaCssPath:           '/node_modules/grunt-blanket-mocha-server/tasks/lib/mocha.css',
          mochaSetupScript:       'mocha.setup(\'bdd\');',
          blanketPath:            '/node_modules/grunt-blanket-mocha-server/tasks/lib/blanket.js',
          gruntReporterPath:      '/node_modules/grunt-blanket-mocha/support/grunt-reporter.js',
          mochaBlanketPath:       '/node_modules/grunt-blanket-mocha/support/mocha-blanket.js',
          assertionLibs:          ['/node_modules/grunt-blanket-mocha-server/tasks/lib/chai.js'],
          assertionsSetupScript:  'window.expect = chai.expect; var should = chai.should();',
          runnerTemplate:         './node_modules/grunt-blanket-mocha-server/tasks/lib/test-runner-template.html'
        }
      };

So my question to you is: do you have an interest in bundling this task with your plugin? If so, I can bundle it up in a fork of grunt-blanket-mocha.

Alternatively I can publish it as its own plugin (which is how it's currently configured) -- in which case, do you have a problem with making it a dev dependency and using it in the test suite that I promised to take a stab at?

Either way, I'm certainly open to discussing changes after you've had a chance to see the code.

Please let me know which way you're leaning (no commitments) so I can post it to github in a way that makes sense. Obviously the paths to the files would change if the task is going to live in your plugin.

Support testing multiple modules in parallel

For large projects with lots of long-running tests, it's a time saver to test each module in a parallel process.

grunt-blanket-mocha should then be able to roll up all the results from the module-specific tests, and report on all the results at the end.

Depends on #14 to capture the per-module results as they are run.

Seeking New Ownership!

This project is no longer actively maintained. If you are interested in taking it over, please comment below. Highlight any contributions you have made to the project, as well as any other examples of your open source work. Thanks!

Add options to configure Growl

There should be a way to toggle when Growl triggers, specifically the ability to turn it off when tests pass.

It seems noisy to have it go off every time the tests pass, so it would be nice if it was configurable.

Support File Output of Coverage Results

As a next step to incorporating grunt-blanket-mocha into my build, I'd like to be able to easily output the coverage results into a file in a format that's somehow digestible by Jenkins. There are a number of options out there and I'm personally not very informed on the landscape of reporting options, but this would be pretty fantastic functionality to add.

Error: You must call blanket.setupCoverage() first.

Hello,
I'm getting this error when running grunt blanket_mocha.

Error: You must call blanket.setupCoverage() first.
      at http://10.2.1.103:8000/test/runner/:2098
      at http://10.2.1.103:8000/test/runner/:2101
      at /Users/krzysztof/dev/TestProject/node_modules/grunt-blanket-mocha/phantomjs/bridge.js:61
      at http://10.2.1.103:8000/test/runner/mocha.js:588
      at http://10.2.1.103:8000/test/runner/mocha.js:4815
      at done (http://10.2.1.103:8000/test/runner/mocha.js:4298)
      at callFn (http://10.2.1.103:8000/test/runner/mocha.js:4341)
      at http://10.2.1.103:8000/test/runner/mocha.js:4329
      at http://10.2.1.103:8000/test/runner/mocha.js:4726
      at http://10.2.1.103:8000/test/runner/mocha.js:4817
      at next (http://10.2.1.103:8000/test/runner/mocha.js:4651)
      at http://10.2.1.103:8000/test/runner/mocha.js:4661
      at next (http://10.2.1.103:8000/test/runner/mocha.js:4599)
      at http://10.2.1.103:8000/test/runner/mocha.js:4628
      at timeslice (http://10.2.1.103:8000/test/runner/mocha.js:5759)

My grunt configuration is:

blanket_mocha: {
            test: {
                options: {
                    run: true,
                    log: true,
                    logErrors: true,
                    urls: [config.path + "/test/runner/"]
                }
            }
        }

Function.prototype.bind is missing

Hi,
I run my tests in phantomjs (1.9.7), some of the calls are doing:

var f = function() {}
return f.bind(object)

suddenly, this started failing when I include blanket.js (1.1.6) to get coverage reports -

grunt blanket_mocha

I believe this is because the code is using old version of phantojms (1.9.12) - for the moment, I solved it by including es5-shim in my coverage tests (https://github.com/es-shims/es5-shim) but hopefully you will be able to upgrade to newer phantomjs

Source file not covered

I'm trying to set up blanket for my mocha tests. Everything seems to work fine except that the coverage report is wrong : only the flle loading is covered but not the calls made by the tests.
You could have a look on my Gruntfile and on one of the test runner. The project is public as well (on branch coverage-blanket).

Bump version and Publish

Humble request: can you publish the changes made since February to NPM as a release? I've been pulling in the repo to get the new functionality (better filename matching).

PhantomJS timeout triggered by ?: expression

An issue is seen when running blanket_mocha:frontend:

      this.attributes =
        _.extend(this.attributes || {},
          {serviceUrl: url + (url.indexOf("?") >= 0 ? "&" : "?") + postData});

results in PhantomJS error:
Warning: PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue.

Re-writing the expression clears the issue:

      var url = this.url(),
        postData = $.param(cartObject) + (postDataString ? postDataString : ""),
        index = url.indexOf("?"),
        separator = index < 0 ? "?" : "&";
      this.attributes = _.extend(this.attributes || {}, {
        serviceUrl: url + separator + postData
      });

Emit events for coverage thresholds

Currently, when coverage thresholds are not met the terminal outputs the failure result with grunt.log.

It would be ideal to have this linked to an event so something like Growl can hook into it for notification.

Incompatible with Mocha 1.1.4+

The following exception prevents tests running:

[Error] TypeError: 'undefined' is not a function (evaluating 'self.suiteURL(suite)')
    (anonymous function) (mocha.js, line 2585)
    emit (mocha.js, line 588)
    runSuite (mocha.js, line 4720)
    next (mocha.js, line 4725)
    next (mocha.js, line 4663)
    runTests (mocha.js, line 4699)
    (anonymous function) (mocha.js, line 4737)
    next (mocha.js, line 4514)
    (anonymous function) (mocha.js, line 4538)
    timeslice (mocha.js, line 5531)

TestRunner:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Tests</title>
    <link rel="stylesheet" href="../node_modules/grunt-blanket-mocha/node_modules/mocha/mocha.css" type="text/css" media="screen" charset="utf-8">
  </head>
<body>
    <!-- Required for browser reporter -->
    <div id="mocha"></div>

    <!-- mocha -->
    <script src="../node_modules/grunt-blanket-mocha/node_modules/mocha/mocha.js" type="text/javascript" charset="utf-8"></script>
    <script src="../node_modules/blanket/dist/mocha/blanket_mocha.js" data-cover-adapter="../node_modules/blanket/src/adapters/mocha-blanket.js"></script>
    <script>
        // If tests are run in PhantomJS.
        if (navigator.userAgent.indexOf('PhantomJS') !== -1) {
          blanket.options("reporter", "../node_modules/grunt-blanket-mocha/support/grunt-reporter.js");
        }
    </script>
    <!-- Include your assertion lib of choice -->
    <script src="../node_modules/chai/chai.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">
        // This will be overridden by mocha-helper if you run with grunt
        mocha.setup('bdd');
    </script>
    <script type="text/javascript" charset="utf-8">
        // Setup chai
        var expect = chai.expect;
    </script>

    <!-- Test -->
    <script src="Cow/Cow.js" data-cover type="text/javascript" charset="utf-8"></script>
    <script src="Cow/CowTest.js" type="text/javascript" charset="utf-8"></script>

    <!-- run mocha -->
    <script type="text/javascript" charset="utf-8">
        // If tests run in a real browser.
        if (navigator.userAgent.indexOf('PhantomJS') === -1) {
            mocha.run();
        }
    </script>
</body>
</html>

This plugin appears to be incompatible with Mocha 1.1.4 (the listed dependency). The exception does not occur when Mocha 1.1.3 is used.

  "devDependencies": {
    "grunt": "~0.4.2",
    "chai": "~1.8.1",
    "blanket": "~1.1.5",
    "grunt-blanket-mocha": "~0.3.3"
  }

Option `dest` not working

It seems that when I set 'dest' that no file is generated. The code for this is in grunt-mocha but upon inspection hasn't been carried over into grunt-blanket-mocha.

It would be great to have this feature so CI systems can display test results.

3rd Party Integration w/ Codecov

I'm interested in directly integrating Codecov.io into this project. I would love to chat and help out in any way to make this possible. I could contribute the methods if you would accept a PR or our upload guide can be found there.

Have a great week and enjoy the great outdoors!

in lieu of formal style guide... tests?

Hi @geekdave.

Let me say this is a great task. Thank you.

I like it so much that I'm preparing 6 PRs that make me like it even more! :)

The docs say that you'd like to have unit tests for new functionality. Four of my changes are definitely new, two are arguable but based on what I saw in the code I considered fixes (but whatever).

Anyway, I was thinking I'd follow the contributing guide... and write unit tests....

But it looks like the recommendation to write unit tests might be sneaky way of asking someone to set up testing? ;)

When I npm install and try grunt test, it complains of no mocha task... and when I install grunt-mocha and mocha I find that the Gruntfile doesn't load the grunt-mocha task.... and generally I"m not seeing where the task is tested...

Which makes me wonder whether you're really looking for unit tests for PRs... or am I missing the obvious?

Global Threshold counts excluded files

The global coverage results take into account the files marked as excluded.
Adding or removing files from the excluded list works (files change from FAIL to SKIP), but Global Coverage does not.
Is there a way to avoid counting this files?

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.