diff --git a/packages/vitest/src/utils/config-helpers.ts b/packages/vitest/src/utils/config-helpers.ts index e170b882de0a..6d63e5a4336e 100644 --- a/packages/vitest/src/utils/config-helpers.ts +++ b/packages/vitest/src/utils/config-helpers.ts @@ -23,14 +23,20 @@ export function getOutputFile(config: PotentialConfig | undefined, reporter: Bui */ export function wrapSerializableConfig(config: ResolvedConfig) { let testNamePattern = config.testNamePattern + let defines = config.defines // v8 serialize does not support regex if (testNamePattern && typeof testNamePattern !== 'string') testNamePattern = `${REGEXP_WRAP_PREFIX}${testNamePattern.toString()}` as unknown as RegExp + // v8 serialize drops properties with undefined value + if (defines) + defines = wrapDefines(defines) + return { ...config, testNamePattern, + defines, } as ResolvedConfig } @@ -45,5 +51,20 @@ export function unwrapSerializableConfig(config: ResolvedConfig) { config.testNamePattern = parseRegexp(testNamePattern.slice(REGEXP_WRAP_PREFIX.length)) } + if (config.defines && Array.isArray(config.defines.keys)) { + const { keys, original } = config.defines as ReturnType + const defines: ResolvedConfig['defines'] = {} + + // Apply all keys from the original. Entries which had undefined value are missing from original now + for (const key of keys) + defines[key] = original[key] + + config.defines = defines + } + return config } + +function wrapDefines(input: Record) { + return { keys: Object.keys(input), original: input } +}