Implementation:DevExpress Testcafe StackCleaningHook
| Knowledge Sources | |
|---|---|
| Domains | Error Handling, Developer Experience |
| Last Updated | 2026-02-12 12:00 GMT |
Overview
stackCleaningHook is a singleton object that filters internal TestCafe frames from error stack traces so that users see only their own code in error output.
Description
src/errors/stack-cleaning-hook.js exports a default singleton object with an enabled property and a cleanError method. When enabled, it:
- Increases the stack trace limit -- Sets
Error.stackTraceLimitto 200 (from the original default, typically 10) because Babel-generated errors can produce very deep stacks. When disabled, it restores the original limit. - Parses error frames -- Uses the
error-stack-parserlibrary to parse the error's.stackstring into structuredStackFrameobjects. - Strips existing trace lines --
_removeStackTraceLineswalks the stack string from the bottom and removes all lines matching the/^\s+at\s+.*$/pattern, leaving only the error message portion. - Filters internal frames -- Applies a stack filter (created by
createStackFilter) that removes frames originating from internal TestCafe code, keeping only user-relevant frames up to the original stack trace limit. - Re-renders the stack -- Appends the filtered frames' source strings back onto the cleaned
error.stack.
The enabled getter/setter uses a guard to avoid redundant state changes and manages the Error.stackTraceLimit toggle as a side effect.
Usage
The hook is enabled just before test file compilation/execution and disabled immediately after (in finally blocks). The APIBasedTestFileCompilerBase enables it around _compileCodeForTestFiles and _runCompiledCode, calling cleanError(err) on caught errors before re-throwing them as TestCompilationError.
Code Reference
Source Location
src/errors/stack-cleaning-hook.js (83 lines)
Signature
export default {
isEnabled: false,
_isStackTraceLine (stackLine): boolean,
_removeStackTraceLines (error): void,
_getFrames (error): StackFrame[],
_renderFrameInfo (frames): string,
get enabled (): boolean,
set enabled (val: boolean): void,
cleanError (error): Error,
};
Import
import stackCleaningHook from '../../errors/stack-cleaning-hook';
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
enabled (setter) |
boolean |
Toggles the hook on/off. When set to true, increases Error.stackTraceLimit to 200. When set to false, restores the original limit.
|
error (for cleanError) |
Error |
An error object whose .stack property will be cleaned of internal TestCafe frames.
|
Outputs
| Method | Return Type | Description |
|---|---|---|
cleanError(error) |
Error |
Returns the same error object with its .stack property rewritten to contain only user-relevant stack frames. If the error has no parseable frames, it is returned unmodified.
|
enabled (getter) |
boolean |
Returns the current enabled state of the hook. |
Usage Examples
Enabling around compilation:
import stackCleaningHook from '../../errors/stack-cleaning-hook';
stackCleaningHook.enabled = true;
try {
const compiled = compiler._precompileCode(testFilesInfo);
return compiled;
}
catch (err) {
throw new TestCompilationError(stackCleaningHook.cleanError(err));
}
finally {
stackCleaningHook.enabled = false;
}
Enabling around test execution:
stackCleaningHook.enabled = true;
try {
await this._execAsModule(compiledCode, filename);
}
catch (err) {
if (!(err instanceof APIError))
throw new TestCompilationError(stackCleaningHook.cleanError(err));
throw err;
}
finally {
stackCleaningHook.enabled = false;
}
Related Pages
- DevExpress_Testcafe_ApiBasedCompiler -- Primary consumer that enables/disables the hook during compilation and execution
- DevExpress_Testcafe_ErrorTypes -- Defines the error codes for errors that get their stacks cleaned by this hook