diff --git a/packages/vitest/src/node/logger.ts b/packages/vitest/src/node/logger.ts index ce288f5f3867..fa17be5ebad0 100644 --- a/packages/vitest/src/node/logger.ts +++ b/packages/vitest/src/node/logger.ts @@ -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)) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 62d5671c7555..07d7ad942c1e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2136,6 +2136,15 @@ importers: specifier: workspace:* version: link:../../packages/vitest + test/ws-api: + devDependencies: + '@vitejs/plugin-basic-ssl': + specifier: ^1.0.2 + version: 1.0.2(vite@5.0.12) + vitest: + specifier: workspace:* + version: link:../../packages/vitest + packages: /@aashutoshrathi/word-wrap@1.2.6: @@ -27570,6 +27579,7 @@ packages: /workbox-google-analytics@7.0.0: resolution: {integrity: sha512-MEYM1JTn/qiC3DbpvP2BVhyIH+dV/5BjHk756u9VbwuAhu0QHyKscTnisQuz21lfRpOwiS9z4XdqeVAKol0bzg==} + deprecated: It is not compatible with newer versions of GA starting with v4, as long as you are using GAv3 it should be ok, but the package is not longer being maintained dependencies: workbox-background-sync: 7.0.0 workbox-core: 7.0.0 diff --git a/test/ws-api/fixtures/server-url/basic.test.ts b/test/ws-api/fixtures/server-url/basic.test.ts new file mode 100644 index 000000000000..9bb0283e10c6 --- /dev/null +++ b/test/ws-api/fixtures/server-url/basic.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from "vitest"; + +test("basic", () => { + expect(1).toBe(1); +}) diff --git a/test/ws-api/fixtures/server-url/vitest.config.ts b/test/ws-api/fixtures/server-url/vitest.config.ts new file mode 100644 index 000000000000..1855b750849e --- /dev/null +++ b/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(), + ], +}) diff --git a/test/ws-api/package.json b/test/ws-api/package.json new file mode 100644 index 000000000000..83754d0a26e8 --- /dev/null +++ b/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:*" + } +} diff --git a/test/ws-api/tests/server-url.test.ts b/test/ws-api/tests/server-url.test.ts new file mode 100644 index 000000000000..68ecdaf11956 --- /dev/null +++ b/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') diff --git a/test/ws-api/vitest.config.ts b/test/ws-api/vitest.config.ts new file mode 100644 index 000000000000..f964be2a40cc --- /dev/null +++ b/test/ws-api/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + include: ['tests/**/*.test.ts'], + }, +})