Skip to content

Commit

Permalink
refactor: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
2eha0 committed Jan 9, 2025
1 parent 5042a73 commit c0a326e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
"scripts": {
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "run-s test-serve test-build",
"test-serve": "vitest run -c vitest.config.e2e.ts",
"test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
"playground:dev": "vite -c playground/vite.config.ts",
"playground:build": "vue-tsc -p playground/tsconfig.json && vite -c playground/vite.config.ts build",
"playground:preview": "vite -c playground/vite.config.ts preview",
"test": "vitest run -c vitest.config.e2e.ts",
"typecheck": "vue-tsc --noEmit",
"build": "unbuild",
"commit": "cz"
Expand Down
27 changes: 14 additions & 13 deletions playground/dynamic-import-retry/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { beforeAll, expect, test } from 'vitest'
import { resolve } from 'node:path'
import type { InlineConfig } from 'vite'
import { build, preview } from 'vite'

import { page, browserErrors, browserLogs } from '../vitestSetup'

const root = resolve(__dirname, 'vue-router')
const configFilePath = resolve(root, 'vite.config.ts')

let viteTestUrl: string

beforeAll(async () => {
const config = { root, configFile: configFilePath }
await build(config)
const previewServer = await preview(config)
const testConfig: InlineConfig = {
configFile: resolve(root, 'vite.config.ts'),
}

await build(testConfig)
const previewServer = await preview(testConfig)

viteTestUrl = previewServer.resolvedUrls!.local[0]
await page.goto(viteTestUrl)
Expand All @@ -28,32 +31,30 @@ test('should dynamic import module successful', async () => {
expect(await page.textContent('h2')).toMatch('AboutView')
})

const resources = ['js', 'css']
const resources = ['css', 'js']

for (const resourceType of resources) {
const filter = (url: URL) => url.pathname.includes('AboutView') && url.pathname.includes(`.${resourceType}`)

test('should recover by retrying import ' + resourceType, async () => {
await page.click('#nav-home')
await page.reload()
await page.goto(viteTestUrl)

let requestCount = 0
let retries = 0
await page.route(filter, route => {
requestCount++
if (requestCount < 3) {
retries++
if (retries < 3) {
return route.fulfill({ status: 404, body: 'Not Found' })
}
return route.continue()
})
await page.click('#nav-about')
await page.waitForTimeout(1000)
expect(await page.textContent('h2')).toMatch('AboutView')
expect(requestCount).toBe(3)
expect(retries).toBe(3)
})

test('should has an error when reach max attempts on loading ' + resourceType, async () => {
await page.click('#nav-home')
await page.reload()
await page.goto(viteTestUrl)

await page.route(filter, route => {
return route.fulfill({ status: 404, body: 'Not Found' })
Expand Down
8 changes: 6 additions & 2 deletions playground/dynamic-import-retry/vue-router/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'node:path'

import { DynamicImportRetryPlugin } from '../../../src/plugin-dynamic-import-retry'

// https://vite.dev/config/
export default defineConfig({
root: 'playground',
logLevel: 'silent',
root: resolve(__dirname),
plugins: [
vue(),
DynamicImportRetryPlugin(),
],
build: {
minify: false,
outDir: 'dist',
emptyOutDir: true,
target: 'esnext',
emptyOutDir: false,
},
})
4 changes: 0 additions & 4 deletions playground/vitestGlobalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ const DIR = path.join(os.tmpdir(), 'vitest_playwright_global_setup')

let browserServer: BrowserServer | undefined

/**
* Sets up the environment for testing by launching a Chromium browser server and
* writing the WebSocket endpoint to a file.
*/
export async function setup(): Promise<void> {
browserServer = await chromium.launchServer({
headless: !process.env.VITE_DEBUG_SERVE,
Expand Down
1 change: 0 additions & 1 deletion playground/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ beforeAll(async (s) => {
// ignore favicon request in headed browser
if (
process.env.VITE_DEBUG_SERVE &&
msg.text().includes('Failed to load resource:') &&
msg.location().url.includes('favicon.ico')
) {
return
Expand Down

0 comments on commit c0a326e

Please sign in to comment.