Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add chai config #3066

Merged
merged 4 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1386,3 +1386,45 @@ 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?, useProxy?, proxyExcludedKeys? }`
- **Default:** `{ includeStack: false, showDiff: true, truncateThreshold: 40, useProxy: true, proxyExcludedKeys: ['then', 'catch', 'inspect', 'toJSON'] }`

Equivalent to [chai config](https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js). You can override it in your `vitest.config.ts` file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Equivalent to [chai config](https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js). You can override it in your `vitest.config.ts` file.
Equivalent to [Chai config](https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js).

This is implied.


#### 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 be shown.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 be shown.
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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.


#### chaiConfig.useProxy

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

Defines if chai will use a Proxy to throw an error when a non-existent property is read, which protects users from typos when using property-based assertions.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed, because Vitest requires Proxy to exist, so the config is irrelevant.


#### chaiConfig.proxyExcludedKeys
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

- **Type:** `string[]`
- **Default:** `['then', 'catch', 'inspect', 'toJSON']`

Defines which properties should be ignored instead of throwing an error if they do not exist on the assertion. This is only applied if the environment Chai is running in supports proxies and if the `useProxy` configuration setting is enabled. By default, `then` and `inspect` will not throw an error if they do not exist on the assertion object because the `.inspect` property is read by `util.inspect` (for example, when using `console.log` on the assertion object) and `.then` is necessary for promise type-checking.
4 changes: 4 additions & 0 deletions packages/vitest/src/integrations/chai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ Object.defineProperty(globalThis, GLOBAL_EXPECT, {

export { assert, should } from 'chai'
export { chai, globalExpect as expect }
export const setupConfig = (config: chaiConfig) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const setupConfig = (config: chaiConfig) => {
export const setupChaiConfig = (config: chaiConfig) => {

Object.assign(chai.config, config)
}
export type chaiConfig = Partial<typeof chai.config>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export type chaiConfig = Partial<typeof chai.config>
export type ChaiConfig = Partial<typeof chai.config>

4 changes: 4 additions & 0 deletions packages/vitest/src/runtime/entry.ts
Original file line number Diff line number Diff line change
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 { setupConfig } 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)
setupConfig(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
Original file line number Diff line number Diff line change
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 @@ -583,6 +584,12 @@ export interface InlineConfig {
* Requires `singleThread: true` OR `threads: false`.
*/
inspectBrk?: boolean

/**
* chai config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* chai config
* Modify default Chai config. Vitest uses Chai for `expect` and `assert` matchers.

* https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js
*/
chaiConfig?: chaiConfig
}

export interface TypecheckConfig {
Expand Down