[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(api): use resolvedUrls from devserver (#5289)

Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>

authored by

Michael サイトー 中村 Bashurov
Hiroshi Ogawa
and committed by
GitHub
(Mar 14, 2024, 1:12 PM +0100) 2fef5a7e bdc371ee

+74 -3
+10
pnpm-lock.yaml
··· 2136 2136 specifier: workspace:* 2137 2137 version: link:../../packages/vitest 2138 2138 2139 + test/ws-api: 2140 + devDependencies: 2141 + '@vitejs/plugin-basic-ssl': 2142 + specifier: ^1.0.2 2143 + version: 1.0.2(vite@5.0.12) 2144 + vitest: 2145 + specifier: workspace:* 2146 + version: link:../../packages/vitest 2147 + 2139 2148 packages: 2140 2149 2141 2150 /@aashutoshrathi/word-wrap@1.2.6: ··· 27570 27579 27571 27580 /workbox-google-analytics@7.0.0: 27572 27581 resolution: {integrity: sha512-MEYM1JTn/qiC3DbpvP2BVhyIH+dV/5BjHk756u9VbwuAhu0QHyKscTnisQuz21lfRpOwiS9z4XdqeVAKol0bzg==} 27582 + 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 27573 27583 dependencies: 27574 27584 workbox-background-sync: 7.0.0 27575 27585 workbox-core: 7.0.0
+12
test/ws-api/package.json
··· 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 + }
+7
test/ws-api/vitest.config.ts
··· 1 + import { defineConfig } from 'vitest/config' 2 + 3 + export default defineConfig({ 4 + test: { 5 + include: ['tests/**/*.test.ts'], 6 + }, 7 + })
+20
test/ws-api/tests/server-url.test.ts
··· 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')
+9 -3
packages/vitest/src/node/logger.ts
··· 171 171 this.log(c.dim(c.green(` ${output} Browser runner started at ${new URL('/', origin)}`))) 172 172 }) 173 173 174 - if (this.ctx.config.ui) 174 + if (this.ctx.config.ui) { 175 175 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 + } 178 184 179 185 if (this.ctx.coverageProvider) 180 186 this.log(c.dim(' Coverage enabled with ') + c.yellow(this.ctx.coverageProvider.name))
+5
test/ws-api/fixtures/server-url/basic.test.ts
··· 1 + import { expect, test } from "vitest"; 2 + 3 + test("basic", () => { 4 + expect(1).toBe(1); 5 + })
+11
test/ws-api/fixtures/server-url/vitest.config.ts
··· 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 + })