Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
feat(reporter): add timestamps to results
Browse files Browse the repository at this point in the history
  • Loading branch information
pghalliday authored and dignifiedquire committed Sep 14, 2016
1 parent c893d0a commit 7b41f52
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ On the end of each test `karma-mocha` passes to `karma` result object with field
* `skipped` True if test is skipped.
* `time` Test duration.
* `log` List of errors.
* `startTime` Milliseconds since epoch that the test started
* `endTime` Milliseconds since epoch that the test ended
* `assertionErrors` List of additional error info:
* `name` Error name.
* `message` Error message.
Expand All @@ -115,6 +117,7 @@ On the end of each test `karma-mocha` passes to `karma` result object with field

This object will be passed to test reporter.

NB. the start and end times are added by the adapter whereas the duration is calculated by Mocha - as such they probably will not match arithmetically. Ie. `endTime - startTime !== duration`. These fields have been added so that timestamped reports can be matched up with other timestamped reports from the target device (eg. memory profiling data collected outside the browser)

----

Expand Down
5 changes: 4 additions & 1 deletion src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ var createMochaReporterConstructor = function (tc, pathname) {
})

runner.on('test', function (test) {
test.$startTime = Date.now()
test.$errors = []
test.$assertionErrors = []
})
Expand Down Expand Up @@ -139,7 +140,9 @@ var createMochaReporterConstructor = function (tc, pathname) {
skipped: skipped,
time: skipped ? 0 : test.duration,
log: test.$errors || [],
assertionErrors: test.$assertionErrors || []
assertionErrors: test.$assertionErrors || [],
startTime: test.$startTime,
endTime: Date.now()
}

var pointer = test.parent
Expand Down
14 changes: 12 additions & 2 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,35 @@ describe('adapter mocha', function () {

describe('test end', function () {
it('should report result', function () {
var beforeStartTime = Date.now()
var DURATION = 200

sandbox.stub(tc, 'result', function (result) {
var afterEndTime = Date.now()
expect(result.id).to.not.be.undefined
expect(result.description).to.eq('should do something')
expect(result.suite instanceof Array).to.eq(true)
expect(result.success).to.eq(true)
expect(result.skipped).to.to.eql(false)
expect(result.log instanceof Array).to.eq(true)
expect(result.assertionErrors instanceof Array).to.eq(true)
expect(result.time).to.eq(123)
expect(result.startTime).to.be.at.least(beforeStartTime)
expect(result.endTime - result.startTime).to.be.at.least(DURATION)
expect(result.endTime).to.be.at.most(afterEndTime)
expect(result.time).to.eq(DURATION)
})

var mockMochaResult = {
duration: 123,
duration: DURATION,
parent: {title: 'desc2', parent: {title: 'desc1', root: true}, root: false},
state: 'passed',
title: 'should do something'
}

runner.emit('test', mockMochaResult)
// wait at least 200ms to get different start and end times
var afterStartTime = Date.now()
while (Date.now() - afterStartTime < DURATION) {}
runner.emit('test end', mockMochaResult)

expect(tc.result.called).to.eq(true)
Expand Down

0 comments on commit 7b41f52

Please sign in to comment.