Skip to content

Commit

Permalink
feat(runner):
Browse files Browse the repository at this point in the history
* 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

* 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
  • Loading branch information
is-already-taken authored and johnjbarton committed Jul 30, 2019
1 parent 582a406 commit 62d4c5a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/dev/04-public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])**
Expand Down
8 changes: 7 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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',
Expand All @@ -54,7 +56,7 @@ function run (config, done) {
response.on('data', function (buffer) {
const parsedResult = parseExitCode(buffer, exitCode, config.failOnEmptyTestSuite)
exitCode = parsedResult.exitCode
process.stdout.write(parsedResult.buffer)
emitter.emit('progress', parsedResult.buffer)
})

response.on('end', () => done(exitCode))
Expand All @@ -77,6 +79,8 @@ function run (config, done) {
refresh: config.refresh,
colors: config.colors
}))

return emitter
}

exports.run = run

0 comments on commit 62d4c5a

Please sign in to comment.