[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat(vitest): add "provide" option (#6253)

authored by

Vladimir and committed by
GitHub
(Aug 9, 2024, 3:15 PM +0200) 4409d779 00dfc1a4

+101 -4
+49 -3
docs/config/index.md
··· 1003 1003 globalThis.resetBeforeEachTest = true 1004 1004 ``` 1005 1005 1006 + ### provide <Version>2.1.0</Version> {#provide} 1007 + 1008 + - **Type:** `Partial<ProvidedContext>` 1009 + 1010 + Define values that can be accessed inside your tests using `inject` method. 1011 + 1012 + :::code-group 1013 + ```ts [vitest.config.js] 1014 + import { defineConfig } from 'vitest/config' 1015 + 1016 + export default defineConfig({ 1017 + test: { 1018 + provide: { 1019 + API_KEY: '123', 1020 + }, 1021 + }, 1022 + }) 1023 + ``` 1024 + ```ts [my.test.js] 1025 + import { expect, inject, test } from 'vitest' 1026 + 1027 + test('api key is defined', () => { 1028 + expect(inject('API_KEY')).toBe('123') 1029 + }) 1030 + ``` 1031 + ::: 1032 + 1033 + ::: warning 1034 + Properties have to be strings and values need to be [serializable](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types) because this object will be transferred between different processes. 1035 + ::: 1036 + 1037 + ::: tip 1038 + If you are using TypeScript, you will need to augment `ProvidedContext` type for type safe access: 1039 + 1040 + ```ts 1041 + // vitest.shims.d.ts 1042 + 1043 + declare module 'vitest' { 1044 + export interface ProvidedContext { 1045 + API_KEY: string 1046 + } 1047 + } 1048 + 1049 + // mark this file as a module so augmentation works correctly 1050 + export {} 1051 + ``` 1052 + ::: 1053 + 1006 1054 ### globalSetup 1007 1055 1008 1056 - **Type:** `string | string[]` ··· 1018 1066 ::: warning 1019 1067 Global setup runs only if there is at least one running test. This means that global setup might start running during watch mode after test file is changed (the test file will wait for global setup to finish before running). 1020 1068 1021 - Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via `provide` method: 1069 + Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via [`provide`](#provide) method: 1022 1070 1023 1071 :::code-group 1024 1072 ```js [globalSetup.js] ··· 1033 1081 provide('wsPort', 3000) 1034 1082 } 1035 1083 1036 - // You can also extend `ProvidedContext` type 1037 - // to have type safe access to `provide/inject` methods: 1038 1084 declare module 'vitest' { 1039 1085 export interface ProvidedContext { 1040 1086 wsPort: number
+2
test/workspaces/globalTest.ts
··· 7 7 globalSetup: boolean 8 8 globalSetupOverriden: boolean 9 9 invalidValue: unknown 10 + projectConfigValue: boolean 11 + globalConfigValue: boolean 10 12 } 11 13 } 12 14
+3
test/workspaces/vitest.config.ts
··· 16 16 CONFIG_VAR: 'root', 17 17 CONFIG_OVERRIDE: 'root', 18 18 }, 19 + provide: { 20 + globalConfigValue: true, 21 + }, 19 22 }, 20 23 })
+2
test/workspaces/space_3/global-provide.space-3-test.ts
··· 3 3 test('global setup provides data correctly', () => { 4 4 expect(inject('globalSetup')).toBe(true) 5 5 expect(inject('globalSetupOverriden')).toBe(true) 6 + expect(inject('projectConfigValue')).toBe(true) 7 + expect(inject('globalConfigValue')).toBe(true) 6 8 expect(inject('invalidValue')).toBe(undefined) 7 9 })
+3
test/workspaces/space_3/vitest.config.ts
··· 5 5 include: ['**/*.space-3-test.ts'], 6 6 environment: 'node', 7 7 globalSetup: './localSetup.ts', 8 + provide: { 9 + projectConfigValue: true, 10 + }, 8 11 }, 9 12 })
+17
packages/vitest/src/node/workspace.ts
··· 364 364 project.server = ctx.server 365 365 project.runner = ctx.runner 366 366 project.config = ctx.config 367 + for (const _providedKey in ctx.config.provide) { 368 + const providedKey = _providedKey as keyof ProvidedContext 369 + // type is very strict here, so we cast it to any 370 + (project.provide as (key: string, value: unknown) => void)( 371 + providedKey, 372 + ctx.config.provide[providedKey], 373 + ) 374 + } 367 375 project.testProject = new TestProject(project) 368 376 return project 369 377 } ··· 384 392 server.config, 385 393 this.ctx.logger, 386 394 ) 395 + for (const _providedKey in this.config.provide) { 396 + const providedKey = _providedKey as keyof ProvidedContext 397 + // type is very strict here, so we cast it to any 398 + (this.provide as (key: string, value: unknown) => void)( 399 + providedKey, 400 + this.config.provide[providedKey], 401 + ) 402 + } 403 + 387 404 this.testProject = new TestProject(this) 388 405 389 406 this.server = server
+1
packages/vitest/src/node/cli/cli-config.ts
··· 794 794 compare: null, 795 795 outputJson: null, 796 796 json: null, 797 + provide: null, 797 798 } 798 799 799 800 export const benchCliOptionsConfig: Pick<
+2
packages/vitest/src/node/config/resolveConfig.ts
··· 140 140 mode, 141 141 } as any as ResolvedConfig 142 142 143 + resolved.provide ??= {} 144 + 143 145 const inspector = resolved.inspect || resolved.inspectBrk 144 146 145 147 resolved.inspector = {
+22 -1
packages/vitest/src/node/types/config.ts
··· 10 10 } from '../reporters' 11 11 import type { TestSequencerConstructor } from '../sequencers/types' 12 12 import type { ChaiConfig } from '../../integrations/chai/config' 13 - import type { Arrayable, ErrorWithDiff, ParsedStack } from '../../types/general' 13 + import type { Arrayable, ErrorWithDiff, ParsedStack, ProvidedContext } from '../../types/general' 14 14 import type { JSDOMOptions } from '../../types/jsdom-options' 15 15 import type { HappyDOMOptions } from '../../types/happy-dom-options' 16 16 import type { EnvironmentOptions } from '../../types/environment' ··· 730 730 */ 731 731 waitForDebugger?: boolean 732 732 } 733 + 734 + /** 735 + * Define variables that will be returned from `inject` in the test environment. 736 + * @example 737 + * ```ts 738 + * // vitest.config.ts 739 + * export default defineConfig({ 740 + * test: { 741 + * provide: { 742 + * someKey: 'someValue' 743 + * } 744 + * } 745 + * }) 746 + * ``` 747 + * ```ts 748 + * // test file 749 + * import { inject } from 'vitest' 750 + * const value = inject('someKey') // 'someValue' 751 + * ``` 752 + */ 753 + provide?: Partial<ProvidedContext> 733 754 734 755 /** 735 756 * Configuration options for expect() matches.