Global configuration options for customizing assertion behavior, error reporting, proxy usage, and deep equality comparison. Chai's configuration system allows fine-tuning of library behavior across all assertion styles.
Global configuration object containing all customizable settings.
interface Config {
/**
* Include stack trace in assertion error messages
* @default false
*/
includeStack: boolean;
/**
* Show diff in assertion error messages when appropriate
* @default true
*/
showDiff: boolean;
/**
* Length threshold for truncating values in error messages
* Set to 0 to disable truncation entirely
* @default 40
*/
truncateThreshold: number;
/**
* Use Proxy to catch typos in property names
* Automatically disabled if Proxy is not supported
* @default true
*/
useProxy: boolean;
/**
* Array of property names to exclude from proxy error checking
* @default ['then', 'catch', 'inspect', 'toJSON']
*/
proxyExcludedKeys: string[];
/**
* Custom deep equality function for assertions
* If null, uses default deep-eql package implementation
* @default null
*/
deepEqual: ((expected: any, actual: any) => boolean) | null;
}The configuration object is available as a named export and property.
import { config } from "chai";
// Access configuration settings
const currentStackSetting = config.includeStack;
const currentDiffSetting = config.showDiff;
// Modify configuration
config.includeStack = true;
config.showDiff = false;Controls whether stack traces are included in assertion error messages.
import { config, expect } from "chai";
// Enable stack traces (disabled by default for cleaner output)
config.includeStack = true;
try {
expect(1).to.equal(2);
} catch (error) {
console.log(error.stack); // Now includes full stack trace
}
// Disable stack traces for cleaner error messages
config.includeStack = false;Controls whether diffs are shown in assertion error messages when comparing objects or arrays.
import { config, expect } from "chai";
// Enable diffs (enabled by default)
config.showDiff = true;
try {
expect({ a: 1, b: 2 }).to.deep.equal({ a: 1, b: 3 });
} catch (error) {
console.log(error.message);
// Shows diff:
// {
// "a": 1,
// - "b": 2
// + "b": 3
// }
}
// Disable diffs
config.showDiff = false;Sets the length threshold for truncating long values in error messages.
import { config, expect } from "chai";
// Default threshold (40 characters)
config.truncateThreshold = 40;
const longArray = new Array(100).fill(0);
try {
expect(longArray).to.deep.equal([]);
} catch (error) {
console.log(error.message);
// Shows: expected [ Array(100) ] to deeply equal []
}
// Disable truncation
config.truncateThreshold = 0;
try {
expect(longArray).to.deep.equal([]);
} catch (error) {
console.log(error.message);
// Shows full array: expected [ 0, 0, 0, ... ] to deeply equal []
}
// Set custom threshold
config.truncateThreshold = 100;Controls whether Chai uses Proxy objects to catch typos in property names.
import { config, expect } from "chai";
// Enable proxy (enabled by default in supporting environments)
config.useProxy = true;
try {
expect(42).to.be.greaterThan(10); // Typo: should be 'greaterThan'
} catch (error) {
// TypeError: Invalid Chai property: greaterThan
}
// Disable proxy (allows invalid properties to pass silently)
config.useProxy = false;
expect(42).to.be.greaterThan(10); // No error, but assertion doesn't workArray of property names that should not trigger proxy errors, even if they don't exist.
import { config, expect } from "chai";
// Default excluded keys
console.log(config.proxyExcludedKeys);
// ['then', 'catch', 'inspect', 'toJSON']
// Add custom excluded keys
config.proxyExcludedKeys.push('customProperty');
// Now 'customProperty' won't trigger proxy error
const assertion = expect(42);
console.log(assertion.customProperty); // undefined, no error
// Remove excluded keys to catch more typos
config.proxyExcludedKeys = ['then', 'inspect'];Custom deep equality function for all deep equality assertions.
import { config, expect } from "chai";
// Default behavior (uses deep-eql package)
config.deepEqual = null;
expect({ a: 1 }).to.deep.equal({ a: 1 }); // Uses default deep equality
// Custom deep equality with tolerance for numbers
config.deepEqual = (expected, actual) => {
// Custom logic for number comparison with tolerance
if (typeof expected === 'number' && typeof actual === 'number') {
return Math.abs(expected - actual) < 0.01;
}
// Use default deep equality for non-numbers
return require('chai').util.eql(expected, actual);
};
expect(1.0001).to.deep.equal(1.0002); // Now passes due to tolerance
// Reset to default
config.deepEqual = null;// test-setup.js
import { config } from "chai";
// Configure for development environment
if (process.env.NODE_ENV === 'development') {
config.includeStack = true;
config.showDiff = true;
config.truncateThreshold = 100;
}
// Configure for production/CI environment
if (process.env.NODE_ENV === 'production' || process.env.CI) {
config.includeStack = false;
config.showDiff = true;
config.truncateThreshold = 40;
}
// Configure for debugging
if (process.env.DEBUG) {
config.includeStack = true;
config.truncateThreshold = 0; // Show full values
}import { config, expect } from "chai";
// Custom deep equality for dates with day precision
config.deepEqual = (expected, actual) => {
if (expected instanceof Date && actual instanceof Date) {
return expected.toDateString() === actual.toDateString();
}
// Use default for non-dates
return require('chai').util.eql(expected, actual);
};
const date1 = new Date('2023-01-01T08:00:00Z');
const date2 = new Date('2023-01-01T20:00:00Z');
expect(date1).to.deep.equal(date2); // Passes - same day// Detect and configure based on browser capabilities
import { config } from "chai";
// Disable proxy in older browsers
if (typeof Proxy === 'undefined') {
config.useProxy = false;
}
// Adjust proxy excluded keys for specific frameworks
if (typeof window !== 'undefined' && window.jQuery) {
config.proxyExcludedKeys.push('$');
}
// Add framework-specific excluded keys
if (typeof angular !== 'undefined') {
config.proxyExcludedKeys.push('$$');
}import { config } from "chai";
// Optimize for performance in large test suites
config.showDiff = false; // Disable expensive diff generation
config.includeStack = false; // Disable stack trace generation
config.truncateThreshold = 20; // Reduce string processing
config.useProxy = false; // Disable proxy overhead
// Optimize for debugging
config.showDiff = true;
config.includeStack = true;
config.truncateThreshold = 0;
config.useProxy = true;// Jest integration
if (typeof jest !== 'undefined') {
config.includeStack = false; // Jest handles stack traces
config.showDiff = true; // Jest can display diffs
}
// Mocha integration
if (typeof mocha !== 'undefined') {
config.includeStack = true; // Mocha benefits from stack traces
config.showDiff = true;
}
// Custom test runner integration
if (process.env.CUSTOM_RUNNER) {
config.includeStack = Boolean(process.env.SHOW_STACKS);
config.showDiff = Boolean(process.env.SHOW_DIFFS);
config.truncateThreshold = parseInt(process.env.TRUNCATE_THRESHOLD) || 40;
}Chai 6.x moved some settings from Assertion class to the config object:
// Old way (deprecated in 6.x)
import { Assertion } from "chai";
Assertion.includeStack = true;
Assertion.showDiff = false;
// New way
import { config } from "chai";
config.includeStack = true;
config.showDiff = false;