Skip to content

Commit 21ee94f

Browse files
committed
fix: prevent JSONResponse._beforeSuite from hanging recorder
JSONResponse._beforeSuite() was throwing errors synchronously when the request helper wasn't found or properly configured. When running with codeceptjs run/run-workers, this caused 'trapped unhandled rejection' and the process would hang. Changed to: - Catch all errors and log warnings instead of throwing - Early return if request helper is not available - Ensure config object exists before setting callbacks - Prevents recorder from hanging on rejected promises This allows tests to proceed even if JSONResponse isn't properly configured, with clear warning messages instead of silent hangs.
1 parent cb7ba57 commit 21ee94f

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

lib/helper/JSONResponse.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,40 @@ class JSONResponse extends Helper {
6969

7070
_beforeSuite() {
7171
this.response = null
72-
if (!this.helpers[this.options.requestHelper]) {
73-
throw new Error(`Error setting JSONResponse, helper ${this.options.requestHelper} is not enabled in config, helpers: ${Object.keys(this.helpers)}`)
74-
}
75-
const origOnResponse = this.helpers[this.options.requestHelper].config.onResponse
76-
this.helpers[this.options.requestHelper].config.onResponse = response => {
77-
this.response = response
78-
if (typeof origOnResponse === 'function') origOnResponse(response)
72+
73+
try {
74+
const requestHelper = this.helpers[this.options.requestHelper]
75+
76+
if (!requestHelper) {
77+
const helperNames = Object.keys(this.helpers || {})
78+
const errorMsg = `Error setting JSONResponse, helper ${this.options.requestHelper} is not enabled in config, helpers: ${helperNames.join(', ') || 'none'}`
79+
this.debugSection('JSONResponse', errorMsg)
80+
// Don't throw - just warn and return to avoid hanging
81+
console.warn(`[JSONResponse] ${errorMsg}`)
82+
return
83+
}
84+
85+
// Ensure the helper has a config object
86+
if (!requestHelper.config) {
87+
requestHelper.config = {}
88+
}
89+
90+
const origOnResponse = requestHelper.config.onResponse
91+
requestHelper.config.onResponse = response => {
92+
this.response = response
93+
if (typeof origOnResponse === 'function') origOnResponse(response)
94+
}
95+
} catch (error) {
96+
// Log the error but don't throw to avoid hanging the recorder
97+
console.error(`[JSONResponse._beforeSuite] ${error.message}`)
98+
this.debugSection('JSONResponse Error', error.message)
7999
}
80100
}
81101

82102
_before() {
83103
this.response = null
84104
}
85105

86-
87106
/**
88107
* Checks that response code is equal to the provided one
89108
*

0 commit comments

Comments
 (0)