Skip to content

Commit

Permalink
fixed mocha hooks, analyze plugin, added custom reporter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Jan 23, 2025
1 parent b7e4acb commit 1f30058
Show file tree
Hide file tree
Showing 23 changed files with 292 additions and 154 deletions.
2 changes: 1 addition & 1 deletion lib/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Codecept {
}

try {
event.emit(event.all.before, container.result())
event.emit(event.all.before)
mocha.run(() => done())
} catch (e) {
output.error(e.stack)
Expand Down
9 changes: 0 additions & 9 deletions lib/command/run-workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ const Workers = require('../workers')
module.exports = async function (workerCount, selectedRuns, options) {
process.env.profile = options.profile

const suiteArr = []
const passedTestArr = []
const failedTestArr = []
const skippedTestArr = []
const stepArr = []

const { config: testConfig, override = '' } = options
const overrideConfigs = tryOrDefault(() => JSON.parse(override), {})
const by = options.suites ? 'suite' : 'test'
Expand All @@ -36,17 +30,14 @@ module.exports = async function (workerCount, selectedRuns, options) {
workers.overrideConfig(overrideConfigs)

workers.on(event.test.failed, test => {
failedTestArr.push(test)
output.test.failed(test)
})

workers.on(event.test.passed, test => {
passedTestArr.push(test)
output.test.passed(test)
})

workers.on(event.test.skipped, test => {
skippedTestArr.push(test)
output.test.skipped(test)
})

Expand Down
3 changes: 2 additions & 1 deletion lib/command/workers/runTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ function initializeListeners() {
event.dispatcher.on(event.step.failed, step => sendToParentThread({ event: event.step.failed, workerIndex, data: step.simplify() }))

event.dispatcher.on(event.hook.failed, (hook, err) => sendToParentThread({ event: event.hook.failed, workerIndex, data: { ...hook.simplify(), err } }))
event.dispatcher.on(event.hook.passed, (hook, err) => sendToParentThread({ event: event.hook.passed, workerIndex, data: { ...hook.simplify(), err } }))
event.dispatcher.on(event.hook.passed, hook => sendToParentThread({ event: event.hook.passed, workerIndex, data: hook.simplify() }))
event.dispatcher.on(event.hook.finished, hook => sendToParentThread({ event: event.hook.finished, workerIndex, data: hook.simplify() }))

event.dispatcher.once(event.all.after, () => {
sendToParentThread({ event: event.all.after, workerIndex, data: container.result().simplify() })
Expand Down
35 changes: 18 additions & 17 deletions lib/event.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const debug = require('debug')('codeceptjs:event');
const events = require('events');
const { error } = require('./output');
const debug = require('debug')('codeceptjs:event')
const events = require('events')
const { error } = require('./output')

const dispatcher = new events.EventEmitter();
const dispatcher = new events.EventEmitter()

dispatcher.setMaxListeners(50);
dispatcher.setMaxListeners(50)
/**
* @namespace
* @alias event
Expand Down Expand Up @@ -59,6 +59,7 @@ module.exports = {
started: 'hook.start',
passed: 'hook.passed',
failed: 'hook.failed',
finished: 'hook.finished',
},

/**
Expand Down Expand Up @@ -141,33 +142,33 @@ module.exports = {
* @param {*} [param]
*/
emit(event, param) {
let msg = `Emitted | ${event}`;
let msg = `Emitted | ${event}`
if (param && param.toString()) {
msg += ` (${param.toString()})`;
msg += ` (${param.toString()})`
}
debug(msg);
debug(msg)
try {
this.dispatcher.emit.apply(this.dispatcher, arguments);
this.dispatcher.emit.apply(this.dispatcher, arguments)
} catch (err) {
error(`Error processing ${event} event:`);
error(err.stack);
error(`Error processing ${event} event:`)
error(err.stack)
}
},

/** for testing only! */
cleanDispatcher: () => {
let event;
let event
for (event in this.test) {
this.dispatcher.removeAllListeners(this.test[event]);
this.dispatcher.removeAllListeners(this.test[event])
}
for (event in this.suite) {
this.dispatcher.removeAllListeners(this.test[event]);
this.dispatcher.removeAllListeners(this.test[event])
}
for (event in this.step) {
this.dispatcher.removeAllListeners(this.test[event]);
this.dispatcher.removeAllListeners(this.test[event])
}
for (event in this.all) {
this.dispatcher.removeAllListeners(this.test[event]);
this.dispatcher.removeAllListeners(this.test[event])
}
},
};
}
2 changes: 1 addition & 1 deletion lib/listener/globalTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ module.exports = function () {

if (typeof timeout === 'number' && timeout <= 0 && recorder.isRunning()) {
debug(`step ${step.toCode().trim()} timed out`)
if (currentTest && currentTest.callback && currentTest.type == 'test') {
if (currentTest && currentTest.callback) {
debug(`Failing test ${currentTest.title} with timeout ${currentTimeout}s`)
recorder.reset()
// replace mocha timeout with custom timeout
Expand Down
2 changes: 1 addition & 1 deletion lib/listener/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function () {
container.result().addStats({ failedHooks: 1 })
})

event.dispatcher.on(event.test.started, test => {
event.dispatcher.on(event.test.before, test => {
container.result().addTest(test)
})
}
14 changes: 12 additions & 2 deletions lib/mocha/asyncWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ const injectHook = function (inject, suite) {
recorder.throw(err)
}
recorder.catch(err => {
event.emit(event.test.failed, suite, err)
suiteTestFailedHookError(suite, err)
throw err
})
return recorder.promise()
}

function suiteTestFailedHookError(suite, err) {
suite.eachTest(test => {
test.err = err
event.emit(event.test.failed, test, err)
})
}

function makeDoneCallableOnce(done) {
let called = false
return function (err) {
Expand Down Expand Up @@ -61,6 +68,7 @@ module.exports.test = test => {
err = newErr
}
}
test.err = err
event.emit(event.test.failed, test, err)
event.emit(event.test.finished, test)
recorder.add(() => doneFn(err))
Expand Down Expand Up @@ -112,7 +120,7 @@ module.exports.injected = function (fn, suite, hookName) {
const errHandler = err => {
recorder.session.start('teardown')
recorder.cleanAsyncErr()
if (hookName == 'before' || hookName == 'beforeSuite') suite.eachTest(test => event.emit(event.test.failed, test, err))
if (hookName == 'before' || hookName == 'beforeSuite') suiteTestFailedHookError(suite, err)
if (hookName === 'after') event.emit(event.test.after, suite)
if (hookName === 'afterSuite') event.emit(event.suite.after, suite)
recorder.add(() => doneFn(err))
Expand Down Expand Up @@ -156,6 +164,7 @@ module.exports.injected = function (fn, suite, hookName) {
)
.then(() => {
recorder.add('fire hook.passed', () => fireHook(event.hook.passed, suite))
recorder.add('fire hook.finished', () => fireHook(event.hook.finished, suite))
recorder.add(`finish ${hookName} hook`, doneFn)
recorder.catch()
})
Expand All @@ -166,6 +175,7 @@ module.exports.injected = function (fn, suite, hookName) {
errHandler(err)
})
recorder.add('fire hook.failed', () => fireHook(event.hook.failed, suite, e))
recorder.add('fire hook.finished', () => fireHook(event.hook.finished, suite))
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Cli extends Base {
stack.shift()
}

if (stack[0].trim() == 'Error:') {
if (stack[0] && stack[0].trim() == 'Error:') {
stack.shift()
}

Expand Down
25 changes: 21 additions & 4 deletions lib/mocha/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@ const event = require('../event')
const { serializeError } = require('../utils')
// const { serializeTest } = require('./test')

/**
* Represents a test hook in the testing framework
* @class
* @property {Object} suite - The test suite this hook belongs to
* @property {Object} test - The test object associated with this hook
* @property {Object} runnable - The current test being executed
* @property {Object} ctx - The context object
* @property {Error|null} err - The error that occurred during hook execution, if any
*/
class Hook {
/**
* Creates a new Hook instance
* @param {Object} context - The context object containing suite and test information
* @param {Object} context.suite - The test suite
* @param {Object} context.test - The test object
* @param {Object} context.ctx - The context object
* @param {Error} error - The error object if hook execution failed
*/
constructor(context, error) {
this.suite = context.suite
this.test = context.test
this.runnable = context?.ctx?.test
this.ctx = context.ctx
this.error = error
this.err = error
}

get hookName() {
Expand All @@ -21,7 +38,7 @@ class Hook {
title: this.title,
// test: this.test ? serializeTest(this.test) : null,
// suite: this.suite ? serializeSuite(this.suite) : null,
error: this.error ? serializeError(this.error) : null,
error: this.err ? serializeError(this.err) : null,
}
}

Expand Down Expand Up @@ -58,13 +75,13 @@ function fireHook(eventType, suite, error) {
const hook = suite.ctx?.test?.title?.match(/"([^"]*)"/)[1]
switch (hook) {
case 'before each':
event.emit(eventType, new BeforeHook(suite))
event.emit(eventType, new BeforeHook(suite, error))
break
case 'after each':
event.emit(eventType, new AfterHook(suite, error))
break
case 'before all':
event.emit(eventType, new BeforeSuiteHook(suite))
event.emit(eventType, new BeforeSuiteHook(suite, error))
break
case 'after all':
event.emit(eventType, new AfterSuiteHook(suite, error))
Expand Down
21 changes: 14 additions & 7 deletions lib/mocha/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function enhanceMochaTest(test) {
test.addToSuite = function (suite) {
enhanceMochaSuite(suite)
suite.addTest(testWrapper(this))
if (test.file) suite.file = relativeDir(test.file)
if (test.file && !suite.file) suite.file = test.file
test.tags = [...(test.tags || []), ...(suite.tags || [])]
test.fullTitle = () => `${suite.title}: ${test.title}`
test.uid = genTestId(test)
Expand Down Expand Up @@ -78,20 +78,22 @@ function deserializeTest(test) {
return test
}

function serializeTest(test, err = null) {
function serializeTest(test, error = null) {
// test = { ...test }

if (test.start && !test.duration) {
const end = +new Date()
test.duration = end - test.start
}

let err

if (test.err) {
err = serializeError(test.err)
test.status = 'failed'
} else if (err) {
err = serializeError(err)
test.status = 'failed'
test.state = 'failed'
} else if (error) {
err = serializeError(error)
test.state = 'failed'
}
const parent = {}
if (test.parent) {
Expand All @@ -105,6 +107,11 @@ function serializeTest(test, err = null) {
})
}

let steps = undefined
if (Array.isArray(test.steps)) {
steps = test.steps.map(step => (step.simplify ? step.simplify() : step))
}

return {
opts: test.opts || {},
tags: test.tags || [],
Expand All @@ -118,7 +125,7 @@ function serializeTest(test, err = null) {
duration: test.duration || 0,
err,
parent,
steps: test.steps?.toArray()?.map(step => (step.simplify ? step.simplify() : step)),
steps,
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/mocha/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ declare global {
type: string
text: string
}>
state: string
err?: Error
config: Record<string, any>
artifacts: string[]
inject: Record<string, any>
Expand Down
3 changes: 2 additions & 1 deletion lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ module.exports = {
*/
started: suite => {
if (!suite.title) return
print(`${colors.bold(suite.title)} --`, colors.underline.grey(suite.file || ''))
print(`${colors.bold(suite.title)} --`)
if (suite.file && outputLevel >= 1) print(colors.underline.grey(suite.file))
if (suite.comment) print(suite.comment)
},
},
Expand Down
Loading

0 comments on commit 1f30058

Please sign in to comment.