From a74869bc587f6deb87130c76ab6fb3ede6aa1224 Mon Sep 17 00:00:00 2001 From: Lyrkan Date: Fri, 18 Aug 2017 12:55:19 +0200 Subject: [PATCH] Intercept output during functional tests (fixes #47) --- lib/test/setup.js | 56 ++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/test/setup.js b/lib/test/setup.js index d95d5533..cb66cdfc 100644 --- a/lib/test/setup.js +++ b/lib/test/setup.js @@ -60,32 +60,47 @@ function createWebpackConfig(testAppDir, outputDirName = '', command, argv = {}) } function runWebpack(webpackConfig, callback, allowCompilationError = false) { - validator(webpackConfig); - const compiler = webpack(configGenerator(webpackConfig)); - compiler.run((err, stats) => { - if (err) { - console.error(err.stack || err); - if (err.details) { - console.error(err.details); - } + const stdoutWrite = process.stdout.write; - throw new Error('Error running webpack!'); - } + try { + // Mute stdout + process.stdout.write = () => {}; - const info = stats.toJson(); + validator(webpackConfig); - if (stats.hasErrors() && !allowCompilationError) { - console.error(info.errors); + const compiler = webpack(configGenerator(webpackConfig)); + compiler.run((err, stats) => { - throw new Error('Compilation error running webpack!'); - } + if (err) { + console.error(err.stack || err); + if (err.details) { + console.error(err.details); + } - if (stats.hasWarnings()) { - console.warn(info.warnings); - } + throw new Error('Error running webpack!'); + } - callback(assertUtil(webpackConfig), stats); - }); + const info = stats.toJson(); + + if (stats.hasErrors() && !allowCompilationError) { + console.error(info.errors); + + throw new Error('Compilation error running webpack!'); + } + + if (stats.hasWarnings()) { + console.warn(info.warnings); + } + + // Restore stdout and then call the callback + process.stdout.write = stdoutWrite; + callback(assertUtil(webpackConfig), stats); + }); + } catch (e) { + // Restore stdout and then re-throw the exception + process.stdout.write = stdoutWrite; + throw e; + } } function emptyTmpDir() { @@ -158,6 +173,7 @@ function requestTestPage(webRootDir, scriptSrcs, callback) { startHttpServer('8090', webRootDir); const browser = new Browser(); + browser.silent = true; browser.on('error', function(error) { throw new Error(`Error when running the browser: ${error}`); });