From ea0b9c499739d37148a1ca0dafcbefe567ff2adc Mon Sep 17 00:00:00 2001 From: Thomas Lehmann Date: Wed, 10 Apr 2019 06:38:25 +0200 Subject: [PATCH 1/2] feat(runner): Return emitter that forwards server data Allow the consumer to decide whether to echo the runner progress or not. This might be the case when Karma.run() is called inside the same process/terminal as the server is running in (e.g. development servers). Fixes #2121, #2799 --- docs/dev/04-public-api.md | 11 +++++++++++ lib/runner.js | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/docs/dev/04-public-api.md b/docs/dev/04-public-api.md index 3f810a43c..24d9b81e5 100644 --- a/docs/dev/04-public-api.md +++ b/docs/dev/04-public-api.md @@ -129,6 +129,17 @@ runner.run({port: 9876}, function(exitCode) { }) ``` +`runner.run()` returns an `EventEmitter` which emits a `progress` event passing +the reporter output as a `Buffer` object. + +You may listen for that event to print the reporter output to the console: + +```javascript +runner.run({port: 9876}).on('progress', function(data) { + process.stdout.write(data) +}) +``` + ## karma.stopper ### **stopper.stop(options, [callback=process.exit])** diff --git a/lib/runner.js b/lib/runner.js index 3ff5cccbe..3361fd635 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -3,6 +3,7 @@ const http = require('http') const constant = require('./constants') +const EventEmitter = require('events').EventEmitter const helper = require('./helper') const cfg = require('./config') const logger = require('./logger') @@ -40,6 +41,7 @@ function run (config, done) { config = cfg.parseConfig(config.configFile, config) let exitCode = 1 + let emitter = new EventEmitter() const options = { hostname: config.hostname, path: config.urlRoot + 'run', @@ -54,6 +56,7 @@ function run (config, done) { response.on('data', function (buffer) { const parsedResult = parseExitCode(buffer, exitCode, config.failOnEmptyTestSuite) exitCode = parsedResult.exitCode + emitter.emit('progress', parsedResult.buffer) process.stdout.write(parsedResult.buffer) }) @@ -77,6 +80,8 @@ function run (config, done) { refresh: config.refresh, colors: config.colors })) + + return emitter } exports.run = run From cca89942c7b0eb97a4123b8fce3411615c250a02 Mon Sep 17 00:00:00 2001 From: Thomas Lehmann Date: Wed, 10 Apr 2019 06:40:27 +0200 Subject: [PATCH 2/2] feat(cli): Echo the runner progress event, don't echo in runner The CLI shall echo the progress, not the runner itself. Fixes #2121, #2799 --- lib/cli.js | 8 +++++++- lib/runner.js | 1 - 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index d43847750..fc01865ba 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -240,6 +240,10 @@ function describeCompletion () { .describe('help', 'Print usage.') } +function printRunnerProgress (data) { + process.stdout.write(data) +} + exports.process = function () { const argv = optimist.parse(argsBeforeDoubleDash(process.argv.slice(2))) const options = { @@ -291,7 +295,9 @@ exports.run = function () { new Server(config).start() break case 'run': - require('./runner').run(config) + require('./runner') + .run(config) + .on('progress', printRunnerProgress) break case 'stop': require('./stopper').stop(config) diff --git a/lib/runner.js b/lib/runner.js index 3361fd635..f1d7324d2 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -57,7 +57,6 @@ function run (config, done) { const parsedResult = parseExitCode(buffer, exitCode, config.failOnEmptyTestSuite) exitCode = parsedResult.exitCode emitter.emit('progress', parsedResult.buffer) - process.stdout.write(parsedResult.buffer) }) response.on('end', () => done(exitCode))