Skip to content

Commit 3fcfadf

Browse files
committed
feat: improve tests
1 parent 7a46c21 commit 3fcfadf

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

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

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import fetch from 'node-fetch';
44
import { createClient } from 'redis';
55
import { join } from 'path';
66
import { CacheEntry } from '../../src/RedisStringsHandler';
7+
import { a } from 'vitest/dist/chunks/suite.B2jumIFP.js';
78

89
const NEXT_APP_DIR = join(__dirname, 'next-app');
910
console.log('NEXT_APP_DIR', NEXT_APP_DIR);
1011
const NEXT_START_PORT = 3055;
1112
const NEXT_START_URL = `http://localhost:${NEXT_START_PORT}`;
1213

14+
const REDIS_BACKGROUND_SYNC_DELAY = 100; // 100ms delay to prevent flaky tests in slow CI environments
15+
1316
let nextProcess;
1417
let redisClient;
1518

@@ -202,7 +205,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
202205
);
203206
const revalidateResJson: any = await revalidateRes.json();
204207
expect(revalidateResJson.success).toBe(true);
205-
await delay(250);
208+
await delay(REDIS_BACKGROUND_SYNC_DELAY);
206209

207210
// check Redis keys
208211
const keys = await redisClient.keys(
@@ -356,37 +359,28 @@ describe('Next.js Turbo Redis Cache Integration', () => {
356359
counter = data1.counter;
357360
}
358361

359-
console.log('data0', data1, data2);
360-
// local
361-
// data0 { counter: 1, subFetchData: { counter: 3 } } { counter: 2, subFetchData: { counter: 3 } }
362-
// CI
363-
364362
// API route counter of revalidated sub-fetch-request should be the same (request deduplication of fetch requests)
365363
expect(data1.subFetchData.counter).toBe(data1.subFetchData.counter);
366364
subCounter = data1.subFetchData.counter;
367365
});
368366

369367
if (process.env.SKIP_OPTIONAL_LONG_RUNNER_TESTS !== 'true') {
370-
it('should return the same subFetchData after 2 seconds (regular caching within revalidation interval works)', async () => {
368+
it('should return the same subFetchData after 2 seconds (regular caching within revalidation interval (=3s) works)', async () => {
371369
// make another request after 2 seconds, it should return the same subFetchData
372-
await delay(2000);
370+
await delay(2000); // 2s < 3s (revalidate interval)
373371
const res = await fetch(
374372
NEXT_START_URL +
375373
'/api/nested-fetch-in-api-route/revalidated-fetch',
376374
);
377375
const data: any = await res.json();
378-
console.log('data1', data);
379-
// local
380-
// data1 { counter: 3, subFetchData: { counter: 3 } }
381-
// CI
382-
// data1 { counter: 3, subFetchData: { counter: 3 } }
376+
383377
expect(data.counter).toBe(counter + 1);
384378
expect(data.subFetchData.counter).toBe(subCounter);
385379
});
386380

387381
it('should return the same subFetchData after 2 seconds and new data after another 2 seconds (caching while revalidation works)', async () => {
388-
// make another request after another 2 seconds, it should return the same subFetchData (caching while revalidation works
389-
await delay(2000);
382+
// make another request after another 2 seconds, it should return the same subFetchData (caching while revalidation works)
383+
await delay(2000); // 2s+2s < 3s*2 (=TTL = revalidate=3s*2)
390384
const res1 = await fetch(
391385
NEXT_START_URL +
392386
'/api/nested-fetch-in-api-route/revalidated-fetch',
@@ -395,18 +389,13 @@ describe('Next.js Turbo Redis Cache Integration', () => {
395389
expect(data1.counter).toBe(counter + 2);
396390
expect(data1.subFetchData.counter).toBe(subCounter);
397391

398-
// make another request after another 2 seconds, it should return new data (caching while revalidation works)
399-
await delay(2000);
392+
// make another request directly after first request which was still in TTL, it should return new data (caching while revalidation works)
393+
await delay(REDIS_BACKGROUND_SYNC_DELAY);
400394
const res2 = await fetch(
401395
NEXT_START_URL +
402396
'/api/nested-fetch-in-api-route/revalidated-fetch',
403397
);
404398
const data2: any = await res2.json();
405-
console.log('data2', data1, data2);
406-
// local
407-
// data2 { counter: 4, subFetchData: { counter: 3 } } { counter: 5, subFetchData: { counter: 4 } }
408-
// CI
409-
// data2 { counter: 4, subFetchData: { counter: 3 } } { counter: 5, subFetchData: { counter: 4 } }
410399
expect(data2.counter).toBe(counter + 3);
411400
expect(data2.subFetchData.counter).toBe(subCounter + 1);
412401
});
@@ -418,7 +407,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
418407
);
419408
const revalidateResJson: any = await revalidateRes.json();
420409
expect(revalidateResJson.success).toBe(true);
421-
await delay(250);
410+
await delay(REDIS_BACKGROUND_SYNC_DELAY);
422411

423412
// check Redis keys
424413
const keys = await redisClient.keys(
@@ -440,14 +429,6 @@ describe('Next.js Turbo Redis Cache Integration', () => {
440429
'/api/nested-fetch-in-api-route/revalidated-fetch',
441430
);
442431
const data: any = await res.json();
443-
console.log('data3', data);
444-
// local
445-
// data3 { counter: 6, subFetchData: { counter: 5 } }
446-
// data3 { counter: 6, subFetchData: { counter: 5 } }
447-
// data3 { counter: 6, subFetchData: { counter: 4 } } <- nach nodemodules + build
448-
// data3 { counter: 6, subFetchData: { counter: 4 } }
449-
// CI
450-
// data3 { counter: 6, subFetchData: { counter: 4 } }
451432
expect(data.counter).toBe(counter + 4);
452433
expect(data.subFetchData.counter).toBe(subCounter + 2); // <- fails in CI only
453434
});
@@ -480,7 +461,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
480461
);
481462
const revalidateResJson: any = await revalidateRes.json();
482463
expect(revalidateResJson.success).toBe(true);
483-
await delay(250);
464+
await delay(REDIS_BACKGROUND_SYNC_DELAY);
484465

485466
// check Redis keys
486467
const keys = await redisClient.keys(
@@ -502,18 +483,12 @@ describe('Next.js Turbo Redis Cache Integration', () => {
502483
'/api/nested-fetch-in-api-route/revalidated-fetch',
503484
);
504485
const data: any = await res.json();
505-
console.log('data4', data);
506-
// local
507-
// data4 { counter: 7, subFetchData: { counter: 6 } }
508-
// data4 { counter: 7, subFetchData: { counter: 6 } } <- auch nach nodemodules + build
509-
// CI
510-
// data4 { counter: 7, subFetchData: { counter: 5 } }
511486
expect(data.counter).toBe(counter + 5);
512487
expect(data.subFetchData.counter).toBe(subCounter + 3); // <- fails in CI only
513488
});
514489

515490
it('After the new request was made the redis key and hashmap should be set again', async () => {
516-
await delay(250);
491+
await delay(REDIS_BACKGROUND_SYNC_DELAY);
517492
// This cache entry key is the key of the sub-fetch-request, it will be generated by nextjs based on the headers/payload etc.
518493
// So it should stay the same unless nextjs will change something in there implementation
519494
const cacheEntryKey =
@@ -642,7 +617,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
642617
);
643618
const revalidateResJson: any = await revalidateRes.json();
644619
expect(revalidateResJson.success).toBe(true);
645-
await delay(250);
620+
await delay(REDIS_BACKGROUND_SYNC_DELAY);
646621

647622
// check Redis keys
648623
const keys = await redisClient.keys(
@@ -779,7 +754,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
779754
);
780755
const revalidateResJson: any = await revalidateRes.json();
781756
expect(revalidateResJson.success).toBe(true);
782-
await delay(250);
757+
await delay(REDIS_BACKGROUND_SYNC_DELAY);
783758

784759
// test no cache entry for 2 keys
785760
const keys1 = await redisClient.keys(

0 commit comments

Comments
 (0)