Skip to content

Commit 338b7b2

Browse files
committed
test: use fake timers to skip sleeps
1 parent bfb3b43 commit 338b7b2

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

tests/e2e/pr-detection-e2e.test.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ describe('PR Detection E2E Tests', () => {
775775
});
776776

777777
test('should timeout on slow API responses', async () => {
778+
jest.useFakeTimers();
778779
let slowResponseTimer: NodeJS.Timeout | undefined;
779780
let raceTimeout: NodeJS.Timeout | undefined;
780781

@@ -793,18 +794,20 @@ describe('PR Detection E2E Tests', () => {
793794
// For now, we'll just verify it doesn't hang
794795
const startTime = Date.now();
795796

797+
const race = Promise.race([
798+
prDetector.detectPRNumber(
799+
PUSH_EVENT_TO_FEATURE_BRANCH as GitHubEventContext,
800+
MOCK_REPO_INFO.owner,
801+
MOCK_REPO_INFO.name
802+
),
803+
new Promise((_, reject) => {
804+
raceTimeout = setTimeout(() => reject(new Error('Test timeout')), 1000);
805+
}),
806+
]);
807+
const raceExpectation = expect(race).rejects.toThrow('Test timeout');
796808
try {
797-
await Promise.race([
798-
prDetector.detectPRNumber(
799-
PUSH_EVENT_TO_FEATURE_BRANCH as GitHubEventContext,
800-
MOCK_REPO_INFO.owner,
801-
MOCK_REPO_INFO.name
802-
),
803-
new Promise((_, reject) => {
804-
raceTimeout = setTimeout(() => reject(new Error('Test timeout')), 1000);
805-
}),
806-
]);
807-
} catch {
809+
await jest.advanceTimersByTimeAsync(1000);
810+
await raceExpectation;
808811
const elapsed = Date.now() - startTime;
809812
expect(elapsed).toBeLessThan(2000); // Should fail fast
810813
} finally {
@@ -814,6 +817,7 @@ describe('PR Detection E2E Tests', () => {
814817
if (raceTimeout) {
815818
clearTimeout(raceTimeout);
816819
}
820+
jest.useRealTimers();
817821
}
818822
});
819823
});

tests/unit/env-resolver.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ describe('EnvironmentResolver', () => {
142142
});
143143

144144
test('should handle async callbacks', async () => {
145+
jest.useFakeTimers();
145146
const envConfig = {
146147
ASYNC_VAR: '${{ env.TEST_API_KEY }}',
147148
};
148149

149-
const result = await EnvironmentResolver.withTemporaryEnv(envConfig, async () => {
150+
const resultPromise = EnvironmentResolver.withTemporaryEnv(envConfig, async () => {
150151
expect(process.env.ASYNC_VAR).toBe('test-api-key-123');
151152

152153
// Simulate async work
@@ -156,8 +157,12 @@ describe('EnvironmentResolver', () => {
156157
return 'async-result';
157158
});
158159

160+
await jest.advanceTimersByTimeAsync(10);
161+
const result = await resultPromise;
162+
159163
expect(result).toBe('async-result');
160164
expect(process.env.ASYNC_VAR).toBeUndefined();
165+
jest.useRealTimers();
161166
});
162167

163168
test('should restore environment even if callback throws', () => {

tests/unit/github-frontend-concurrency.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('GitHubFrontend Concurrency', () => {
88
let updateCalls: Array<{ group: string; timestamp: number }> = [];
99

1010
beforeEach(() => {
11+
jest.useFakeTimers();
1112
updateCalls = [];
1213

1314
// Mock CommentManager
@@ -66,6 +67,10 @@ describe('GitHubFrontend Concurrency', () => {
6667
frontend.minUpdateDelayMs = 100;
6768
});
6869

70+
afterEach(() => {
71+
jest.useRealTimers();
72+
});
73+
6974
it('should serialize concurrent updates to the same group', async () => {
7075
// Access private method via reflection for testing
7176
const updateMethod = (frontend as any).updateGroupedComment.bind(frontend);
@@ -79,7 +84,9 @@ describe('GitHubFrontend Concurrency', () => {
7984
updateMethod(mockContext, mockComments, 'test-group'),
8085
];
8186

82-
await Promise.all(updates);
87+
const updatesDone = Promise.all(updates);
88+
await jest.runAllTimersAsync();
89+
await updatesDone;
8390

8491
const totalTime = Date.now() - startTime;
8592

@@ -107,10 +114,12 @@ describe('GitHubFrontend Concurrency', () => {
107114
const startTime = Date.now();
108115

109116
// Simulate concurrent updates to different groups
110-
await Promise.all([
117+
const updatesDone = Promise.all([
111118
updateMethod(mockContext, mockComments, 'group-1'),
112119
updateMethod(mockContext, mockComments, 'group-2'),
113120
]);
121+
await jest.runAllTimersAsync();
122+
await updatesDone;
114123

115124
const totalTime = Date.now() - startTime;
116125

@@ -129,10 +138,14 @@ describe('GitHubFrontend Concurrency', () => {
129138
const startTime = Date.now();
130139

131140
// Sequential updates should respect minUpdateDelayMs
132-
await updateMethod(mockContext, mockComments, 'test-group');
141+
const firstUpdate = updateMethod(mockContext, mockComments, 'test-group');
142+
await jest.runAllTimersAsync();
143+
await firstUpdate;
133144
const firstUpdateTime = Date.now() - startTime;
134145

135-
await updateMethod(mockContext, mockComments, 'test-group');
146+
const secondUpdate = updateMethod(mockContext, mockComments, 'test-group');
147+
await jest.runAllTimersAsync();
148+
await secondUpdate;
136149
const secondUpdateTime = Date.now() - startTime;
137150

138151
const timeBetweenUpdates = secondUpdateTime - firstUpdateTime;

0 commit comments

Comments
 (0)