Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): use resolvedUrls from devserver #5289

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

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