Skip to content

Commit 8b1aa85

Browse files
piehascorbic
andauthored
test: fix fetch-handler test flakiness (#170)
* test: ensure blobServer and test apiServer are not overlapping * test: allow shards to continue running when one of shards fail --------- Co-authored-by: Matt Kane <[email protected]>
1 parent c8901be commit 8b1aa85

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

.github/workflows/run-tests.yml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ jobs:
5757
test:
5858
runs-on: ubuntu-latest
5959
strategy:
60+
fail-fast: false
6061
matrix:
6162
shard: [1/5, 2/5, 3/5, 4/5, 5/5]
6263
steps:

tests/integration/fetch-handler.test.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import getPort from 'get-port'
33
import { getLogger } from 'lambda-local'
44
import { createServer, type Server } from 'node:http'
55
import { v4 } from 'uuid'
6-
import { afterAll, beforeAll, beforeEach, expect, test, vi } from 'vitest'
6+
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
77
import {
88
createFixture,
99
invokeFunction,
@@ -22,6 +22,10 @@ import {
2222
// Disable the verbose logging of the lambda-local runtime
2323
getLogger().level = 'alert'
2424

25+
let apiBase: string
26+
let testServer: Server
27+
let handlerCalled = 0
28+
2529
beforeEach<FixtureTestContext>(async (ctx) => {
2630
// set for each test a new deployID and siteID
2731
ctx.deployID = generateRandomObjectID()
@@ -32,15 +36,10 @@ beforeEach<FixtureTestContext>(async (ctx) => {
3236
// vi.spyOn(console, 'debug').mockImplementation(() => {})
3337

3438
await startMockBlobStore(ctx)
35-
})
36-
37-
let apiBase: string
38-
let testServer: Server
39-
let handlerCalled = 0
4039

41-
beforeAll(async () => {
4240
// create a fake endpoint to test if it got called
43-
const port = await getPort({ host: '0.0.0.0' })
41+
const port = await getPort({ host: '0.0.0.0', exclude: [ctx.blobStorePort] })
42+
handlerCalled = 0
4443

4544
testServer = createServer((_, res) => {
4645
handlerCalled++
@@ -56,7 +55,7 @@ beforeAll(async () => {
5655
})
5756
})
5857

59-
afterAll(async () => {
58+
afterEach(async () => {
6059
testServer.closeAllConnections()
6160
await new Promise((resolve) => {
6261
testServer.on('close', resolve)

tests/utils/fixture.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface FixtureTestContext extends TestContext {
3030
siteID: string
3131
deployID: string
3232
blobStoreHost: string
33+
blobStorePort: number
3334
blobServer: BlobsServer
3435
blobStore: ReturnType<typeof getStore>
3536
functionDist: string

tests/utils/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const startMockBlobStore = async (ctx: FixtureTestContext) => {
6363
})
6464
await ctx.blobServer.start()
6565
ctx.blobStoreHost = `localhost:${port}`
66+
ctx.blobStorePort = port
6667
vi.stubEnv('NETLIFY_BLOBS_CONTEXT', createBlobContext(ctx))
6768

6869
ctx.blobStore = getDeployStore({

0 commit comments

Comments
 (0)