Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fork #111

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Fork #111

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
35 changes: 34 additions & 1 deletion tasks/lib/MochaWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
28 changes: 2 additions & 26 deletions tasks/mocha-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -89,6 +64,7 @@ module.exports = function(grunt) {
} else {
complete(null, failureCount);
}

});
}, function(error, failureCount) {
// restore the uncaught exception handlers
Expand Down