Skip to content

Commit

Permalink
test: cleanup testConfig.baseRoute and use testConfig.previewBase (
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Oct 3, 2023
1 parent ad9718d commit bf3cc83
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import {
getColor,
isBuild,
page,
viteConfig,
} from '~utils'

const getBase = () => (viteConfig ? viteConfig?.testConfig?.baseRoute : '')

const absoluteAssetMatch = isBuild
? /http.*\/other-assets\/asset-\w{8}\.png/
: '/nested/asset.png'
Expand Down Expand Up @@ -140,8 +137,7 @@ describe('css url() references', () => {
describe.runIf(isBuild)('index.css URLs', () => {
let css: string
beforeAll(() => {
const base = getBase()
css = findAssetFile(/index.*\.css$/, base, 'other-assets')
css = findAssetFile(/index.*\.css$/, 'relative-base', 'other-assets')
})

test('relative asset URL', () => {
Expand Down Expand Up @@ -203,7 +199,7 @@ test('?url import on css', async () => {
isBuild ? /http.*\/other-assets\/icons-\w{8}\.css/ : '/css/icons.css',
)
isBuild &&
expect(findAssetFile(/index.*\.js$/, getBase(), 'entries')).toMatch(
expect(findAssetFile(/index.*\.js$/, 'relative-base', 'entries')).toMatch(
/icons-.+\.css(?!\?used)/,
)
})
Expand Down
4 changes: 1 addition & 3 deletions playground/assets/__tests__/url-base/url-base-assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getColor,
isBuild,
page,
viteConfig,
} from '~utils'

const urlAssetMatch = isBuild
Expand Down Expand Up @@ -132,8 +131,7 @@ describe('css url() references', () => {
describe.runIf(isBuild)('index.css URLs', () => {
let css: string
beforeAll(() => {
const base = viteConfig ? viteConfig?.testConfig?.baseRoute : ''
css = findAssetFile(/index.*\.css$/, base, 'other-assets')
css = findAssetFile(/index.*\.css$/, 'url-base', 'other-assets')
})

test('use base URL for asset URL', () => {
Expand Down
2 changes: 1 addition & 1 deletion playground/assets/vite.config-relative-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
},
},
testConfig: {
baseRoute: '/relative-base/',
previewBase: '/relative-base/',
},
cacheDir: 'node_modules/.vite-relative-base',
})
3 changes: 0 additions & 3 deletions playground/assets/vite.config-url-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,5 @@ export default defineConfig({
port,
strictPort: true,
},
testConfig: {
baseRoute: '/url-base/',
},
cacheDir: 'node_modules/.vite-url-base',
})
2 changes: 1 addition & 1 deletion playground/css/vite.config-relative-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
},
},
testConfig: {
baseRoute: '/relative-base/',
previewBase: '/relative-base/',
},
cacheDir: 'node_modules/.vite-relative-base',
})
2 changes: 1 addition & 1 deletion playground/legacy/vite.config-no-polyfills-no-systemjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export default defineConfig({
},
},
testConfig: {
baseRoute: '/no-polyfills-no-systemjs/',
previewBase: '/no-polyfills-no-systemjs/',
},
})
2 changes: 1 addition & 1 deletion playground/legacy/vite.config-no-polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export default defineConfig({
},
},
testConfig: {
baseRoute: '/no-polyfills/',
previewBase: '/no-polyfills/',
},
})
38 changes: 15 additions & 23 deletions playground/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ export let testDir: string
* Test folder name
*/
export let testName: string
/**
* current test using vite inline config
* when using serve.[jt]s is not possible to get the config
*/
export let viteConfig: InlineConfig | undefined

export const serverLogs: string[] = []
export const browserLogs: string[] = []
Expand All @@ -79,22 +74,6 @@ export let browser: Browser = undefined!
export let viteTestUrl: string = ''
export let watcher: RollupWatcher | undefined = undefined

declare module 'vite' {
interface InlineConfig {
testConfig?: {
// relative base output use relative path
// rewrite the url to truth file path
baseRoute: string
}
}

interface UserConfig {
testConfig?: {
baseRoute: string
}
}
}

export function setViteUrl(url: string): void {
viteTestUrl = url
}
Expand Down Expand Up @@ -259,7 +238,6 @@ export async function startDefaultServe(): Promise<void> {
if (!isBuild) {
process.env.VITE_INLINE = 'inline-serve'
const testConfig = mergeConfig(options, config || {})
viteConfig = testConfig
viteServer = server = await (await createServer(testConfig)).listen()
// use resolved port/base from server
const devBase = server.config.base
Expand All @@ -278,7 +256,6 @@ export async function startDefaultServe(): Promise<void> {
})
options.plugins = [resolvedPlugin()]
const testConfig = mergeConfig(options, config || {})
viteConfig = testConfig
const rollupOutput = await build(testConfig)
const isWatch = !!resolvedConfig!.build.watch
// in build watch,call startStaticServer after the build is complete
Expand All @@ -289,6 +266,10 @@ export async function startDefaultServe(): Promise<void> {
if (config && config.__test__) {
config.__test__()
}
// TODO: use something like ConfigEnv['cmd'] https://github.com/vitejs/vite/pull/12298
if (config?.testConfig?.previewBase) {
testConfig.base = config.testConfig.previewBase
}
const _nodeEnv = process.env.NODE_ENV
const previewServer = await preview(testConfig)
// prevent preview change NODE_ENV
Expand Down Expand Up @@ -369,5 +350,16 @@ declare module 'vite' {
* runs after build and before preview
*/
__test__?: () => void
/**
* special test only configs
*/
testConfig?: {
/**
* a base used for preview
*
* useful for relative base tests
*/
previewBase?: string
}
}
}
2 changes: 1 addition & 1 deletion playground/worker/vite.config-relative-base-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default defineConfig({
},
},
testConfig: {
baseRoute: '/relative-base-iife/',
previewBase: '/relative-base-iife/',
},
plugins: [workerPluginTestPlugin()],
cacheDir: 'node_modules/.vite-relative-base-iife',
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/vite.config-relative-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default defineConfig({
},
},
testConfig: {
baseRoute: '/relative-base/',
previewBase: '/relative-base/',
},
plugins: [
workerPluginTestPlugin(),
Expand Down

0 comments on commit bf3cc83

Please sign in to comment.