From b1961f0405083726e553370c742d54b58b7e0c89 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 8 Nov 2022 13:52:25 +0100 Subject: [PATCH] feat: add `vi.setConfig` helper (#2293) * feat: add `vi.setConfig` helper * chore: add types for setConfig * chore: remove SequenceHooks * test: add tets for changing config --- docs/api/index.md | 12 ++++++++++++ packages/vitest/src/integrations/vi.ts | 23 +++++++++++++++++++++++ packages/vitest/src/runtime/entry.ts | 4 ++++ packages/vitest/src/types/config.ts | 12 ++++++++++++ test/core/test/vi.spec.ts | 13 +++++++++++++ 5 files changed, 64 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() 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' +> 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