Skip to content

Commit 35885e3

Browse files
committed
fix: tests, make them independent from each other
1 parent 8501a4e commit 35885e3

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

test/integration/nextjs-cache-handler.integration.test.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,11 @@ describe('Next.js Turbo Redis Cache Integration', () => {
324324
});
325325
});
326326

327-
describe('DEVELOPMENT should cache a nested fetch request inside a uncached API route', () => {
327+
describe('should cache a nested fetch request inside a uncached API route', () => {
328328
describe('should cache the nested fetch request (but not the API route itself)', () => {
329+
let counter: number;
330+
let subCounter: number;
331+
329332
it('should deduplicate requests to the sub-fetch-request, but not to the API route itself', async () => {
330333
// make two requests, both should return the same subFetchData but different counter
331334
const res1 = await fetch(
@@ -342,16 +345,16 @@ describe('Next.js Turbo Redis Cache Integration', () => {
342345
// API route counter itself increments for each request
343346
// But we do not know which request is first and which is second
344347
if (data1.counter < data2.counter) {
345-
expect(data1.counter).toBe(1);
346-
expect(data2.counter).toBe(2);
348+
expect(data2.counter).toBeGreaterThan(data1.counter);
349+
counter = data2.counter;
347350
} else {
348-
expect(data1.counter).toBe(2);
349-
expect(data2.counter).toBe(1);
351+
expect(data1.counter).toBeGreaterThan(data2.counter);
352+
counter = data1.counter;
350353
}
351354

352355
// API route counter of revalidated sub-fetch-request should be the same (request deduplication)
353-
expect(data1.subFetchData.counter).toBe(1);
354-
expect(data2.subFetchData.counter).toBe(1);
356+
expect(data1.subFetchData.counter).toBe(data1.subFetchData.counter);
357+
subCounter = data1.subFetchData.counter;
355358
});
356359

357360
if (process.env.SKIP_OPTIONAL_LONG_RUNNER_TESTS !== 'true') {
@@ -363,8 +366,8 @@ describe('Next.js Turbo Redis Cache Integration', () => {
363366
'/api/nested-fetch-in-api-route/revalidated-fetch',
364367
);
365368
const data: any = await res.json();
366-
expect(data.counter).toBe(3);
367-
expect(data.subFetchData.counter).toBe(1);
369+
expect(data.counter).toBe(counter + 1);
370+
expect(data.subFetchData.counter).toBe(subCounter);
368371
});
369372

370373
it('should return the same subFetchData after 2 seconds and new data after another 2 seconds (caching while revalidation works)', async () => {
@@ -375,8 +378,8 @@ describe('Next.js Turbo Redis Cache Integration', () => {
375378
'/api/nested-fetch-in-api-route/revalidated-fetch',
376379
);
377380
const data1: any = await res1.json();
378-
expect(data1.counter).toBe(4);
379-
expect(data1.subFetchData.counter).toBe(1);
381+
expect(data1.counter).toBe(counter + 2);
382+
expect(data1.subFetchData.counter).toBe(subCounter);
380383

381384
// make another request after another 2 seconds, it should return new data (caching while revalidation works)
382385
await delay(2000);
@@ -385,8 +388,8 @@ describe('Next.js Turbo Redis Cache Integration', () => {
385388
'/api/nested-fetch-in-api-route/revalidated-fetch',
386389
);
387390
const data2: any = await res2.json();
388-
expect(data2.counter).toBe(5);
389-
expect(data2.subFetchData.counter).toBe(2);
391+
expect(data2.counter).toBe(counter + 3);
392+
expect(data2.subFetchData.counter).toBe(subCounter + 1);
390393
});
391394

392395
it('A request to revalidatePage API should remove the route from redis (string and hashmap)', async () => {
@@ -417,9 +420,9 @@ describe('Next.js Turbo Redis Cache Integration', () => {
417420
'/api/nested-fetch-in-api-route/revalidated-fetch',
418421
);
419422
const data: any = await res.json();
420-
expect(data.counter).toBe(6);
423+
expect(data.counter).toBe(counter + 4);
421424
// subFetchData counter should be 3 because the sub-fetch-request was re-evaluated. If revalidate would not work, it would be 2.
422-
expect(data.subFetchData.counter).toBe(3);
425+
expect(data.subFetchData.counter).toBe(subCounter + 2);
423426
});
424427

425428
it('After the new request was made the redis key and hashmap should be set again', async () => {
@@ -472,9 +475,9 @@ describe('Next.js Turbo Redis Cache Integration', () => {
472475
'/api/nested-fetch-in-api-route/revalidated-fetch',
473476
);
474477
const data: any = await res.json();
475-
expect(data.counter).toBe(7);
478+
expect(data.counter).toBe(counter + 5);
476479
// subFetchData counter should be 4 because the sub-fetch-request was re-evaluated. If revalidate would not work, it would be 3.
477-
expect(data.subFetchData.counter).toBe(4);
480+
expect(data.subFetchData.counter).toBe(subCounter + 3);
478481
});
479482

480483
it('After the new request was made the redis key and hashmap should be set again', async () => {

0 commit comments

Comments
 (0)