From ad9e001c0d1a7a6d507640414e71443ee9d1593d Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 27 Dec 2024 08:30:38 +0900 Subject: [PATCH] test: remove `runInBuild` flag from `editFile` and `untilUpdated` (#395) --- .../react-classic/__tests__/react.spec.ts | 2 +- .../react-emotion/__tests__/react.spec.ts | 18 ++++++++++-------- playground/react/__tests__/react.spec.ts | 2 +- .../ssr-react/__tests__/ssr-react.spec.ts | 11 +++++++---- playground/test-utils.ts | 6 +----- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/playground/react-classic/__tests__/react.spec.ts b/playground/react-classic/__tests__/react.spec.ts index e40aa8c2..33fe7dac 100644 --- a/playground/react-classic/__tests__/react.spec.ts +++ b/playground/react-classic/__tests__/react.spec.ts @@ -11,7 +11,7 @@ test('should update', async () => { expect(await page.textContent('button')).toMatch('count is: 1') }) -test('should hmr', async () => { +test.runIf(isServe)('should hmr', async () => { editFile('App.jsx', (code) => code.replace('Vite + React', 'Updated')) await untilUpdated(() => page.textContent('h1'), 'Hello Updated') // preserve state diff --git a/playground/react-emotion/__tests__/react.spec.ts b/playground/react-emotion/__tests__/react.spec.ts index 222c9a74..dc98fb37 100644 --- a/playground/react-emotion/__tests__/react.spec.ts +++ b/playground/react-emotion/__tests__/react.spec.ts @@ -1,5 +1,5 @@ import { expect, test } from 'vitest' -import { editFile, getColor, page, untilUpdated } from '~utils' +import { editFile, getColor, isServe, page, untilUpdated } from '~utils' test('should render', async () => { expect(await page.textContent('h1')).toMatch( @@ -13,7 +13,7 @@ test('should update', async () => { expect(await page.textContent('button')).toMatch('count is: 1') }) -test('should hmr', async () => { +test.runIf(isServe)('should hmr', async () => { editFile('App.jsx', (code) => code.replace('Vite + React + @emotion/react', 'Updated'), ) @@ -42,12 +42,14 @@ test('should update button style', async () => { expect(await getButtonBorderStyle()).toMatch('2px solid rgb(0, 0, 0)') - editFile('Counter.jsx', (code) => - code.replace('border: 2px solid #000', 'border: 4px solid red'), - ) + if (isServe) { + editFile('Counter.jsx', (code) => + code.replace('border: 2px solid #000', 'border: 4px solid red'), + ) - await untilUpdated(getButtonBorderStyle, '4px solid rgb(255, 0, 0)') + await untilUpdated(getButtonBorderStyle, '4px solid rgb(255, 0, 0)') - // preserve state - expect(await page.textContent('button')).toMatch('count is: 1') + // preserve state + expect(await page.textContent('button')).toMatch('count is: 1') + } }) diff --git a/playground/react/__tests__/react.spec.ts b/playground/react/__tests__/react.spec.ts index bec1b007..3c17d963 100644 --- a/playground/react/__tests__/react.spec.ts +++ b/playground/react/__tests__/react.spec.ts @@ -18,7 +18,7 @@ test('should update', async () => { expect(await page.textContent('#state-button')).toMatch('count is: 1') }) -test('should hmr', async () => { +test.runIf(isServe)('should hmr', async () => { editFile('App.jsx', (code) => code.replace('Vite + React', 'Vite + React Updated'), ) diff --git a/playground/ssr-react/__tests__/ssr-react.spec.ts b/playground/ssr-react/__tests__/ssr-react.spec.ts index e5d5e3db..9d3b2cc4 100644 --- a/playground/ssr-react/__tests__/ssr-react.spec.ts +++ b/playground/ssr-react/__tests__/ssr-react.spec.ts @@ -70,8 +70,11 @@ test('client navigation', async () => { await untilUpdated(() => page.textContent('a[href="/about"]'), 'About') await page.click('a[href="/about"]') await untilUpdated(() => page.textContent('h1'), 'About') - editFile('src/pages/About.jsx', (code) => - code.replace('

About', '

changed'), - ) - await untilUpdated(() => page.textContent('h1'), 'changed') + + if (!isBuild) { + editFile('src/pages/About.jsx', (code) => + code.replace('

About', '

changed'), + ) + await untilUpdated(() => page.textContent('h1'), 'changed') + } }) diff --git a/playground/test-utils.ts b/playground/test-utils.ts index d404d438..dabc63c0 100644 --- a/playground/test-utils.ts +++ b/playground/test-utils.ts @@ -5,7 +5,7 @@ import fs from 'node:fs' import path from 'node:path' import type { ConsoleMessage, ElementHandle } from 'playwright-chromium' import { expect } from 'vitest' -import { isBuild, page, testDir } from './vitestSetup' +import { page, testDir } from './vitestSetup' export * from './vitestSetup' @@ -69,9 +69,7 @@ export function readFile(filename: string): string { export function editFile( filename: string, replacer: (str: string) => string, - runInBuild: boolean = false, ): void { - if (isBuild && !runInBuild) return filename = path.resolve(testDir, filename) const content = fs.readFileSync(filename, 'utf-8') const modified = replacer(content) @@ -92,9 +90,7 @@ export function removeFile(filename: string): void { export async function untilUpdated( poll: () => string | Promise, expected: string, - runInBuild = false, ): Promise { - if (isBuild && !runInBuild) return const maxTries = process.env.CI ? 100 : 50 for (let tries = 0; tries < maxTries; tries++) { const actual = (await poll()) ?? ''