Skip to content

Commit

Permalink
fix(api): use resolvedUrls from devserver (#5289)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
  • Loading branch information
saitonakamura and hi-ogawa committed Mar 14, 2024
1 parent bdc371e commit 2fef5a7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/vitest/src/node/logger.ts
Expand Up @@ -171,10 +171,16 @@ export class Logger {
this.log(c.dim(c.green(` ${output} Browser runner started at ${new URL('/', origin)}`)))
})

if (this.ctx.config.ui)
if (this.ctx.config.ui) {
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}`)))
else if (this.ctx.config.api?.port)
this.log(c.dim(c.green(` API started at http://${this.ctx.config.api?.host || 'localhost'}:${c.bold(`${this.ctx.config.api.port}`)}`)))
}
else if (this.ctx.config.api?.port) {
const resolvedUrls = this.ctx.server.resolvedUrls
// workaround for https://github.com/vitejs/vite/issues/15438, it was fixed in vite 5.1
const fallbackUrl = `http://${this.ctx.config.api.host || 'localhost'}:${this.ctx.config.api.port}`
const origin = resolvedUrls?.local[0] ?? resolvedUrls?.network[0] ?? fallbackUrl
this.log(c.dim(c.green(` API started at ${new URL('/', origin)}`)))
}

if (this.ctx.coverageProvider)
this.log(c.dim(' Coverage enabled with ') + c.yellow(this.ctx.coverageProvider.name))
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/ws-api/fixtures/server-url/basic.test.ts
@@ -0,0 +1,5 @@
import { expect, test } from "vitest";

test("basic", () => {
expect(1).toBe(1);
})
11 changes: 11 additions & 0 deletions test/ws-api/fixtures/server-url/vitest.config.ts
@@ -0,0 +1,11 @@
import { defineConfig } from 'vitest/config'
import basicSsl from '@vitejs/plugin-basic-ssl'

// test https by
// TEST_HTTPS=1 pnpm test --root fixtures/server-url --api

export default defineConfig({
plugins: [
!!process.env.TEST_HTTPS && basicSsl(),
],
})
12 changes: 12 additions & 0 deletions test/ws-api/package.json
@@ -0,0 +1,12 @@
{
"name": "@vitest/test-ws-api",
"type": "module",
"private": true,
"scripts": {
"test": "vitest"
},
"devDependencies": {
"@vitejs/plugin-basic-ssl": "^1.0.2",
"vitest": "workspace:*"
}
}
20 changes: 20 additions & 0 deletions test/ws-api/tests/server-url.test.ts
@@ -0,0 +1,20 @@
import { join } from 'node:path'
import { expect, it } from 'vitest'

import { runVitestCli } from '../../test-utils'

it('api server-url http', async () => {
delete process.env.TEST_HTTPS
const { stdout } = await runVitestCli('run', '--root', join(process.cwd(), './fixtures/server-url'), '--api')
expect(stdout).toContain('API started at http://localhost:51204/')
expect(stdout).toContain('Test Files 1 passed')
})

it('api server-url https', async () => {
process.env.TEST_HTTPS = '1'
const { stdout } = await runVitestCli('run', '--root', join(process.cwd(), './fixtures/server-url'), '--api')
expect(stdout).toContain('API started at https://localhost:51204/')
expect(stdout).toContain('Test Files 1 passed')
})

it.todo('api server-url fallback if resolvedUrls is null')
7 changes: 7 additions & 0 deletions test/ws-api/vitest.config.ts
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['tests/**/*.test.ts'],
},
})

0 comments on commit 2fef5a7

Please sign in to comment.