Visitar URL original
Honor START_VITEST_SERVER=false in vitest global setup (#63747) · github/docs@df701b8 · GitHub
Skip to content

Commit df701b8

Browse files
heiskrCopilot
andauthored
Honor START_VITEST_SERVER=false in vitest global setup (#63747)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 796e2742-a386-4cab-8cc3-be8fe189e974
1 parent 18482ab commit df701b8

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ jobs:
8282
- name: languages
8383
isPrivateRepo: false
8484

85+
env:
86+
# These suites make no HTTP requests, so they skip the Next.js build and test server.
87+
# A suite that adds an HTTP test fails with a connection error until it's removed from this list.
88+
START_VITEST_SERVER: ${{ contains(fromJSON('["color-schemes", "content-linter", "data-directory", "ghes-releases", "journeys", "links", "observability", "products", "workflows"]'), matrix.name) && 'false' || 'true' }}
89+
8590
steps:
8691
# Repeat each if on its step so skipped work still leaves the required check present.
8792
- name: Check out repo
@@ -133,8 +138,10 @@ jobs:
133138
run: echo "${{ steps.changed_files.outputs.all_modified_files }}" > get_diff_files.txt
134139

135140
- uses: ./.github/actions/cache-nextjs
141+
if: ${{ env.START_VITEST_SERVER != 'false' }}
136142

137143
- name: Run build script
144+
if: ${{ env.START_VITEST_SERVER != 'false' }}
138145
run: npm run build
139146

140147
- uses: ./.github/actions/warmup-remotejson-cache

‎src/tests/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ Tests that make HTTP requests to `localhost:4000`:
142142
- Vitest automatically starts/stops server via hooks
143143
- Disable with `START_VITEST_SERVER=false` for manual server control
144144

145+
Unit tests that don't make HTTP requests can skip the server. This avoids the build, Elasticsearch, and translations:
146+
```bash
147+
START_VITEST_SERVER=false npm test -- src/languages/tests/correct-translation-content.ts
148+
```
149+
150+
In CI, `.github/workflows/test.yml` sets `START_VITEST_SERVER=false` and skips the build for suites with no HTTP tests. If you add an HTTP test to one of those suites, remove the suite from that list.
151+
145152
Manual server for debugging:
146153
```bash
147154
# Terminal 1

‎src/tests/vitest.setup.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { main } from '@/frame/start-server'
21
import { overrideEnvForTesting } from './mocks/start-mock-server'
32

43
let teardownHappened = false
5-
type PromiseType<T extends Promise<unknown>> = T extends Promise<infer U> ? U : never
6-
type Server = PromiseType<ReturnType<typeof main>>
4+
type Server = Awaited<ReturnType<typeof import('@/frame/start-server').main>>
75

86
let server: Server | undefined
97

108
export async function setup() {
119
overrideEnvForTesting()
10+
// Unit tests and manually started servers don't need the Vitest-managed server.
11+
if (process.env.START_VITEST_SERVER === 'false') return
12+
// Import lazily so skipping the server also skips loading the app.
13+
const { main } = await import('@/frame/start-server')
1214
server = await main()
1315
}
1416

0 commit comments

Comments
 (0)