diff --git a/README.md b/README.md index cd74816..2e56e57 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,29 @@ +# Main difference +On original `grunt-mocha-test` if you have a mocha test that use `console.log` and configuration like that + + mochaTest: { + 'coverage': { + options: { + reporter: 'html-cov', + quiet: true, + captureFile: 'unit-tests.html', + }, + src: ['test.js'] + }, + } + + the test + + describe('grunt-mocha-test', function() { + it('should return 1', function(done) { + console.log('It will out on report file'); + done(); + }); + }); + +In this case the console output will be reported on report file, this fork **fix it**. To it work the reporter need to write on the `runner.on('end')` event + + # grunt-mocha-test [![Build Status](https://travis-ci.org/pghalliday/grunt-mocha-test.svg)](https://travis-ci.org/pghalliday/grunt-mocha-test) diff --git a/package.json b/package.json index 67c94bf..fc9dc1c 100644 --- a/package.json +++ b/package.json @@ -1,19 +1,19 @@ { - "name": "grunt-mocha-test", + "name": "grunt-mocha-test-y", "description": "A grunt task for running server side mocha tests", - "version": "0.12.7", - "homepage": "https://github.com/pghalliday/grunt-mocha-test", + "version": "0.12.8", + "homepage": "https://github.com/mageddo/grunt-mocha-test", "author": { - "name": "Peter Halliday", - "email": "pghalliday@gmail.com", - "url": "http://pghalliday.github.io/" + "name": "Elvis de Freitas", + "email": "edigitalb@gmail.com", + "url": "http://github.com/mageddo/gunt-mocha-test/" }, "repository": { "type": "git", - "url": "git://github.com/pghalliday/grunt-mocha-test.git" + "url": "git://github.com/mageddo/grunt-mocha-test.git" }, "bugs": { - "url": "https://github.com/pghalliday/grunt-mocha-test/issues" + "url": "https://github.com/mageddo/grunt-mocha-test/issues" }, "license": "MIT", "engines": { diff --git a/tasks/lib/MochaWrapper.js b/tasks/lib/MochaWrapper.js index fceb2fc..9b0d978 100644 --- a/tasks/lib/MochaWrapper.js +++ b/tasks/lib/MochaWrapper.js @@ -2,6 +2,8 @@ var domain = require('domain'); var fs = require('fs'); var path = require('path'); var Mocha = require('mocha'); +var hooker = require('hooker'); +var mkdirpSync = require('mkdirp').sync; function MochaWrapper(params) { // If require option is specified then require that file. @@ -53,7 +55,38 @@ function MochaWrapper(params) { var mochaSuite = mocha.suite; var mochaOptions = mocha.options; var mochaRunner = new Mocha.Runner(mochaSuite); - var mochaReporter = new mocha._reporter(mochaRunner, mochaOptions); + + var fd; + mochaRunner.on('end', function(){ + if (params.options.captureFile) { + mkdirpSync(path.dirname(params.options.captureFile)); + fd = fs.openSync(params.options.captureFile, 'w'); + } + // Hook process.stdout.write + hooker.hook(process.stdout, 'write', { + // This gets executed before the original process.stdout.write + pre: function(result) { + // Write result to file if it was opened + if (fd) { + fs.writeSync(fd, result); + } + // Prevent the original process.stdout.write from executing if quiet was specified + if (params.options.quiet) { + return hooker.preempt(); + } + } + }); + }); + var mochaReporter = new mocha._reporter(mochaRunner, mochaOptions, mocha); + mochaRunner.on('end', function(){ + // close the file if it was opened + if (fd) { + fs.closeSync(fd); + } + // Restore process.stdout.write to its original value + hooker.unhook(process.stdout, 'write'); + }); + mochaRunner.ignoreLeaks = false !== mochaOptions.ignoreLeaks; mochaRunner.asyncOnly = mochaOptions.asyncOnly; if (mochaOptions.grep) { diff --git a/tasks/mocha-test.js b/tasks/mocha-test.js index 47274d1..8e1f27b 100644 --- a/tasks/mocha-test.js +++ b/tasks/mocha-test.js @@ -2,39 +2,14 @@ var MochaWrapper = require('./lib/MochaWrapper'); var fs = require('fs'); var path = require('path'); var hooker = require('hooker'); -var mkdirpSync = require('mkdirp').sync; module.exports = function(grunt) { // Helper to capture task output (adapted from tests for grunt-contrib-jshint) var capture = function(captureFile, quiet, run, done) { - var fd; - if (captureFile) { - mkdirpSync(path.dirname(captureFile)); - fd = fs.openSync(captureFile, 'w'); - } - // Hook process.stdout.write - hooker.hook(process.stdout, 'write', { - // This gets executed before the original process.stdout.write - pre: function(result) { - // Write result to file if it was opened - if (fd) { - fs.writeSync(fd, result); - } - // Prevent the original process.stdout.write from executing if quiet was specified - if (quiet) { - return hooker.preempt(); - } - } - }); + // Execute the code whose output is to be captured run(function(error, failureCount) { - // close the file if it was opened - if (fd) { - fs.closeSync(fd); - } - // Restore process.stdout.write to its original value - hooker.unhook(process.stdout, 'write'); // Actually test the actually-logged stdout string to the expected value done(error, failureCount); }); @@ -89,6 +64,7 @@ module.exports = function(grunt) { } else { complete(null, failureCount); } + }); }, function(error, failureCount) { // restore the uncaught exception handlers