From 066a39be7984fa2cb2fa74667f84952d0d4ca94b Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 8 Nov 2022 11:22:08 +0100 Subject: [PATCH 1/4] feat: add `vi.setConfig` helper --- docs/api/index.md | 12 ++++++++++++ packages/vitest/src/integrations/vi.ts | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/docs/api/index.md b/docs/api/index.md index d3547279cdb9..a744cc5c4efe 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -2598,6 +2598,12 @@ Vitest provides utility functions to help you out through it's **vi** helper. Yo Will call [`.mockReset()`](/api/#mockreset) on all spies. This will clear mock history and reset its implementation to an empty function (will return `undefined`). +### vi.resetConfig + +- **Type**: `RuntimeConfig` + + If [`vi.setConfig`](/api/#vi-setconfig) was called before, this will reset config to the original state. + ### vi.resetModules - **Type**: `() => Vitest` @@ -2691,6 +2697,12 @@ Vitest provides utility functions to help you out through it's **vi** helper. Yo vi.useRealTimers() ``` +### vi.setConfig + +- **Type**: `RuntimeConfig` + + Updates config for the current test file. You can only affect values that are used, when executing tests. + ### vi.spyOn - **Type:** `(object: T, method: K, accessType?: 'get' | 'set') => MockInstance` diff --git a/packages/vitest/src/integrations/vi.ts b/packages/vitest/src/integrations/vi.ts index 086926d24bac..5ed45968f469 100644 --- a/packages/vitest/src/integrations/vi.ts +++ b/packages/vitest/src/integrations/vi.ts @@ -1,6 +1,7 @@ import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers' import { parseStacktrace } from '../utils/source-map' import type { VitestMocker } from '../runtime/mocker' +import type { ResolvedConfig, RuntimeConfig } from '../types' import { getWorkerState, resetModules, setTimeout } from '../utils' import { FakeTimers } from './mock/timers' import type { EnhancedSpy, MaybeMocked, MaybeMockedDeep, MaybePartiallyMocked, MaybePartiallyMockedDeep } from './spy' @@ -260,6 +261,28 @@ class VitestUtils { // like in import('./example').then(...) await new Promise(resolve => setTimeout(resolve, 1)).then(() => Promise.resolve()) } + + private _config: null | ResolvedConfig = null + + /** + * Updates runtime config. You can only change values that are used when executing tests. + */ + public setConfig(config: RuntimeConfig) { + const state = getWorkerState() + if (!this._config) + this._config = { ...state.config } + Object.assign(state.config, config) + } + + /** + * If config was changed with `vi.setConfig`, this will reset it to the original state. + */ + public resetConfig() { + if (this._config) { + const state = getWorkerState() + state.config = { ...this._config } + } + } } export const vitest = new VitestUtils() From cb270883c6a5b8636ccb4509babbdcf32ee27052 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 8 Nov 2022 11:23:39 +0100 Subject: [PATCH 2/4] chore: add types for setConfig --- packages/vitest/src/runtime/entry.ts | 4 ++++ packages/vitest/src/types/config.ts | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/vitest/src/runtime/entry.ts b/packages/vitest/src/runtime/entry.ts index be24e7508851..fa43ceb5fca4 100644 --- a/packages/vitest/src/runtime/entry.ts +++ b/packages/vitest/src/runtime/entry.ts @@ -1,6 +1,7 @@ import { promises as fs } from 'fs' import type { EnvironmentOptions, ResolvedConfig, VitestEnvironment } from '../types' import { getWorkerState, resetModules } from '../utils' +import { vi } from '../integrations/vi' import { envs } from '../integrations/env' import { setupGlobalEnv, withEnv } from './setup' import { startTests } from './run' @@ -75,6 +76,9 @@ export async function run(files: string[], config: ResolvedConfig): Promise, 'config' | 'f typecheck: TypecheckConfig } + +export type RuntimeConfig = Pick< + UserConfig, + | 'allowOnly' + | 'testTimeout' + | 'hookTimeout' + | 'clearMocks' + | 'mockReset' + | 'restoreMocks' + | 'fakeTimers' + | 'maxConcurrency' +> & { sequence?: { hooks?: SequenceHooks } } From 29abaa4ff10535283693ebf5d6d3f82388a9ec24 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 8 Nov 2022 11:29:34 +0100 Subject: [PATCH 3/4] chore: remove SequenceHooks --- packages/vitest/src/types/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 710e6ed77ed2..256061aaea1b 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -569,4 +569,4 @@ export type RuntimeConfig = Pick< | 'restoreMocks' | 'fakeTimers' | 'maxConcurrency' -> & { sequence?: { hooks?: SequenceHooks } } +> From 37d5657367b696b0d09d1a5950e59421bd3e3c95 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 8 Nov 2022 12:07:02 +0100 Subject: [PATCH 4/4] test: add tets for changing config --- test/core/test/vi.spec.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/core/test/vi.spec.ts b/test/core/test/vi.spec.ts index a6b08a1812e8..c55f8bb1ca26 100644 --- a/test/core/test/vi.spec.ts +++ b/test/core/test/vi.spec.ts @@ -4,6 +4,7 @@ import type { MockedFunction, MockedObject } from 'vitest' import { describe, expect, test, vi } from 'vitest' +import { getWorkerState } from 'vitest/src/utils' const expectType = (obj: T) => obj @@ -64,6 +65,18 @@ describe('testing vi utils', () => { }) }) + test('can change config', () => { + const state = getWorkerState() + expect(state.config.hookTimeout).toBe(10000) + expect(state.config.clearMocks).toBe(false) + vi.setConfig({ hookTimeout: 6000, clearMocks: true }) + expect(state.config.hookTimeout).toBe(6000) + expect(state.config.clearMocks).toBe(true) + vi.resetConfig() + expect(state.config.hookTimeout).toBe(10000) + expect(state.config.clearMocks).toBe(false) + }) + // TODO: it's unstable in CI, skip until resolved test.skip('loads unloaded module', async () => { let mod: any