feat: warn if `envPrefix` contains spaces (#21292) · vitejs/vite@9fcde3c

Original file line numberDiff line numberDiff line change

@@ -1,7 +1,7 @@

11

import http from 'node:http'

22

import path from 'node:path'

33

import fs from 'node:fs'

4-

import { afterEach, describe, expect, test } from 'vitest'

4+

import { afterEach, describe, expect, test, vi } from 'vitest'

55

import type { InlineConfig, PluginOption } from '..'

66

import type { UserConfig, UserConfigExport } from '../config'

77

import { defineConfig, loadConfigFromFile, resolveConfig } from '../config'

@@ -632,9 +632,22 @@ describe('resolveEnvPrefix', () => {

632632

expect(() => resolveEnvPrefix(config)).toThrow()

633633

})

634634
635+

test(`show a warning message if envPrefix contains a whitespace`, () => {

636+

const consoleWarnSpy = vi

637+

.spyOn(console, 'warn')

638+

.mockImplementation(() => {})

639+

let config: UserConfig = { envPrefix: 'WITH SPACE' }

640+

resolveEnvPrefix(config)

641+

expect(consoleWarnSpy).toHaveBeenCalled()

642+

config = { envPrefix: ['CUSTOM_', 'ANOTHER SPACE'] }

643+

resolveEnvPrefix(config)

644+

expect(consoleWarnSpy).toHaveBeenCalledTimes(2)

645+

consoleWarnSpy.mockRestore()

646+

})

647+
635648

test('should work correctly for valid envPrefix value', () => {

636-

const config: UserConfig = { envPrefix: [' ', 'CUSTOM_'] }

637-

expect(resolveEnvPrefix(config)).toMatchObject([' ', 'CUSTOM_'])

649+

const config: UserConfig = { envPrefix: ['CUSTOM_'] }

650+

expect(resolveEnvPrefix(config)).toMatchObject(['CUSTOM_'])

638651

})

639652

})

640653