Skip to content

Commit

Permalink
feat: add chai config (#3066)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed Apr 6, 2023
1 parent 7856ec1 commit 6fcba9b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/config/index.md
Expand Up @@ -1355,3 +1355,33 @@ Path to custom tsconfig, relative to the project root.
- **Default**: `300`

The number of milliseconds after which a test is considered slow and reported as such in the results.

### chaiConfig

- **Type:** `{ includeStack?, showDiff?, truncateThreshold? }`
- **Default:** `{ includeStack: false, showDiff: true, truncateThreshold: 40 }`

Equivalent to [Chai config](https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js).

#### chaiConfig.includeStack

- **Type:** `boolean`
- **Default:** `false`

Influences whether stack trace is included in Assertion error message. Default of false suppresses stack trace in the error message.

#### chaiConfig.showDiff

- **Type:** `boolean`
- **Default:** `true`

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.

#### chaiConfig.truncateThreshold

- **Type:** `number`
- **Default:** `40`

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.

This config option affects truncating values in `test.each` titles and inside the assertion error message.
4 changes: 4 additions & 0 deletions packages/vitest/src/integrations/chai/index.ts
Expand Up @@ -80,3 +80,7 @@ Object.defineProperty(globalThis, GLOBAL_EXPECT, {

export { assert, should } from 'chai'
export { chai, globalExpect as expect }
export const setupChaiConfig = (config: ChaiConfig) => {
Object.assign(chai.config, config)
}
export type ChaiConfig = Omit<Partial<typeof chai.config>, 'useProxy' | 'proxyExcludedKeys'>
4 changes: 4 additions & 0 deletions packages/vitest/src/runtime/entry.ts
Expand Up @@ -6,6 +6,7 @@ import { getWorkerState, resetModules } from '../utils'
import { vi } from '../integrations/vi'
import { distDir } from '../paths'
import { startCoverageInsideWorker, stopCoverageInsideWorker, takeCoverageInsideWorker } from '../integrations/coverage'
import { setupChaiConfig } from '../integrations/chai'
import { setupGlobalEnv, withEnv } from './setup.node'
import { rpc } from './rpc'
import type { VitestExecutor } from './execute'
Expand Down Expand Up @@ -71,6 +72,9 @@ export async function run(files: string[], config: ResolvedConfig, environment:

const workerState = getWorkerState()

if (config.chaiConfig)
setupChaiConfig(config.chaiConfig)

const runner = await getTestRunner(config, executor)

// @ts-expect-error untyped global
Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/src/types/config.ts
Expand Up @@ -4,6 +4,7 @@ import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers'
import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner'
import type { BuiltinReporters } from '../node/reporters'
import type { TestSequencerConstructor } from '../node/sequencers/types'
import type { ChaiConfig } from '../integrations/chai'
import type { CoverageOptions, ResolvedCoverageOptions } from './coverage'
import type { JSDOMOptions } from './jsdom-options'
import type { Reporter } from './reporter'
Expand Down Expand Up @@ -562,6 +563,12 @@ export interface InlineConfig {
* Requires `singleThread: true` OR `threads: false`.
*/
inspectBrk?: boolean

/**
* Modify default Chai config. Vitest uses Chai for `expect` and `assert` matches.
* https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js
*/
chaiConfig?: ChaiConfig
}

export interface TypecheckConfig {
Expand Down

0 comments on commit 6fcba9b

Please sign in to comment.