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

Commit 1a8226c

Browse files
authored
fix(deps): Report fails without emit 'test end' event (#223)
* Report fails without emit 'test end' event The mocha listens for 'test end' event (it can restart test if it is failed). If an exception is thrown from the setup/beforeEach, the karma-mocha must not fire 'test end' event, because it leads to exception inside mocha code (the mocha tries to get current test, which is undefined). With this, mocha adapter reports fail directly to karma instead of raising an intermediate 'test end' event. * test: 'fail' hook doesn't emit 'test end' event
1 parent 5828416 commit 1a8226c

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

src/adapter.js

+37-33
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,41 @@ var haveMochaConfig = function (karma) {
8080
return karma.config && karma.config.mocha
8181
}
8282

83+
var reportTestResult = function (karma, test) {
84+
var skipped = test.pending === true
85+
86+
var result = {
87+
id: '',
88+
description: test.title,
89+
suite: [],
90+
success: test.state === 'passed',
91+
skipped: skipped,
92+
pending: skipped,
93+
time: skipped ? 0 : test.duration,
94+
log: test.$errors || [],
95+
assertionErrors: test.$assertionErrors || [],
96+
startTime: test.$startTime,
97+
endTime: Date.now()
98+
}
99+
100+
var pointer = test.parent
101+
while (!pointer.root) {
102+
result.suite.unshift(pointer.title)
103+
pointer = pointer.parent
104+
}
105+
106+
if (haveMochaConfig(karma) && karma.config.mocha.expose && karma.config.mocha.expose.forEach) {
107+
result.mocha = {}
108+
karma.config.mocha.expose.forEach(function (prop) {
109+
if (test.hasOwnProperty(prop)) {
110+
result.mocha[prop] = test[prop]
111+
}
112+
})
113+
}
114+
115+
karma.result(result)
116+
}
117+
83118
var createMochaReporterConstructor = function (tc, pathname) {
84119
var isDebugPage = /debug.html$/.test(pathname)
85120

@@ -129,46 +164,15 @@ var createMochaReporterConstructor = function (tc, pathname) {
129164
if (test.type === 'hook') {
130165
test.$errors = isDebugPage ? [error] : [simpleError]
131166
test.$assertionErrors = assertionError ? [assertionError] : []
132-
runner.emit('test end', test)
167+
reportTestResult(tc, test)
133168
} else {
134169
test.$errors.push(isDebugPage ? error : simpleError)
135170
if (assertionError) test.$assertionErrors.push(assertionError)
136171
}
137172
})
138173

139174
runner.on('test end', function (test) {
140-
var skipped = test.pending === true
141-
142-
var result = {
143-
id: '',
144-
description: test.title,
145-
suite: [],
146-
success: test.state === 'passed',
147-
skipped: skipped,
148-
pending: skipped,
149-
time: skipped ? 0 : test.duration,
150-
log: test.$errors || [],
151-
assertionErrors: test.$assertionErrors || [],
152-
startTime: test.$startTime,
153-
endTime: Date.now()
154-
}
155-
156-
var pointer = test.parent
157-
while (!pointer.root) {
158-
result.suite.unshift(pointer.title)
159-
pointer = pointer.parent
160-
}
161-
162-
if (haveMochaConfig(tc) && tc.config.mocha.expose && tc.config.mocha.expose.forEach) {
163-
result.mocha = {}
164-
tc.config.mocha.expose.forEach(function (prop) {
165-
if (test.hasOwnProperty(prop)) {
166-
result.mocha[prop] = test[prop]
167-
}
168-
})
169-
}
170-
171-
tc.result(result)
175+
reportTestResult(tc, test)
172176
})
173177
}
174178
}

test/src/adapter.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,21 @@ describe('adapter mocha', function () {
290290
expect(tc.result.called).to.eq(true)
291291
})
292292

293+
it('should not emit test end on hook failure', function () {
294+
const testEndStub = sandbox.stub()
295+
runner.on('test end', testEndStub)
296+
297+
var mockMochaHook = {
298+
type: 'hook',
299+
title: 'scenario "before each" hook',
300+
parent: {title: 'desc1', root: true}
301+
}
302+
303+
runner.emit('hook', mockMochaHook)
304+
runner.emit('fail', mockMochaHook, {message: 'hook failed'})
305+
expect(testEndStub.called).to.eq(false)
306+
})
307+
293308
it('should end the test only once on uncaught exceptions', function () {
294309
sandbox.stub(tc, 'result', function (result) {
295310
expect(result.success).to.to.eql(false)

0 commit comments

Comments
 (0)