Adds a new default value formatter that will be used to for all new assertion contexts
The value formatter to add
OptionalcircularDefines a custom message to display when a circular reference is detected in a value.
The default is a function that returns the string "[
Returns the current config options in a new plain object
Optionaloptions: IAssertConfigThe options to override the current config options
OptionaldefDefiines the default assertion failure message to display if no message is provided.
OptionaldefDefines the default fatal message to display if no message is provided.
OptionalformatOptions for configuring value formatting.
These options allow you to customize how values are formatted in assertion error messages. You can provide custom formatters to override the default formatting behavior for specific types. These formatters will be checked before the default formatters, allowing you to provide specialized formatting for certain value types.
Additionally, you can control post-processing of the formatted output using IFormatterOptions.finalize and IFormatterOptions.finalizeFn to escape ANSI codes, wrap output, or apply other transformations.
import { assertConfig } from '@nevware21/tripwire';
import { eFormatResult } from '@nevware21/tripwire';
import { escapeAnsi, gray, replaceAnsi } from '@nevware21/chromacon';
// Example 1: Custom formatters with default ANSI escaping
assertConfig.format = {
finalize: true, // Enable default escapeAnsi
formatters: [
{
name: "stringFormatter",
value: (ctx, value) => {
if (typeof value === 'string') {
return { res: eFormatResult.Ok, val: `'${value}'` };
}
return { res: eFormatResult.Skip };
}
}
]
};
// Example 2: Custom formatters with custom finalization
assertConfig.format = {
finalize: true,
finalizeFn: (value) => replaceAnsi(value, (match) => gray(escapeAnsi(match))),
formatters: [
{
name: "arrayFormatter",
value: (ctx, value) => {
if (Array.isArray(value)) {
return { res: eFormatResult.Ok, val: `[${value.length} items]` };
}
return { res: eFormatResult.Skip };
}
}
]
};
OptionalfullIdentifies if the current context is in full-stack mode, which will include the full stack trace in the error message if an assertion fails.
OptionalisIdentifies if the current context is verbose mode, which will include additional information in the execution context and any resulting assertion failures.
Removes a value formatter from the current context options
The value formatter to remove
Resets the current config options to their default values removing any customizations
Export the interfaces