Package detail

grunt-mocha

disqus12.3kMIT1.2.0

Grunt task for running client-side Mocha specs in PhantomJS

gruntplugin, mocha, test, phantomjs

readme

grunt-mocha NPM version Build Status

Automatically run client-side mocha specs via grunt/mocha/PhantomJS

For a grunt task for server-side mocha tests, see grunt-mocha-test or grunt-simple-mocha

Getting Started

This plugin requires Grunt ~0.4.0. Use a 0.1.x tag of this plugin to use with Grunt ~0.3.0.

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-mocha --save-dev

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

grunt.loadNpmTasks('grunt-mocha');

Mocha task

Run this task with the grunt mocha command.

Settings

files/src

Type: String|Array

This defines which HTML spec files to run using PhantomJS. These are the same files you would open to run tests in a browser.

There are a number of options available. Please review the minimatch options here.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
  },
},

dest

Type: String Default: undefined

Write reporter output to a file. Useful if you need a file to feed your CI bot.

Example:

mocha: {
  test: {
    options: {
      reporter: 'XUnit'
    },
    src: ['tests/**/*.html'],
    dest: './test/output/xunit.out',
  },
},

options.run

Type: Boolean Default: true

grunt-mocha injects a script into the PhantomJS instance that loads your HTML spec files. The file sets up a reporter and listeners so the output can be output in the command line. This option will call mocha.run() after the script is injected, ensuring that the proper listeners are setup.

You may want to set this to false if your files are loaded asynchronously via AMD and call mocha.run in your own callback.

In HTML spec:

<!-- run mocha after all test are loaded -->
<script type="text/javascript" charset="utf-8">
  // Only tests run in real browser, injected script run if options.run == true
  if (navigator.userAgent.indexOf('PhantomJS') < 0) {
    mocha.run();
  }
</script>

Gruntfile:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      run: true,
    },
  },
},

options.urls

Type: Array|String Default: []

Instead of files, hit these URLs. Usually used in conjunction with the connect task to spin up a server for testing.

connect: {
  server: {
    options: {
      port: 8888,
      base: '.',
    },
  },
},
mocha: {
  test: {
    options: {
      urls: [ 'http://localhost:8888/example/test/test2.html' ],
    },
  },
},

Then run:

grunt connect mocha

options.timeout

Type: Number Default: 5000

PhantomJS timeout in milliseconds. If nothing happens within 5 seconds, exit.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      timeout: 10000,
    },
  },
},

options.bail

Type: Boolean Default: false

Call grunt.warn and exit the grunt task on the first failed test. This only calls grunt.warn after the entire spec file is finished.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      bail: true,
    },
  },
},

options.growlOnFail

Type: Boolean Default: true

Display a Growl notification when tests fail.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      growlOnFail: false,
    },
  },
},

options.growlOnSuccess

Type: Boolean Default: true

Display a Growl notification when all tests successfully pass.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      growlOnSuccess: false,
    },
  },
},

options.log

Type: Boolean Default: false

Print any console.log calls from PhantomJS to the command line. Only used for very quick and dirty debugging. It is highly recommended that you open the failing spec file in a browser so you can use much richer debugging tools.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      log: true,
    },
  },
},

options.logErrors

Type: Boolean Default: false

Fail and output script errors.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      logErrors: true,
    },
  },
},

options.mocha

Type: Object

A mocha options simple object. Very few options are currently supported. Actually, I think grep is the only one.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      mocha: {
        grep: 'router*'
      }
    }
  },
},

options.reporter

Type: String Default: 'Dot'

The reporter to use. Note: XUnit and those types of reporters should probably use the dest option.

Example:

mocha: {
  test: {
    files: ['tests/**/*.html'],
    options: {
      reporter: 'Nyan',
    }
  },
},

Custom reporter example: Example:

mocha: {
  test: {
    files: ['tests/**/*.html'],
    options {
      reporter: './path/to/custom/reporter', // included via require
    },
  },
},

options.page

Type: Object

Set properties in the PhantomJS webpage instance used for tests, see http://phantomjs.org/api/webpage/

Example:

mocha: {
  test: {
    options: {
      page: {
        settings: {
          webSecurityEnabled: false,  // disable cors checks in phantomjs
        },  
      },
    },
  },
},

Hacks

The PhantomJS -> Grunt superdimensional conduit uses alert. If you have disabled or aliased alert in your app, this won't work. I have conveniently set a global PHANTOMJS on window so you can conditionally override alert in your app.

Examples

Vanilla JS

Option 1 (recommended)

  • Write mocha task description in grunt config using and specify run: true option (see this task's Gruntfile.js for details);
  • Check for PhantomJS userAgent in a test html file and run tests only in a real browser (see test2.html for details).

In this case you shouldn't include bridge.js (it will be included automatically) and tests will be run from bridge.js.

Option 2

Alternatively, include bridge.js from tasks/phantomjs after you include mocha.js and run mocha.setup in your HTML file. The helper will override mocha.setup if it detects PhantomJS. See test.html.

AMD

Mocha must be included via script tag in the header. There is no need to load Mocha via AMD. You may load other testing libs via AMD if that gives you a fuzzy feeling.

Example setup with AMD (advanced): https://gist.github.com/2655876

License

Licensed under the MIT license.

changelog

History

1.2.0

  • Add support for node 10

1.1.0

  • Add growlOnFail option

1.0.5

  • Adding titlePath in bridge to handle reporting of test failures

1.0.0

  • Bump mocha to 2.4.5
  • [BREAKING] Bump grunt-lib-phantomjs to master, uses PhantomJS 2.x
  • [BREAKING] run option is now true by default

0.4.11

  • Bump mocha to 1.18 for promise support
  • README fixes
  • options.growlOnSuccess option added (@naganowl)

0.4.10

  • Actually error out if logErrors is true and error

0.4.9

  • Add logErrors option to fail/log script errors (@demmer)

0.4.8

  • Add test state to output for XUnit

0.4.7

  • Explicit done() call with true/false so grunt exits with correct code. (@Bartvds)

0.4.6

  • lodash is a dep, not a dev-dep

0.4.5

  • Add dest output config for outputting to a file (@BYK)

0.4.4

  • Bump example mocha to 1.14.0
  • Compatible with mocha 1.14.0 (@perfectworks)
  • Cleanup log listeners during watch (@rayshih)

0.4.3

  • Bump grunt-lib-phantomjs to 0.4, which uses PhantomJS 1.9.*

0.4.2

  • Fix bail option

0.4.1

  • Fixing package.json keywords, no functional changes

0.4.0

  • Support for custom reporters (@Bartvds)
  • Keep track of suites and fake test parent (@demmer)
  • Package size optimizations (@sindresorhus)
  • Bump mocha version to 1.12

0.3.4

  • grunt.warn instead of grunt.log.warn on failures with bail false, correct exit code. (#71)

0.3.3

  • Add log option to output console.log. False by default.
  • Update Readme to reflect Grunt 0.4 changes (finally)

0.3.2

  • Add bail option (false by default)
  • Do not bail on error by default

0.3.1

  • Update grunt-lib-phantomjs to 0.3.0

0.3.0

  • You may now specify which reporter you want to use (thanks SBoudrias)
  • Growl only shows notices on failures
sampleTest: {
    src: ['test.js'],
    options: {
        reporter: 'Nyan'
        mocha: {
            ui: 'tdd'
        }
    }
}

0.2.2

  • Updating grunt/gruntplugin dependencies to rc6.
  • Added travis.yml
  • Added .jshintrc and jshint to Gruntfile.js
  • Migrated documentation and Gruntfile.js to grunt-contrib standards

0.2.0

  • Grunt >=0.4.0 only, MIGRATION REQUIRED if updating from 0.1.x
  • Uses grunt-lib-phantomjs for easy PhantomJS installation!
  • If things are not working right, try removing and installing via npm again.
  • Config changes required
    • tasks/mocha/mocha-helper.js has been moved to phantomjs/bridge.js, if you are including it manually, you must change the path/filename
    • Task options are now nested in the options key per task/target
all: {
    src: ['test.js'],
    options: {
        mocha: {
            ui: 'tdd'
        },
        run: true
    }
}

0.1.7

  • Fix bad legacy mocha check for mocha < 1.4.2 (rohni)

0.1.6

  • Add ability to pass mocha config options (grep, etc) via grunt task config. (gamtiq)
  • Add option to not include mocha-spec-helper.js and auto-inject/run with the run config option. Note: Still not required for AMD. This requires an if-statement in your HTML spec to check for PhantomJS environment, it's either that or include the helper. See example/test2.html. (gamtiq)

0.1.5

  • Fix total duration when testing multiple html spec files in a single task. (chevalric)

0.1.4

  • Critical fix: Compatibility with Mocha 1.4.2 (iammerick)
    • If you use mocha-helper.js for non-AMD and you want to use Mocha >1.4.2, you replace it with the one in this update.

0.1.3

  • Remove grunt from deps

0.1.2

  • Added example for non-AMD usage. Modified mocha-helper to be included in page (optional)
  • Added duration

0.1.1

  • Consistency
  • Make growl optional

0.1.0

init