···22872287- **Type:** `Partial<NodeJS.ProcessEnv>`
2288228822892289Environment variables available on `process.env` and `import.meta.env` during tests. These variables will not be available in the main process (in `globalSetup`, for example).
22902290+22912291+### expect
22922292+22932293+- **Type:** `ExpectOptions`
22942294+22952295+#### expect.requireAssertions
22962296+22972297+- **Type:** `boolean`
22982298+- **Default:** `false`
22992299+23002300+The same as calling [`expect.hasAssertions()`](/api/expect#expect-hasassertions) at the start of every test. This makes sure that no test will pass accidentally.
23012301+23022302+::: tip
23032303+This only works with Vitest's `expect`. If you use `assert` ot `.should` assertions, they will not count, and your test will fail due to the lack of expect assertions.
23042304+23052305+You can change the value of this by calling `vi.setConfig({ expect: { requireAssertions: false } })`. The config will be applied to every subsequent `expect` call until the `vi.resetConfig` is called manually.
23062306+:::
23072307+23082308+#### expect.poll
23092309+23102310+Global configuration options for [`expect.poll`](/api/expect#poll). These are the same options you can pass down to `expect.poll(condition, options)`.
23112311+23122312+##### expect.poll.interval
23132313+23142314+- **Type:** `number`
23152315+- **Default:** `50`
23162316+23172317+Polling interval in milliseconds
23182318+23192319+##### expect.poll.timeout
23202320+23212321+- **Type:** `number`
23222322+- **Default:** `1000`
23232323+23242324+Polling timeout in milliseconds
+3
docs/guide/cli-table.md
···114114| `--slowTestThreshold <threshold>` | Threshold in milliseconds for a test to be considered slow (default: `300`) |
115115| `--teardownTimeout <timeout>` | Default timeout of a teardown function in milliseconds (default: `10000`) |
116116| `--maxConcurrency <number>` | Maximum number of concurrent tests in a suite (default: `5`) |
117117+| `--expect.requireAssertions` | Require that all tests have at least one assertion |
118118+| `--expect.poll.interval <interval>` | Poll interval in milliseconds for `expect.poll()` assertions (default: `50`) |
119119+| `--expect.poll.timeout <timeout>` | Poll timeout in milliseconds for `expect.poll()` assertions (default: `1000`) |
117120| `--run` | Disable watch mode |
118121| `--no-color` | Removes colors from the console output |
119122| `--clearScreen` | Clear terminal screen when re-running tests during watch mode (default: `true`) |
+18
test/core/test/cli-test.test.ts
···316316 expect(getCLIOptions('--merge-reports different-folder')).toEqual({ mergeReports: 'different-folder' })
317317})
318318319319+test('configure expect', () => {
320320+ expect(() => getCLIOptions('vitest --expect.poll=1000')).toThrowErrorMatchingInlineSnapshot(`[Error: Unexpected value for --expect.poll: true. If you need to configure timeout, use --expect.poll.timeout=<timeout>]`)
321321+ expect(() => getCLIOptions('vitest --expect=1000')).toThrowErrorMatchingInlineSnapshot(`[Error: Unexpected value for --expect: true. If you need to configure expect options, use --expect.{name}=<value> syntax]`)
322322+ expect(getCLIOptions('vitest --expect.poll.interval=100 --expect.poll.timeout=300')).toEqual({
323323+ expect: {
324324+ poll: {
325325+ interval: 100,
326326+ timeout: 300,
327327+ },
328328+ },
329329+ })
330330+ expect(getCLIOptions('vitest --expect.requireAssertions')).toEqual({
331331+ expect: {
332332+ requireAssertions: true,
333333+ },
334334+ })
335335+})
336336+319337test('public parseCLI works correctly', () => {
320338 expect(parseCLI('vitest dev')).toEqual({
321339 filter: [],
+1-1
test/core/test/web-worker-node.test.ts
···263263 worker.port.close()
264264 await new Promise((resolve) => {
265265 worker.port.addEventListener('message', () => {
266266- expect.fail('should not trigger message')
266266+ expect.unreachable('should not trigger message')
267267 })
268268 worker.port.postMessage('event')
269269 setTimeout(resolve, 100)
+2
packages/vitest/src/node/config.ts
···179179 throw new Error(`You cannot set "coverage.reportsDirectory" as ${reportsDirectory}. Vitest needs to be able to remove this directory before test run`)
180180 }
181181182182+ resolved.expect ??= {}
183183+182184 resolved.deps ??= {}
183185 resolved.deps.moduleDirectories ??= []
184186 resolved.deps.moduleDirectories = resolved.deps.moduleDirectories.map((dir) => {
···158158"
159159`;
160160161161+exports[`stacktraces should respect sourcemaps > require-assertions.test.js > require-assertions.test.js 1`] = `
162162+" ❯ require-assertions.test.js:3:1
163163+ 1| import { test } from 'vitest'
164164+ 2|
165165+ 3| test('assertion is not called', () => {
166166+ | ^
167167+ 4| // no expect
168168+ 5| })
169169+"
170170+`;
171171+161172exports[`stacktraces should respect sourcemaps > reset-modules.test.ts > reset-modules.test.ts 1`] = `
162173" ❯ reset-modules.test.ts:16:26
163174 14| expect(2 + 1).eq(3)
+8-1
packages/vitest/src/integrations/chai/poll.ts
···11import * as chai from 'chai'
22import type { ExpectStatic } from '@vitest/expect'
33import { getSafeTimers } from '@vitest/utils'
44+import { getWorkerState } from '../../utils'
4556// these matchers are not supported because they don't make sense with poll
67const unsupported = [
···26272728export function createExpectPoll(expect: ExpectStatic): ExpectStatic['poll'] {
2829 return function poll(fn, options = {}) {
2929- const { interval = 50, timeout = 1000, message } = options
3030+ const state = getWorkerState()
3131+ const defaults = state.config.expect?.poll ?? {}
3232+ const {
3333+ interval = defaults.interval ?? 50,
3434+ timeout = defaults.timeout ?? 1000,
3535+ message,
3636+ } = options
3037 // @ts-expect-error private poll access
3138 const assertion = expect(null, message).withContext({ poll: true }) as Assertion
3239 const proxy: any = new Proxy(assertion, {
+33
packages/vitest/src/node/cli/cli-config.ts
···585585 description: 'Maximum number of concurrent tests in a suite (default: `5`)',
586586 argument: '<number>',
587587 },
588588+ expect: {
589589+ description: 'Configuration options for `expect()` matches',
590590+ argument: '', // no displayed
591591+ subcommands: {
592592+ requireAssertions: {
593593+ description: 'Require that all tests have at least one assertion',
594594+ },
595595+ poll: {
596596+ description: 'Default options for `expect.poll()`',
597597+ argument: '',
598598+ subcommands: {
599599+ interval: {
600600+ description: 'Poll interval in milliseconds for `expect.poll()` assertions (default: `50`)',
601601+ argument: '<interval>',
602602+ },
603603+ timeout: {
604604+ description: 'Poll timeout in milliseconds for `expect.poll()` assertions (default: `1000`)',
605605+ argument: '<timeout>',
606606+ },
607607+ },
608608+ transform(value) {
609609+ if (typeof value !== 'object')
610610+ throw new Error(`Unexpected value for --expect.poll: ${value}. If you need to configure timeout, use --expect.poll.timeout=<timeout>`)
611611+ return value
612612+ },
613613+ },
614614+ },
615615+ transform(value) {
616616+ if (typeof value !== 'object')
617617+ throw new Error(`Unexpected value for --expect: ${value}. If you need to configure expect options, use --expect.{name}=<value> syntax`)
618618+ return value
619619+ },
620620+ },
588621589622 // CLI only options
590623 run: {
+7
packages/vitest/src/runtime/runners/test.ts
···1515 private __vitest_executor!: VitestExecutor
1616 private cancelRun = false
17171818+ private assertionsErrors = new WeakMap<Readonly<Task>, Error>()
1919+1820 constructor(public config: ResolvedConfig) {}
19212022 importFile(filepath: string, source: VitestRunnerImportSource): unknown {
···123125 throw expectedAssertionsNumberErrorGen!()
124126 if (isExpectingAssertions === true && assertionCalls === 0)
125127 throw isExpectingAssertionsError
128128+ if (this.config.expect.requireAssertions && assertionCalls === 0)
129129+ throw this.assertionsErrors.get(test)
126130 }
127131128132 extendTaskContext<T extends Test | Custom>(context: TaskContext<T>): ExtendedContext<T> {
133133+ // create error during the test initialization so we have a nice stack trace
134134+ if (this.config.expect.requireAssertions)
135135+ this.assertionsErrors.set(context.task, new Error('expected any number of assertion, but got none'))
129136 let _expect: ExpectStatic | undefined
130137 Object.defineProperty(context, 'expect', {
131138 get() {