Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cleanup testConfig.baseRoute and use testConfig.previewBase #14475

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 21 additions & 3 deletions playground/assets/vite.config-relative-base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineConfig } from 'vite'
import baseConfig from './vite.config.js'

let isTestHookCalled = false

export default defineConfig({
...baseConfig,
base: './', // relative base to make dist portable
Expand All @@ -18,8 +20,24 @@ export default defineConfig({
},
},
},
testConfig: {
baseRoute: '/relative-base/',
},
cacheDir: 'node_modules/.vite-relative-base',
__test__() {
// process.argv is different when running tests
// so use this hook instead
isTestHookCalled = true
},
plugins: [
{
name: 'set-base-if-preview',
config() {
// TODO: use something like ConfigEnv['cmd'] https://github.com/vitejs/vite/pull/12298
const isPreview = isTestHookCalled || process.argv.includes('preview')
if (isPreview) {
return {
base: '/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/',
},
Comment on lines -32 to -34
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not using relative path, so this can be removed.

cacheDir: 'node_modules/.vite-url-base',
})
3 changes: 0 additions & 3 deletions playground/css/vite.config-relative-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@ export default defineConfig({
},
},
},
testConfig: {
baseRoute: '/relative-base/',
},
cacheDir: 'node_modules/.vite-relative-base',
})
3 changes: 0 additions & 3 deletions playground/legacy/vite.config-no-polyfills-no-systemjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,4 @@ export default defineConfig({
},
},
},
testConfig: {
baseRoute: '/no-polyfills-no-systemjs/',
},
})
3 changes: 0 additions & 3 deletions playground/legacy/vite.config-no-polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ export default defineConfig({
},
},
},
testConfig: {
baseRoute: '/no-polyfills/',
},
})
23 changes: 0 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 Down
26 changes: 22 additions & 4 deletions playground/worker/vite.config-relative-base-iife.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineConfig } from 'vite'
import workerPluginTestPlugin from './worker-plugin-test-plugin'

let isTestHookCalled = false

export default defineConfig({
base: './',
resolve: {
Expand Down Expand Up @@ -29,9 +31,25 @@ export default defineConfig({
},
},
},
testConfig: {
baseRoute: '/relative-base-iife/',
},
plugins: [workerPluginTestPlugin()],
plugins: [
{
name: 'set-base-if-preview',
config() {
// TODO: use something like ConfigEnv['cmd'] https://github.com/vitejs/vite/pull/12298
const isPreview = isTestHookCalled || process.argv.includes('preview')
if (isPreview) {
return {
base: '/relative-base/',
}
}
},
},
workerPluginTestPlugin(),
],
cacheDir: 'node_modules/.vite-relative-base-iife',
__test__() {
// process.argv is different when running tests
// so use this hook instead
isTestHookCalled = true
},
})
22 changes: 19 additions & 3 deletions playground/worker/vite.config-relative-base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineConfig } from 'vite'
import workerPluginTestPlugin from './worker-plugin-test-plugin'

let isTestHookCalled = false

export default defineConfig({
base: './',
resolve: {
Expand Down Expand Up @@ -29,10 +31,19 @@ export default defineConfig({
},
},
},
testConfig: {
baseRoute: '/relative-base/',
},
plugins: [
{
name: 'set-base-if-preview',
config() {
// TODO: use something like ConfigEnv['cmd'] https://github.com/vitejs/vite/pull/12298
const isPreview = isTestHookCalled || process.argv.includes('preview')
if (isPreview) {
return {
base: '/relative-base/',
}
}
},
},
workerPluginTestPlugin(),
{
name: 'resolve-format-es',
Expand All @@ -47,4 +58,9 @@ export default defineConfig({
},
],
cacheDir: 'node_modules/.vite-relative-base',
__test__() {
// process.argv is different when running tests
// so use this hook instead
isTestHookCalled = true
},
})