Skip to content

Commit 1f30058

Browse files
author
DavertMik
committed
fixed mocha hooks, analyze plugin, added custom reporter tests
1 parent b7e4acb commit 1f30058

File tree

23 files changed

+292
-154
lines changed

23 files changed

+292
-154
lines changed

lib/codecept.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class Codecept {
205205
}
206206

207207
try {
208-
event.emit(event.all.before, container.result())
208+
event.emit(event.all.before)
209209
mocha.run(() => done())
210210
} catch (e) {
211211
output.error(e.stack)

lib/command/run-workers.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ const Workers = require('../workers')
88
module.exports = async function (workerCount, selectedRuns, options) {
99
process.env.profile = options.profile
1010

11-
const suiteArr = []
12-
const passedTestArr = []
13-
const failedTestArr = []
14-
const skippedTestArr = []
15-
const stepArr = []
16-
1711
const { config: testConfig, override = '' } = options
1812
const overrideConfigs = tryOrDefault(() => JSON.parse(override), {})
1913
const by = options.suites ? 'suite' : 'test'
@@ -36,17 +30,14 @@ module.exports = async function (workerCount, selectedRuns, options) {
3630
workers.overrideConfig(overrideConfigs)
3731

3832
workers.on(event.test.failed, test => {
39-
failedTestArr.push(test)
4033
output.test.failed(test)
4134
})
4235

4336
workers.on(event.test.passed, test => {
44-
passedTestArr.push(test)
4537
output.test.passed(test)
4638
})
4739

4840
workers.on(event.test.skipped, test => {
49-
skippedTestArr.push(test)
5041
output.test.skipped(test)
5142
})
5243

lib/command/workers/runTests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ function initializeListeners() {
9999
event.dispatcher.on(event.step.failed, step => sendToParentThread({ event: event.step.failed, workerIndex, data: step.simplify() }))
100100

101101
event.dispatcher.on(event.hook.failed, (hook, err) => sendToParentThread({ event: event.hook.failed, workerIndex, data: { ...hook.simplify(), err } }))
102-
event.dispatcher.on(event.hook.passed, (hook, err) => sendToParentThread({ event: event.hook.passed, workerIndex, data: { ...hook.simplify(), err } }))
102+
event.dispatcher.on(event.hook.passed, hook => sendToParentThread({ event: event.hook.passed, workerIndex, data: hook.simplify() }))
103+
event.dispatcher.on(event.hook.finished, hook => sendToParentThread({ event: event.hook.finished, workerIndex, data: hook.simplify() }))
103104

104105
event.dispatcher.once(event.all.after, () => {
105106
sendToParentThread({ event: event.all.after, workerIndex, data: container.result().simplify() })

lib/event.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const debug = require('debug')('codeceptjs:event');
2-
const events = require('events');
3-
const { error } = require('./output');
1+
const debug = require('debug')('codeceptjs:event')
2+
const events = require('events')
3+
const { error } = require('./output')
44

5-
const dispatcher = new events.EventEmitter();
5+
const dispatcher = new events.EventEmitter()
66

7-
dispatcher.setMaxListeners(50);
7+
dispatcher.setMaxListeners(50)
88
/**
99
* @namespace
1010
* @alias event
@@ -59,6 +59,7 @@ module.exports = {
5959
started: 'hook.start',
6060
passed: 'hook.passed',
6161
failed: 'hook.failed',
62+
finished: 'hook.finished',
6263
},
6364

6465
/**
@@ -141,33 +142,33 @@ module.exports = {
141142
* @param {*} [param]
142143
*/
143144
emit(event, param) {
144-
let msg = `Emitted | ${event}`;
145+
let msg = `Emitted | ${event}`
145146
if (param && param.toString()) {
146-
msg += ` (${param.toString()})`;
147+
msg += ` (${param.toString()})`
147148
}
148-
debug(msg);
149+
debug(msg)
149150
try {
150-
this.dispatcher.emit.apply(this.dispatcher, arguments);
151+
this.dispatcher.emit.apply(this.dispatcher, arguments)
151152
} catch (err) {
152-
error(`Error processing ${event} event:`);
153-
error(err.stack);
153+
error(`Error processing ${event} event:`)
154+
error(err.stack)
154155
}
155156
},
156157

157158
/** for testing only! */
158159
cleanDispatcher: () => {
159-
let event;
160+
let event
160161
for (event in this.test) {
161-
this.dispatcher.removeAllListeners(this.test[event]);
162+
this.dispatcher.removeAllListeners(this.test[event])
162163
}
163164
for (event in this.suite) {
164-
this.dispatcher.removeAllListeners(this.test[event]);
165+
this.dispatcher.removeAllListeners(this.test[event])
165166
}
166167
for (event in this.step) {
167-
this.dispatcher.removeAllListeners(this.test[event]);
168+
this.dispatcher.removeAllListeners(this.test[event])
168169
}
169170
for (event in this.all) {
170-
this.dispatcher.removeAllListeners(this.test[event]);
171+
this.dispatcher.removeAllListeners(this.test[event])
171172
}
172173
},
173-
};
174+
}

lib/listener/globalTimeout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ module.exports = function () {
130130

131131
if (typeof timeout === 'number' && timeout <= 0 && recorder.isRunning()) {
132132
debug(`step ${step.toCode().trim()} timed out`)
133-
if (currentTest && currentTest.callback && currentTest.type == 'test') {
133+
if (currentTest && currentTest.callback) {
134134
debug(`Failing test ${currentTest.title} with timeout ${currentTimeout}s`)
135135
recorder.reset()
136136
// replace mocha timeout with custom timeout

lib/listener/result.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = function () {
66
container.result().addStats({ failedHooks: 1 })
77
})
88

9-
event.dispatcher.on(event.test.started, test => {
9+
event.dispatcher.on(event.test.before, test => {
1010
container.result().addTest(test)
1111
})
1212
}

lib/mocha/asyncWrapper.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ const injectHook = function (inject, suite) {
1313
recorder.throw(err)
1414
}
1515
recorder.catch(err => {
16-
event.emit(event.test.failed, suite, err)
16+
suiteTestFailedHookError(suite, err)
1717
throw err
1818
})
1919
return recorder.promise()
2020
}
2121

22+
function suiteTestFailedHookError(suite, err) {
23+
suite.eachTest(test => {
24+
test.err = err
25+
event.emit(event.test.failed, test, err)
26+
})
27+
}
28+
2229
function makeDoneCallableOnce(done) {
2330
let called = false
2431
return function (err) {
@@ -61,6 +68,7 @@ module.exports.test = test => {
6168
err = newErr
6269
}
6370
}
71+
test.err = err
6472
event.emit(event.test.failed, test, err)
6573
event.emit(event.test.finished, test)
6674
recorder.add(() => doneFn(err))
@@ -112,7 +120,7 @@ module.exports.injected = function (fn, suite, hookName) {
112120
const errHandler = err => {
113121
recorder.session.start('teardown')
114122
recorder.cleanAsyncErr()
115-
if (hookName == 'before' || hookName == 'beforeSuite') suite.eachTest(test => event.emit(event.test.failed, test, err))
123+
if (hookName == 'before' || hookName == 'beforeSuite') suiteTestFailedHookError(suite, err)
116124
if (hookName === 'after') event.emit(event.test.after, suite)
117125
if (hookName === 'afterSuite') event.emit(event.suite.after, suite)
118126
recorder.add(() => doneFn(err))
@@ -156,6 +164,7 @@ module.exports.injected = function (fn, suite, hookName) {
156164
)
157165
.then(() => {
158166
recorder.add('fire hook.passed', () => fireHook(event.hook.passed, suite))
167+
recorder.add('fire hook.finished', () => fireHook(event.hook.finished, suite))
159168
recorder.add(`finish ${hookName} hook`, doneFn)
160169
recorder.catch()
161170
})
@@ -166,6 +175,7 @@ module.exports.injected = function (fn, suite, hookName) {
166175
errHandler(err)
167176
})
168177
recorder.add('fire hook.failed', () => fireHook(event.hook.failed, suite, e))
178+
recorder.add('fire hook.finished', () => fireHook(event.hook.finished, suite))
169179
})
170180
}
171181
}

lib/mocha/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class Cli extends Base {
230230
stack.shift()
231231
}
232232

233-
if (stack[0].trim() == 'Error:') {
233+
if (stack[0] && stack[0].trim() == 'Error:') {
234234
stack.shift()
235235
}
236236

lib/mocha/hooks.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@ const event = require('../event')
22
const { serializeError } = require('../utils')
33
// const { serializeTest } = require('./test')
44

5+
/**
6+
* Represents a test hook in the testing framework
7+
* @class
8+
* @property {Object} suite - The test suite this hook belongs to
9+
* @property {Object} test - The test object associated with this hook
10+
* @property {Object} runnable - The current test being executed
11+
* @property {Object} ctx - The context object
12+
* @property {Error|null} err - The error that occurred during hook execution, if any
13+
*/
514
class Hook {
15+
/**
16+
* Creates a new Hook instance
17+
* @param {Object} context - The context object containing suite and test information
18+
* @param {Object} context.suite - The test suite
19+
* @param {Object} context.test - The test object
20+
* @param {Object} context.ctx - The context object
21+
* @param {Error} error - The error object if hook execution failed
22+
*/
623
constructor(context, error) {
724
this.suite = context.suite
825
this.test = context.test
926
this.runnable = context?.ctx?.test
1027
this.ctx = context.ctx
11-
this.error = error
28+
this.err = error
1229
}
1330

1431
get hookName() {
@@ -21,7 +38,7 @@ class Hook {
2138
title: this.title,
2239
// test: this.test ? serializeTest(this.test) : null,
2340
// suite: this.suite ? serializeSuite(this.suite) : null,
24-
error: this.error ? serializeError(this.error) : null,
41+
error: this.err ? serializeError(this.err) : null,
2542
}
2643
}
2744

@@ -58,13 +75,13 @@ function fireHook(eventType, suite, error) {
5875
const hook = suite.ctx?.test?.title?.match(/"([^"]*)"/)[1]
5976
switch (hook) {
6077
case 'before each':
61-
event.emit(eventType, new BeforeHook(suite))
78+
event.emit(eventType, new BeforeHook(suite, error))
6279
break
6380
case 'after each':
6481
event.emit(eventType, new AfterHook(suite, error))
6582
break
6683
case 'before all':
67-
event.emit(eventType, new BeforeSuiteHook(suite))
84+
event.emit(eventType, new BeforeSuiteHook(suite, error))
6885
break
6986
case 'after all':
7087
event.emit(eventType, new AfterSuiteHook(suite, error))

lib/mocha/test.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function enhanceMochaTest(test) {
4646
test.addToSuite = function (suite) {
4747
enhanceMochaSuite(suite)
4848
suite.addTest(testWrapper(this))
49-
if (test.file) suite.file = relativeDir(test.file)
49+
if (test.file && !suite.file) suite.file = test.file
5050
test.tags = [...(test.tags || []), ...(suite.tags || [])]
5151
test.fullTitle = () => `${suite.title}: ${test.title}`
5252
test.uid = genTestId(test)
@@ -78,20 +78,22 @@ function deserializeTest(test) {
7878
return test
7979
}
8080

81-
function serializeTest(test, err = null) {
81+
function serializeTest(test, error = null) {
8282
// test = { ...test }
8383

8484
if (test.start && !test.duration) {
8585
const end = +new Date()
8686
test.duration = end - test.start
8787
}
8888

89+
let err
90+
8991
if (test.err) {
9092
err = serializeError(test.err)
91-
test.status = 'failed'
92-
} else if (err) {
93-
err = serializeError(err)
94-
test.status = 'failed'
93+
test.state = 'failed'
94+
} else if (error) {
95+
err = serializeError(error)
96+
test.state = 'failed'
9597
}
9698
const parent = {}
9799
if (test.parent) {
@@ -105,6 +107,11 @@ function serializeTest(test, err = null) {
105107
})
106108
}
107109

110+
let steps = undefined
111+
if (Array.isArray(test.steps)) {
112+
steps = test.steps.map(step => (step.simplify ? step.simplify() : step))
113+
}
114+
108115
return {
109116
opts: test.opts || {},
110117
tags: test.tags || [],
@@ -118,7 +125,7 @@ function serializeTest(test, err = null) {
118125
duration: test.duration || 0,
119126
err,
120127
parent,
121-
steps: test.steps?.toArray()?.map(step => (step.simplify ? step.simplify() : step)),
128+
steps,
122129
}
123130
}
124131

0 commit comments

Comments
 (0)