Skip to content

Commit 6c1cc78

Browse files
authoredJan 16, 2024
fix(browser): fix browser testing url for https (#4855)
1 parent 952c31d commit 6c1cc78

File tree

9 files changed

+80
-3
lines changed

9 files changed

+80
-3
lines changed
 

‎.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ jobs:
143143
strategy:
144144
matrix:
145145
browser: [[chrome, chromium], [firefox, firefox], [edge, webkit]]
146+
fail-fast: false
146147

147148
timeout-minutes: 30
148149

@@ -186,6 +187,7 @@ jobs:
186187
strategy:
187188
matrix:
188189
browser: [[chrome, chromium], [edge, webkit]]
190+
fail-fast: false
189191

190192
timeout-minutes: 30
191193

‎packages/vitest/src/node/logger.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ export class Logger {
166166
const name = project.getName()
167167
const output = project.isCore() ? '' : ` [${name}]`
168168

169-
this.log(c.dim(c.green(` ${output} Browser runner started at http://${project.config.browser.api?.host || 'localhost'}:${c.bold(`${project.browser.config.server.port}`)}`)))
169+
const resolvedUrls = project.browser.resolvedUrls
170+
const origin = resolvedUrls?.local[0] ?? resolvedUrls?.network[0]
171+
this.log(c.dim(c.green(` ${output} Browser runner started at ${new URL('/', origin)}`)))
170172
})
171173

172174
if (this.ctx.config.ui)

‎packages/vitest/src/node/pools/browser.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function createBrowserPool(ctx: Vitest): ProcessPool {
3838
const provider = project.browserProvider!
3939
providers.add(provider)
4040

41-
const origin = `http://${ctx.config.browser.api?.host || 'localhost'}:${project.browser!.config.server.port}`
41+
const resolvedUrls = project.browser?.resolvedUrls
42+
const origin = resolvedUrls?.local[0] ?? resolvedUrls?.network[0]
4243
const paths = files.map(file => relative(project.config.root, file))
4344

4445
if (project.config.browser.isolate) {

‎pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from "vitest";
2+
3+
test("basic", () => {
4+
expect(1).toBe(1);
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import path from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
import { defineConfig } from 'vitest/config'
4+
import basicSsl from '@vitejs/plugin-basic-ssl'
5+
6+
// test https by
7+
// TEST_HTTPS=1 pnpm test-fixtures --root fixtures/server-url
8+
9+
const provider = process.env.PROVIDER || 'webdriverio';
10+
const browser = process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome');
11+
12+
export default defineConfig({
13+
plugins: [
14+
!!process.env.TEST_HTTPS && basicSsl(),
15+
],
16+
test: {
17+
browser: {
18+
enabled: true,
19+
provider,
20+
name: browser,
21+
},
22+
},
23+
// separate cacheDir from test/browser/vite.config.ts
24+
// to prevent pre-bundling related flakiness on Webkit
25+
cacheDir: path.join(path.dirname(fileURLToPath(import.meta.url)), "node_modules/.vite")
26+
})

‎test/browser/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"coverage": "vitest --coverage.enabled --coverage.provider=istanbul --browser.headless=yes"
1212
},
1313
"devDependencies": {
14+
"@vitejs/plugin-basic-ssl": "^1.0.2",
1415
"@vitest/browser": "workspace:*",
1516
"@vitest/cjs-lib": "link:./cjs-lib",
1617
"execa": "^7.1.1",
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import assert from 'node:assert'
2+
import test from 'node:test'
3+
import { execa } from 'execa'
4+
5+
test('server-url http', async () => {
6+
const result = await execa('npx', ['vitest', 'run', '--root=./fixtures/server-url', '--browser.headless'], {
7+
env: {
8+
CI: '1',
9+
NO_COLOR: '1',
10+
},
11+
})
12+
assert.match(result.stdout, /Browser runner started at http:\/\/localhost:5173\//)
13+
assert.match(result.stdout, /Test Files {2}1 passed/)
14+
})
15+
16+
// this test is skipped since browser warns self-signed https and it requires manual interaction.
17+
// you can toggle "skip" to verify it locally.
18+
test('server-url https', { skip: true }, async () => {
19+
const result = await execa('npx', ['vitest', 'run', '--root=./fixtures/server-url'], {
20+
env: {
21+
NO_COLOR: '1',
22+
TEST_HTTPS: '1',
23+
},
24+
})
25+
assert.match(result.stdout, /Browser runner started at https:\/\/localhost:5173\//)
26+
assert.match(result.stdout, /Test Files {2}1 passed/)
27+
})

‎test/browser/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"compilerOptions": {
33
"rootDir": ".",
4-
"module": "node16",
4+
"module": "ESNext",
5+
"moduleResolution": "Bundler",
56
"paths": {
67
"#src/*": ["./src/*"]
78
},

0 commit comments

Comments
 (0)
Please sign in to comment.