Skip to content

Commit

Permalink
test: remove runInBuild flag from editFile and untilUpdated (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 26, 2024
1 parent 2e368a6 commit ad9e001
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion playground/react-classic/__tests__/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions playground/react-emotion/__tests__/react.spec.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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'),
)
Expand Down Expand Up @@ -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')
}
})
2 changes: 1 addition & 1 deletion playground/react/__tests__/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)
Expand Down
11 changes: 7 additions & 4 deletions playground/ssr-react/__tests__/ssr-react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<h1>About', '<h1>changed'),
)
await untilUpdated(() => page.textContent('h1'), 'changed')

if (!isBuild) {
editFile('src/pages/About.jsx', (code) =>
code.replace('<h1>About', '<h1>changed'),
)
await untilUpdated(() => page.textContent('h1'), 'changed')
}
})
6 changes: 1 addition & 5 deletions playground/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand All @@ -92,9 +90,7 @@ export function removeFile(filename: string): void {
export async function untilUpdated(
poll: () => string | Promise<string>,
expected: string,
runInBuild = false,
): Promise<void> {
if (isBuild && !runInBuild) return
const maxTries = process.env.CI ? 100 : 50
for (let tries = 0; tries < maxTries; tries++) {
const actual = (await poll()) ?? ''
Expand Down

0 comments on commit ad9e001

Please sign in to comment.