From 6fcba9ba1641b01fcb68a2c67b931179e75833d3 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Thu, 6 Apr 2023 14:17:01 +0800 Subject: [PATCH] feat: add chai config (#3066) --- docs/config/index.md | 30 +++++++++++++++++++ .../vitest/src/integrations/chai/index.ts | 4 +++ packages/vitest/src/runtime/entry.ts | 4 +++ packages/vitest/src/types/config.ts | 7 +++++ 4 files changed, 45 insertions(+) diff --git a/docs/config/index.md b/docs/config/index.md index 7e402953740a..4561d8fb3cd6 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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. diff --git a/packages/vitest/src/integrations/chai/index.ts b/packages/vitest/src/integrations/chai/index.ts index acae961d23d1..66879712f791 100644 --- a/packages/vitest/src/integrations/chai/index.ts +++ b/packages/vitest/src/integrations/chai/index.ts @@ -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, 'useProxy' | 'proxyExcludedKeys'> diff --git a/packages/vitest/src/runtime/entry.ts b/packages/vitest/src/runtime/entry.ts index 901709ddcb9e..25136db1101a 100644 --- a/packages/vitest/src/runtime/entry.ts +++ b/packages/vitest/src/runtime/entry.ts @@ -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' @@ -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 diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 19dac39be761..b35732585ec9 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -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' @@ -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 {