Skip to content

Commit

Permalink
fix(forks): wrap defines to support undefined values
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Feb 23, 2024
1 parent d7c319e commit efe6c09
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/vitest/src/utils/config-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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<typeof wrapDefines>
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<string, unknown>) {
return { keys: Object.keys(input), original: input }
}
11 changes: 11 additions & 0 deletions test/core/test/define-ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { afterAll, expect, test } from 'vitest'
declare let __DEFINE__: string
declare let __JSON__: any
declare let __MODE__: string
declare let __UNDEFINED__: undefined
declare let __NULL__: null
declare let __ZERO__: 0
declare let __FALSE__: false
declare let SOME: {
VARIABLE: string
SOME: {
Expand Down Expand Up @@ -64,3 +68,10 @@ test('dotted defines are processed by Vite, but cannot be reassigned', () => {
SOME.VARIABLE = 'new variable'
expect(SOME.VARIABLE).not.toBe('new variable')
})

test('falsy defines are passed', () => {
expect(__UNDEFINED__).toBe(undefined)
expect(__NULL__).toBe(null)
expect(__ZERO__).toBe(0)
expect(__FALSE__).toBe(false)
})
11 changes: 11 additions & 0 deletions test/core/test/define-web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { afterAll, expect, test } from 'vitest'
declare let __DEFINE__: string
declare let __JSON__: any
declare let __MODE__: string
declare let __UNDEFINED__: undefined
declare let __NULL__: null
declare let __ZERO__: 0
declare let __FALSE__: false
declare let SOME: {
VARIABLE: string
SOME: {
Expand Down Expand Up @@ -61,3 +65,10 @@ test('dotted defines can be reassigned', () => {
SOME.VARIABLE = 'new variable'
expect(SOME.VARIABLE).toBe('new variable')
})

test('falsy defines are passed', () => {
expect(__UNDEFINED__).toBe(undefined)
expect(__NULL__).toBe(null)
expect(__ZERO__).toBe(0)
expect(__FALSE__).toBe(false)
})
4 changes: 4 additions & 0 deletions test/core/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default defineConfig({
'__MODE__': 'process.env.MODE',
'SOME.VARIABLE': '"variable"',
'SOME.SOME.VARIABLE': '"nested variable"',
'__UNDEFINED__': undefined,
'__NULL__': null,
'__ZERO__': 0,
'__FALSE__': false,
},
resolve: {
alias: [
Expand Down

0 comments on commit efe6c09

Please sign in to comment.