dirty-chai
raw JSON → 3.0.0 verified Fri May 01 auth: no javascript
A Chai plugin that converts terminating property assertions (e.g., .true, .ok) into function-call form (e.g., .true(), .ok()). This makes assertions lint-friendly (avoiding 'Expected an assignment or function call' warnings) and ensures assertions are actually invoked. Version 3.0.0 requires Chai >= 6.2.0 < 7. It works with browser and Node, and can adapt property assertions from other Chai plugins when loaded before them. Key differentiator: eliminates false positives from uninvoked property access.
Common errors
error TypeError: expect(...).to.be.true is not a function ↓
cause dirty-chai not installed or not used with chai.use()
fix
Install dirty-chai and call chai.use(dirtyChai) before using function-form assertions.
error AssertionError: expected [Function] to be true ↓
cause Assertion called without parentheses, returning the function instead of executing it
fix
Add parentheses to the assertion: .true() instead of .true
error Error: length is not a function ↓
cause Attempting to chain .length after another assertion (e.g., .exist.and.have.length)
fix
Use .length first in the chain or break into separate assertions.
Warnings
breaking dirty-chai 3.x requires Chai >= 6.2.0. Using older Chai versions will cause errors. ↓
fix Update chai to version >=6.2.0 <7
gotcha After dirty-chai, assertions must always be called with parentheses to execute. Syntactically correct without parens but assertion won't run. ↓
fix Always terminate chains with a function call, e.g., .true()
gotcha Chaining after .length or .arguments fails: these properties become functions and cannot be chained after other assertions. ↓
fix Use .length or .arguments first in the chain, or separate into multiple assertions
gotcha Plugin must be loaded after chai-as-promised (if used), but before other plugins to transform their property assertions. ↓
fix Load chai-as-promised before dirty-chai, and dirty-chai before any other plugins
deprecated CommonJS require is deprecated in v3; use ESM imports. ↓
fix Use import dirtyChai from 'dirty-chai' instead of require('dirty-chai')
Install
npm install dirty-chai yarn add dirty-chai pnpm add dirty-chai Imports
- dirtyChai wrong
const dirtyChai = require('dirty-chai')correctimport dirtyChai from 'dirty-chai' - chai wrong
const chai = require('chai')correctimport chai from 'chai' - use wrong
chai.use(dirtyChai())correctchai.use(dirtyChai)
Quickstart
import chai from 'chai';
import dirtyChai from 'dirty-chai';
chai.use(dirtyChai);
const { expect } = chai;
expect(true).to.be.true();
expect(null).to.be.null('Custom error message');
expect([1]).to.be.empty();
expect('hello').to.exist();
// With chai-as-promised:
// import chaiAsPromised from 'chai-as-promised';
// chai.use(chaiAsPromised);
// chai.use(dirtyChai);