Skip to content

Commit 2fef5a7

Browse files
saitonakamurahi-ogawa
andauthoredMar 14, 2024··
fix(api): use resolvedUrls from devserver (#5289)
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
1 parent bdc371e commit 2fef5a7

File tree

7 files changed

+74
-3
lines changed

7 files changed

+74
-3
lines changed
 

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,16 @@ export class Logger {
171171
this.log(c.dim(c.green(` ${output} Browser runner started at ${new URL('/', origin)}`)))
172172
})
173173

174-
if (this.ctx.config.ui)
174+
if (this.ctx.config.ui) {
175175
this.log(c.dim(c.green(` UI started at http://${this.ctx.config.api?.host || 'localhost'}:${c.bold(`${this.ctx.server.config.server.port}`)}${this.ctx.config.uiBase}`)))
176-
else if (this.ctx.config.api?.port)
177-
this.log(c.dim(c.green(` API started at http://${this.ctx.config.api?.host || 'localhost'}:${c.bold(`${this.ctx.config.api.port}`)}`)))
176+
}
177+
else if (this.ctx.config.api?.port) {
178+
const resolvedUrls = this.ctx.server.resolvedUrls
179+
// workaround for https://github.com/vitejs/vite/issues/15438, it was fixed in vite 5.1
180+
const fallbackUrl = `http://${this.ctx.config.api.host || 'localhost'}:${this.ctx.config.api.port}`
181+
const origin = resolvedUrls?.local[0] ?? resolvedUrls?.network[0] ?? fallbackUrl
182+
this.log(c.dim(c.green(` API started at ${new URL('/', origin)}`)))
183+
}
178184

179185
if (this.ctx.coverageProvider)
180186
this.log(c.dim(' Coverage enabled with ') + c.yellow(this.ctx.coverageProvider.name))

‎pnpm-lock.yaml

+10
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,11 @@
1+
import { defineConfig } from 'vitest/config'
2+
import basicSsl from '@vitejs/plugin-basic-ssl'
3+
4+
// test https by
5+
// TEST_HTTPS=1 pnpm test --root fixtures/server-url --api
6+
7+
export default defineConfig({
8+
plugins: [
9+
!!process.env.TEST_HTTPS && basicSsl(),
10+
],
11+
})

‎test/ws-api/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@vitest/test-ws-api",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"test": "vitest"
7+
},
8+
"devDependencies": {
9+
"@vitejs/plugin-basic-ssl": "^1.0.2",
10+
"vitest": "workspace:*"
11+
}
12+
}

‎test/ws-api/tests/server-url.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { join } from 'node:path'
2+
import { expect, it } from 'vitest'
3+
4+
import { runVitestCli } from '../../test-utils'
5+
6+
it('api server-url http', async () => {
7+
delete process.env.TEST_HTTPS
8+
const { stdout } = await runVitestCli('run', '--root', join(process.cwd(), './fixtures/server-url'), '--api')
9+
expect(stdout).toContain('API started at http://localhost:51204/')
10+
expect(stdout).toContain('Test Files 1 passed')
11+
})
12+
13+
it('api server-url https', async () => {
14+
process.env.TEST_HTTPS = '1'
15+
const { stdout } = await runVitestCli('run', '--root', join(process.cwd(), './fixtures/server-url'), '--api')
16+
expect(stdout).toContain('API started at https://localhost:51204/')
17+
expect(stdout).toContain('Test Files 1 passed')
18+
})
19+
20+
it.todo('api server-url fallback if resolvedUrls is null')

‎test/ws-api/vitest.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['tests/**/*.test.ts'],
6+
},
7+
})

0 commit comments

Comments
 (0)
Please sign in to comment.