Skip to content

Commit 6fcba9b

Browse files
authoredApr 6, 2023
feat: add chai config (#3066)
1 parent 7856ec1 commit 6fcba9b

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed
 

‎docs/config/index.md

+30
Original file line numberDiff line numberDiff line change
@@ -1355,3 +1355,33 @@ Path to custom tsconfig, relative to the project root.
13551355
- **Default**: `300`
13561356

13571357
The number of milliseconds after which a test is considered slow and reported as such in the results.
1358+
1359+
### chaiConfig
1360+
1361+
- **Type:** `{ includeStack?, showDiff?, truncateThreshold? }`
1362+
- **Default:** `{ includeStack: false, showDiff: true, truncateThreshold: 40 }`
1363+
1364+
Equivalent to [Chai config](https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js).
1365+
1366+
#### chaiConfig.includeStack
1367+
1368+
- **Type:** `boolean`
1369+
- **Default:** `false`
1370+
1371+
Influences whether stack trace is included in Assertion error message. Default of false suppresses stack trace in the error message.
1372+
1373+
#### chaiConfig.showDiff
1374+
1375+
- **Type:** `boolean`
1376+
- **Default:** `true`
1377+
1378+
Influences whether or not the `showDiff` flag should be included in the thrown AssertionErrors. `false` will always be `false`; `true` will be true when the assertion has requested a diff to be shown.
1379+
1380+
#### chaiConfig.truncateThreshold
1381+
1382+
- **Type:** `number`
1383+
- **Default:** `40`
1384+
1385+
Sets length threshold for actual and expected values in assertion errors. If this threshold is exceeded, for example for large data structures, the value is replaced with something like `[ Array(3) ]` or `{ Object (prop1, prop2) }`. Set it to `0` if you want to disable truncating altogether.
1386+
1387+
This config option affects truncating values in `test.each` titles and inside the assertion error message.

‎packages/vitest/src/integrations/chai/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,7 @@ Object.defineProperty(globalThis, GLOBAL_EXPECT, {
8080

8181
export { assert, should } from 'chai'
8282
export { chai, globalExpect as expect }
83+
export const setupChaiConfig = (config: ChaiConfig) => {
84+
Object.assign(chai.config, config)
85+
}
86+
export type ChaiConfig = Omit<Partial<typeof chai.config>, 'useProxy' | 'proxyExcludedKeys'>

‎packages/vitest/src/runtime/entry.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getWorkerState, resetModules } from '../utils'
66
import { vi } from '../integrations/vi'
77
import { distDir } from '../paths'
88
import { startCoverageInsideWorker, stopCoverageInsideWorker, takeCoverageInsideWorker } from '../integrations/coverage'
9+
import { setupChaiConfig } from '../integrations/chai'
910
import { setupGlobalEnv, withEnv } from './setup.node'
1011
import { rpc } from './rpc'
1112
import type { VitestExecutor } from './execute'
@@ -71,6 +72,9 @@ export async function run(files: string[], config: ResolvedConfig, environment:
7172

7273
const workerState = getWorkerState()
7374

75+
if (config.chaiConfig)
76+
setupChaiConfig(config.chaiConfig)
77+
7478
const runner = await getTestRunner(config, executor)
7579

7680
// @ts-expect-error untyped global

‎packages/vitest/src/types/config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers'
44
import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner'
55
import type { BuiltinReporters } from '../node/reporters'
66
import type { TestSequencerConstructor } from '../node/sequencers/types'
7+
import type { ChaiConfig } from '../integrations/chai'
78
import type { CoverageOptions, ResolvedCoverageOptions } from './coverage'
89
import type { JSDOMOptions } from './jsdom-options'
910
import type { Reporter } from './reporter'
@@ -562,6 +563,12 @@ export interface InlineConfig {
562563
* Requires `singleThread: true` OR `threads: false`.
563564
*/
564565
inspectBrk?: boolean
566+
567+
/**
568+
* Modify default Chai config. Vitest uses Chai for `expect` and `assert` matches.
569+
* https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js
570+
*/
571+
chaiConfig?: ChaiConfig
565572
}
566573

567574
export interface TypecheckConfig {

0 commit comments

Comments
 (0)
Please sign in to comment.