├── .gitignore ├── .npmignore ├── Gruntfile.js ├── LICENSE-MIT ├── README.md ├── bin └── grunt-simple-mocha ├── examples └── grunt.js ├── package.json ├── tasks └── simple-mocha.js └── tests ├── acceptance-tests.js └── fixtures ├── failing-test-with-stacktrace.gruntfile.js ├── failing-test-with-stacktrace.js ├── failing-test.gruntfile.js ├── failing-test.js ├── passing-test.gruntfile.js └── passing-test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | 3 | // Project configuration. 4 | grunt.initConfig({ 5 | watch: { 6 | files: '', 7 | tasks: 'default' 8 | }, 9 | 10 | jshint: { 11 | options: { 12 | curly: true, 13 | eqeqeq: true, 14 | immed: true, 15 | latedef: true, 16 | newcap: true, 17 | noarg: true, 18 | sub: true, 19 | undef: true, 20 | boss: true, 21 | eqnull: true, 22 | node: true, 23 | es5: true, 24 | globals: {} 25 | }, 26 | 27 | files: ['grunt.js', 'tasks/**/*.js'] 28 | } 29 | }); 30 | 31 | // Load local tasks. 32 | grunt.loadTasks('tasks'); 33 | grunt.loadNpmTasks('grunt-contrib-jshint'); 34 | 35 | // Default task. 36 | grunt.registerTask('default', 'jshint'); 37 | }; 38 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Mukund Lakshman 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grunt-simple-mocha 2 | 3 | A simple wrapper for running tests with Mocha. It's pretty much a cleaned up 4 | and plugin-ized version of [this helpful Gist by johnkpaul][johnkpaul_gist]. 5 | 6 | If you're looking to run client-side specs with PhantomJS, you might be 7 | interested in [grunt-mocha][othermocha]. 8 | 9 | ## Installation 10 | 1. Install this grunt plugin next to your project's 11 | [Gruntfile.js][getting_started] with: 12 | ```javascript 13 | npm install grunt-simple-mocha --save-dev 14 | ``` 15 | 16 | 2. Then add this line to your project's `grunt.js` gruntfile: 17 | ```javascript 18 | grunt.loadNpmTasks('grunt-simple-mocha'); 19 | ``` 20 | 21 | ## Usage 22 | 23 | In your Gruntfile.js: 24 | 25 | ```javascript 26 | grunt.initConfig({ 27 | simplemocha: { 28 | all: { 29 | src: ['test/**/*.js', '**/*.spec.js'] 30 | } 31 | } 32 | }); 33 | 34 | // For this to work, you need to have run `npm install grunt-simple-mocha` 35 | grunt.loadNpmTasks('grunt-simple-mocha'); 36 | 37 | // Add a default task. This is optional, of course :) 38 | grunt.registerTask('default', 'simplemocha'); 39 | ``` 40 | 41 | Now, you can just run `grunt simplemocha` in your shell to run the tests. That's it! 42 | 43 | ## Advanced Mocha Options 44 | 45 | All `options` parameters are passed directly to Mocha. Therefore, you can use these options to configure how Mocha runs. 46 | 47 | - `ui` name "bdd", "tdd", "exports" etc 48 | - `reporter` reporter instance, defaults to `mocha.reporters.spec` 49 | - `globals` array of accepted globals 50 | - `timeout` timeout in milliseconds 51 | - `retries` number of times to retry failed tests 52 | - `bail` bail on the first test failure 53 | - `slow` milliseconds to wait before considering a test slow 54 | - `ignoreLeaks` ignore global leaks 55 | - `fullTrace` display the full stack-trace on failing 56 | - `grep` string or regexp to filter tests with 57 | 58 | The Gruntfile configuration would look like this: 59 | 60 | ```javascript 61 | grunt.initConfig({ 62 | simplemocha: { 63 | // 64 | // These are passed directly to the mocha constructor. See: 65 | // https://github.com/mochajs/mocha/blob/master/lib/mocha.js#L56-L74 66 | // 67 | options: { 68 | ui: 'bdd', 69 | reporter: 'tap', 70 | globals: ['window','document','$','should'], 71 | timeout: 3000, 72 | retries: 2, 73 | bail: false, 74 | slow: 2000, 75 | ignoreLeaks: false, 76 | fullTrace: true 77 | grep: 'users', 78 | }, 79 | 80 | all: { src: ['test/**/*.js'] } 81 | } 82 | }); 83 | ``` 84 | 85 | 86 | ## Running tests 87 | 88 | Run `npm install && npm test` 89 | 90 | ## Contributing 91 | In lieu of a formal styleguide, take care to maintain the existing coding 92 | style. Add unit tests for any new or changed functionality. Lint and test your 93 | code using [grunt][grunt_github]. 94 | 95 | ## Migration Guides 96 | 97 | ### Updating from 0.2.x to 0.3.x 98 | 99 | This task now depends on grunt 0.4.x. Please see the 100 | [grunt 0.3 to 0.4 migration guide][migration_guide] for more details. 101 | 102 | ### Updating from 0.1.x to 0.2.x 103 | 104 | If you were using 0.1.x, the task name has changed from `mocha` to 105 | `simplemocha` to avoid confusion with [grunt-mocha][othermocha]. Please make 106 | sure your grunt.js file is updated. See [#3][issue3]. 107 | 108 | ## Release History 109 | 110 | * v0.1 - Woo! 111 | * v0.2 - Changed the task name from `mocha` to `simplemocha`. See [#3][issue3]. 112 | * v0.3 - Updated to support grunt 0.4.x. 113 | * v0.4 - Updated to support node 0.10.x. 114 | * v0.4.1 - Updated readme and added tests. 115 | 116 | ## License 117 | Copyright (c) 2012 Mukund Lakshman 118 | 119 | Licensed under the MIT license. 120 | 121 | [getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md 122 | [johnkpaul_gist]: https://gist.github.com/2361303 123 | [grunt_github]: http://github.com/cowboy/grunt 124 | [issue3]: https://github.com/yaymukund/grunt-simple-mocha/issues/3 125 | [othermocha]: https://github.com/kmiyashiro/grunt-mocha 126 | [migration_guide]: https://github.com/gruntjs/grunt/wiki/Upgrading-from-0.3-to-0.4 127 | -------------------------------------------------------------------------------- /bin/grunt-simple-mocha: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | require('grunt').npmTasks('grunt-simple-mocha').cli(); 3 | -------------------------------------------------------------------------------- /examples/grunt.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.initConfig({ 3 | simplemocha: { 4 | all: { 5 | src: ['test/**/*.js'], 6 | options: { 7 | globals: ['should'], 8 | timeout: 3000, 9 | ignoreLeaks: false, 10 | grep: '*-test', 11 | ui: 'bdd', 12 | reporter: 'tap' 13 | } 14 | } 15 | } 16 | }); 17 | 18 | // For this to work, you need to have run `npm install grunt-simple-mocha` 19 | grunt.loadNpmTasks('grunt-simple-mocha'); 20 | 21 | // Add a default task. 22 | grunt.registerTask('default', 'simplemocha'); 23 | }; 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-simple-mocha", 3 | "description": "A simple wrapper for running tests with Mocha.", 4 | "version": "0.4.1", 5 | "homepage": "https://github.com/yaymukund/grunt-simple-mocha", 6 | "author": { 7 | "name": "Mukund Lakshman", 8 | "email": "yaymukund@gmail.com", 9 | "url": "http://yaymukund.com" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/yaymukund/grunt-simple-mocha.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/yaymukund/grunt-simple-mocha/issues" 17 | }, 18 | "licenses": [ 19 | { 20 | "type": "MIT", 21 | "url": "https://github.com/yaymukund/grunt-simple-mocha/blob/master/LICENSE-MIT" 22 | } 23 | ], 24 | "main": "grunt.js", 25 | "bin": "bin/grunt-simple-mocha", 26 | "engines": { 27 | "node": "*" 28 | }, 29 | "scripts": { 30 | "test": "node_modules/mocha/bin/mocha tests/acceptance-tests.js" 31 | }, 32 | "dependencies": { 33 | "mocha": "*" 34 | }, 35 | "devDependencies": { 36 | "grunt": "0.4.x", 37 | "grunt-contrib-jshint": "0.1.x", 38 | "mocha": "^2.3.4" 39 | }, 40 | "keywords": [ 41 | "gruntplugin", 42 | "mocha", 43 | "test" 44 | ] 45 | } 46 | -------------------------------------------------------------------------------- /tasks/simple-mocha.js: -------------------------------------------------------------------------------- 1 | /* 2 | * grunt-simple-mocha 3 | * https://github.com/yaymukund/grunt-simple-mocha 4 | * 5 | * Copyright (c) 2012 Mukund Lakshman 6 | * Licensed under the MIT license. 7 | */ 8 | "use strict"; 9 | 10 | module.exports = function(grunt) { 11 | 12 | var path = require('path'), 13 | Mocha = require('mocha'); 14 | 15 | grunt.registerMultiTask('simplemocha', 'Run tests with mocha', function() { 16 | 17 | var options = this.options(), 18 | mocha_instance = new Mocha(options); 19 | 20 | this.filesSrc.forEach(mocha_instance.addFile.bind(mocha_instance)); 21 | 22 | // We will now run mocha asynchronously and receive number of errors in a 23 | // callback, which we'll use to report the result of the async task by 24 | // calling done() with the appropriate value to indicate whether an error 25 | // occurred. 26 | 27 | var done = this.async(); 28 | 29 | mocha_instance.run(function(errCount) { 30 | var withoutErrors = (errCount === 0); 31 | done(withoutErrors); 32 | }); 33 | }); 34 | }; 35 | -------------------------------------------------------------------------------- /tests/acceptance-tests.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | exec = require('child_process').exec, 3 | GRUNT = 'node_modules/grunt-cli/bin/grunt'; 4 | 5 | function grunt(gruntfile, done) { 6 | exec(GRUNT+' --gruntfile tests/fixtures/'+gruntfile+'.gruntfile.js', done); 7 | } 8 | 9 | describe('acceptance tests for simplemocha', function() { 10 | it('passes without error', function(done) { 11 | grunt('passing-test', function(error, stdout, stderr) { 12 | assert.ok(error === null); 13 | done(); 14 | }); 15 | }); 16 | 17 | it('fails with code > 0', function(done) { 18 | grunt('failing-test', function(error, stdout, stderr) { 19 | assert.ok(error.code > 0); 20 | done(); 21 | }); 22 | }); 23 | 24 | it('displays mocha error upon exit', function(done) { 25 | grunt('failing-test', function(error, stdout, stderr) { 26 | assert.ok(stdout.match(/1 failing/)); 27 | done(); 28 | }); 29 | }); 30 | 31 | it('fullTrace: true option exposes full stack trace', function(done) { 32 | grunt('failing-test-with-stacktrace', function(error, stdout, stderr) { 33 | assert.ok(stdout.match(/\s+at level2.*\n\s+at level1.*/)); 34 | done(); 35 | }); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/fixtures/failing-test-with-stacktrace.gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.loadTasks('../../tasks'); 3 | 4 | grunt.initConfig({ 5 | simplemocha: { 6 | options: { fullTrace: true }, 7 | all: { src: ['./failing-test-with-stacktrace.js'] } 8 | } 9 | }); 10 | 11 | grunt.registerTask('default', 'simplemocha'); 12 | }; 13 | -------------------------------------------------------------------------------- /tests/fixtures/failing-test-with-stacktrace.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | function level2() { 4 | throw new Error('It failed!'); 5 | } 6 | 7 | function level1() { 8 | level2(); 9 | } 10 | 11 | describe('a failing test', function() { 12 | it('should succeed', function() { 13 | // But it doesn't... 14 | level1(); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /tests/fixtures/failing-test.gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.loadTasks('../../tasks'); 3 | 4 | grunt.initConfig({ 5 | simplemocha: { 6 | all: { src: ['./failing-test.js'] } 7 | } 8 | }); 9 | 10 | grunt.registerTask('default', 'simplemocha'); 11 | }; 12 | -------------------------------------------------------------------------------- /tests/fixtures/failing-test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | describe('a failing test', function() { 4 | it('should succeed', function() { 5 | // But it doesn't... 6 | assert.equal(1, -1); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/fixtures/passing-test.gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.loadTasks('../../tasks'); 3 | 4 | grunt.initConfig({ 5 | simplemocha: { 6 | all: { src: ['./passing-test.js'] } 7 | } 8 | }); 9 | 10 | grunt.registerTask('default', 'simplemocha'); 11 | }; 12 | -------------------------------------------------------------------------------- /tests/fixtures/passing-test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | describe('a failing test', function() { 4 | it('should succeed', function() { 5 | assert.ok(true); 6 | }); 7 | }); 8 | --------------------------------------------------------------------------------