Skip to content

Commit

Permalink
feat(runner): Return emitter that forwards server data
Browse files Browse the repository at this point in the history
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 karma-runner#2121, karma-runner#2799
  • Loading branch information
is-already-taken committed Apr 10, 2019
1 parent c7ebf0b commit ea0b9c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 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
5 changes: 5 additions & 0 deletions 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,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)
})

Expand All @@ -77,6 +80,8 @@ function run (config, done) {
refresh: config.refresh,
colors: config.colors
}))

return emitter
}

exports.run = run

0 comments on commit ea0b9c4

Please sign in to comment.