Skip to content

Instantly share code, notes, and snippets.

@MethodGrab
Last active August 29, 2015 14:02
Show Gist options
  • Save MethodGrab/09f3431942ca14b6805d to your computer and use it in GitHub Desktop.
Save MethodGrab/09f3431942ca14b6805d to your computer and use it in GitHub Desktop.
Mute Yeoman while tests run (see https://github.com/yeoman/generator/issues/472)
###
Silence STDOUT & STDERR streams during tests
Useful for Yeoman to provide a cleaner output
@uses github.com/balderdashy/fixture-stdout
Yeoman Usage:
If your generator logs anything on the 'end' event
you can add an additional event as the very last
thing and trigger Output.unmute() on that event instead.
```
# app.spec.coffee
Output = require( '../utils/mute' );
beforeEach ->
# ...
@app.on( 'start', Output.mute );
@app.on( 'end', Output.unmute );
```
###
'use strict'
Fixture = require( 'fixture-stdout' )
fixtureOut = new Fixture( )
fixtureErr = new Fixture( stream: process.stderr )
_writesOut = []
_writesErr = []
# Mute
module.exports.mute = ->
fixtureOut.capture onWrite = (string, encoding, fd) ->
_writesOut.push string: string
# Prevent original write
return false
fixtureErr.capture onWrite = (string, encoding, fd) ->
_writesErr.push string: string
# Prevent original write
return false
# Unmute
module.exports.unmute = ->
fixtureOut.release( )
fixtureErr.release( )
# Return the output that was captured
module.exports.getMutedWrites = ->
out: _writesOut
err: _writesErr
/**
* Silence Yeoman during tests
* uses github.com/balderdashy/fixture-stdout
*
* Usage:
* ```
* // test-something.js
* var Output = require( './mute' );
*
* // beforeEach() test:
* this.app.on( 'start', Output.mute );
* this.app.on( 'end', Output.unmute );
* ```
*/
'use strict';
var Fixture = require( 'fixture-stdout' );
var fixtureOut = new Fixture( );
var fixtureErr = new Fixture({ stream: process.stderr });
var _writesOut = [];
var _writesErr = [];
// Mute
module.exports.mute = function ( ) {
fixtureOut.capture( function onWrite ( string, encoding, fd ) {
_writesOut.push({ string : string });
// Prevent original write
return false;
});
fixtureErr.capture( function onWrite ( string, encoding, fd ) {
_writesErr.push({ string : string });
// Prevent original write
return false;
});
};
// Unmute
module.exports.unmute = function ( ) {
fixtureOut.release();
fixtureErr.release();
};
// Return the output that was captured
module.exports.getMutedWrites = function ( ) {
return {
out: _writesOut,
err: _writesErr
};
};
'use strict';
var Output = require( './mute' );
describe('My generator', function ( ) {
beforeEach(function ( done ) {
helpers.testDirectory(path.join( __dirname, 'temp' ), function ( err ) {
this.app = helpers.createGenerator('mygenerator:app', [
'../../app'
]);
// Prevent Yeoman writes while the generator runs
// and reenable them when it's finished to see the test results
this.app.on( 'start', Output.mute );
this.app.on( 'end', Output.unmute );
done( );
}.bind( this ));
});
});
@jgoux
Copy link

jgoux commented Aug 1, 2014

Thanks for this. I hope there will be an official solution soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment