Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Commit

Permalink
feat(runner): allow customer reporters via runner.reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkallen authored and thetutlage committed Sep 27, 2018
1 parent 0d99590 commit 1350106
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

const { Runner, reporters, Assertion } = require('japa/api')
const { InvalidArgumentException } = require('@adonisjs/generic-exceptions')
const pSeries = require('p-series')
const { resolver } = require('@adonisjs/fold')
const debug = require('debug')('adonis:vow:runner')
Expand All @@ -28,7 +29,7 @@ Assertion.use(require('chai-subset'))
class TestRunner {
constructor (Env) {
this.clear()
this._reporter = Env.get('REPORTER', reporters.list)
this._reporter = reporters[Env.get('REPORTER', 'list')]
}

/**
Expand Down Expand Up @@ -80,6 +81,20 @@ class TestRunner {
})
}

/**
* Set the reporter function to a custom one
*
* @method reporter
*
* @private
*/
reporter (reporterFn) {
if (typeof (reporterFn) !== 'function') {
throw new InvalidArgumentException('Reporter must be a function that accepts an emitter')
}
this._reporter = reporterFn
}

/**
* Clear properties of runner.
*
Expand Down
39 changes: 39 additions & 0 deletions test/unit/runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const test = require('japa')
const { setupResolver, Env, Config } = require('@adonisjs/sink')
const { InvalidArgumentException } = require('@adonisjs/generic-exceptions')
const { ioc } = require('@adonisjs/fold')
const Runner = require('../../src/Runner')
const props = require('../../lib/props')
Expand Down Expand Up @@ -277,4 +278,42 @@ test.group('Runner', (group) => {
const emitter = new EventEmitter()
this.runner.emitter(emitter)
})

test('run runner set custom reporter', async (assert) => {
const called = []
const suite = this.runner.suite('sample')

suite.test('test', function () {
called.push('run_test')
})

this.runner.reporter(function (emitter) {
emitter.on('group:start', () => {
called.push('group_start')
})

emitter.on('group:end', () => {
called.push('group_end')
})

emitter.on('test:start', () => {
called.push('test_start')
})

emitter.on('test:end', () => {
called.push('test_end')
})
})

await this.runner.run()
assert.deepEqual(called, ['group_start', 'test_start', 'run_test', 'test_end', 'group_end'])
})
})

test('run runner reporter must be a function', async (assert) => {
try {
this.runner.reporter('Not a function')
} catch (error) {
assert.instanceOf(error, InvalidArgumentException)
}
})

0 comments on commit 1350106

Please sign in to comment.