Skip to content

Commit

Permalink
test: address conflicts in port usage for browser-related test suites. (
Browse files Browse the repository at this point in the history
  • Loading branch information
marpme authored Oct 16, 2023
1 parent 4cee671 commit 2396858
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
8 changes: 5 additions & 3 deletions examples/playwright/test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import { chromium } from 'playwright'
import type { Browser, Page } from 'playwright'
import { expect } from '@playwright/test'

const PORT = 3001

// unstable in Windows, TODO: investigate
describe.runIf(process.platform !== 'win32')('basic', async () => {
let server: PreviewServer
let browser: Browser
let page: Page

beforeAll(async () => {
server = await preview({ preview: { port: 3000 } })
browser = await chromium.launch()
server = await preview({ preview: { port: PORT } })
browser = await chromium.launch({ headless: true })
page = await browser.newPage()
})

Expand All @@ -25,7 +27,7 @@ describe.runIf(process.platform !== 'win32')('basic', async () => {
})

test('should change count when button clicked', async () => {
await page.goto('http://localhost:3000')
await page.goto(`http://localhost:${PORT}`)
const button = page.getByRole('button', { name: /Clicked/ })
await expect(button).toBeVisible()

Expand Down
32 changes: 14 additions & 18 deletions examples/puppeteer/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import { preview } from 'vite'
import type { PreviewServer } from 'vite'
import puppeteer from 'puppeteer'
import { launch } from 'puppeteer'
import type { Browser, Page } from 'puppeteer'

const PORT = 3000

describe('basic', async () => {
let server: PreviewServer
let browser: Browser
let page: Page

beforeAll(async () => {
server = await preview({ preview: { port: 3000 } })
browser = await puppeteer.launch()
server = await preview({ preview: { port: PORT } })
browser = await launch({ headless: true })
page = await browser.newPage()
})

Expand All @@ -22,22 +24,16 @@ describe('basic', async () => {
})
})

// TODO make more stable
test.skip('should have the correct title', async () => {
try {
await page.goto('http://localhost:3000')
const button = (await page.$('#btn'))!
expect(button).toBeDefined()
test('should have the correct title', async () => {
await page.goto(`http://localhost:${PORT}`)
const button = (await page.$<HTMLButtonElement>('#btn'))!
expect(button).toBeDefined()

let text = await page.evaluate(btn => btn.textContent, button)
expect(text).toBe('Clicked 0 time(s)')
let text = await page.evaluate(btn => btn.textContent, button)
expect(text).toBe('Clicked 0 time(s)')

await button.click()
text = await page.evaluate(btn => btn.textContent, button)
}
catch (e) {
console.error(e)
expect(e).toBeUndefined()
}
await button.click()
text = await page.evaluate(btn => btn.textContent, button)
expect(text).toBe('Clicked 1 time(s)')
}, 60_000)
})

0 comments on commit 2396858

Please sign in to comment.