Skip to content

Commit 6795e4f

Browse files
committed
test: cleanup after use-cache tests
1 parent 29282a0 commit 6795e4f

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

tests/integration/use-cache.test.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { CheerioAPI, load } from 'cheerio'
22
import { getLogger } from 'lambda-local'
33
import { v4 } from 'uuid'
4-
import { beforeAll, describe, expect, test, vi } from 'vitest'
4+
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
55
import { type FixtureTestContext } from '../utils/contexts.js'
66
import { createFixture, loadSandboxedFunction, runPlugin } from '../utils/fixture.js'
77
import { generateRandomObjectID, startMockBlobStore } from '../utils/helpers.js'
88
import { InvokeFunctionResult } from '../utils/lambda-helpers.mjs'
99
import { nextVersionSatisfies } from '../utils/next-version-helpers.mjs'
10+
import { afterTestCleanup } from '../test-setup.js'
1011

1112
function compareDates(
1213
$response1: CheerioAPI,
@@ -145,25 +146,33 @@ declare module 'vitest' {
145146
// Disable the verbose logging of the lambda-local runtime
146147
getLogger().level = 'alert'
147148

148-
let ctx: FixtureTestContext
149-
beforeAll(async () => {
150-
ctx = {
151-
deployID: generateRandomObjectID(),
152-
siteID: v4(),
153-
} as FixtureTestContext
154-
155-
vi.stubEnv('SITE_ID', ctx.siteID)
156-
vi.stubEnv('DEPLOY_ID', ctx.deployID)
157-
vi.stubEnv('NETLIFY_PURGE_API_TOKEN', 'fake-token')
158-
await startMockBlobStore(ctx as FixtureTestContext)
159-
160-
await createFixture('use-cache', ctx)
161-
await runPlugin(ctx)
162-
})
163-
164149
// only supporting latest variant (https://github.com/vercel/next.js/pull/76687)
165150
// first released in v15.3.0-canary.13 so we should not run tests on older next versions
166151
describe.skipIf(!nextVersionSatisfies('>=15.3.0-canary.13'))('use cache', () => {
152+
// note that in this test suite we are setting up test fixture once
153+
// because every test is using different path and also using sandboxed functions
154+
// so tests are not sharing context between them and this make test running
155+
// much more performant
156+
let ctx: FixtureTestContext
157+
beforeAll(async () => {
158+
ctx = {
159+
deployID: generateRandomObjectID(),
160+
siteID: v4(),
161+
} as FixtureTestContext
162+
163+
vi.stubEnv('SITE_ID', ctx.siteID)
164+
vi.stubEnv('DEPLOY_ID', ctx.deployID)
165+
vi.stubEnv('NETLIFY_PURGE_API_TOKEN', 'fake-token')
166+
await startMockBlobStore(ctx as FixtureTestContext)
167+
168+
await createFixture('use-cache', ctx)
169+
await runPlugin(ctx)
170+
})
171+
172+
afterAll(async () => {
173+
await afterTestCleanup(ctx)
174+
})
175+
167176
describe('default (in-memory cache entries, shared tag manifests)', () => {
168177
for (const {
169178
expectedCachingBehaviorWhenUseCacheRegenerates,

tests/test-setup.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import fs from 'node:fs'
22
import { afterEach } from 'vitest'
33
import { type FixtureTestContext } from './utils/contexts'
44

5-
// cleanup after each test as a fallback if someone forgot to call it
6-
afterEach<FixtureTestContext>(async ({ cleanup }) => {
5+
export async function afterTestCleanup({ cleanup }: FixtureTestContext) {
76
if ('reset' in fs) {
87
;(fs as any).reset()
98
}
109

1110
const jobs = (cleanup ?? []).map((job) => job())
1211

1312
await Promise.all(jobs)
13+
}
14+
15+
// cleanup after each test as a fallback if someone forgot to call it
16+
afterEach<FixtureTestContext>(async (ctx) => {
17+
await afterTestCleanup(ctx)
1418
})

0 commit comments

Comments
 (0)