Skip to content

Commit b5b5a2a

Browse files
test: Increase time related check time
This helps to avoid "random" test failures due to the time gap between runs.
1 parent 2ec0158 commit b5b5a2a

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/Utils.test.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { cacheEnabled, fetchLite, lazyCallback, promiseLimit } from "./Utils"
2+
3+
const TIMER_MULTIPLER = 3
4+
25
describe("utils", () => {
36
it("lazy callback: immediate call", async () => {
47
expect.assertions(1)
@@ -11,18 +14,18 @@ describe("utils", () => {
1114

1215
// Must run immediately:
1316
lazy(() => {
14-
expect(Date.now() - now).toBeLessThan(25)
17+
expect(Date.now() - now).toBeLessThan(25 * TIMER_MULTIPLER)
1518
})
1619

17-
await new Promise((resolve) => setTimeout(resolve, 50))
20+
await new Promise((resolve) => setTimeout(resolve, 50 * TIMER_MULTIPLER))
1821
})
1922

2023
it("lazy callback: avoid first call", async () => {
2124
expect.assertions(1)
2225

2326
const lazy = lazyCallback((callNumber: () => void) => {
2427
callNumber()
25-
}, 25)
28+
}, 25 * TIMER_MULTIPLER)
2629

2730
const now = Date.now()
2831

@@ -31,27 +34,27 @@ describe("utils", () => {
3134

3235
// Must run after 25ms:
3336
lazy(() => {
34-
expect(Date.now() - now).toBeGreaterThanOrEqual(25)
37+
expect(Date.now() - now).toBeGreaterThanOrEqual(25 * TIMER_MULTIPLER)
3538
})
3639

37-
await new Promise((resolve) => setTimeout(resolve, 50))
40+
await new Promise((resolve) => setTimeout(resolve, 50 * TIMER_MULTIPLER))
3841
})
3942

4043
it("lazy callback: wait first call", async () => {
4144
expect.assertions(1)
4245

4346
const lazy = lazyCallback((callNumber: () => void) => {
4447
callNumber()
45-
}, 25)
48+
}, 25 * TIMER_MULTIPLER)
4649

4750
const now = Date.now()
4851

4952
// Must run after 25ms:
5053
lazy(() => {
51-
expect(Date.now() - now).toBeGreaterThanOrEqual(25)
54+
expect(Date.now() - now).toBeGreaterThanOrEqual(25 * TIMER_MULTIPLER)
5255
})
5356

54-
await new Promise((resolve) => setTimeout(resolve, 50))
57+
await new Promise((resolve) => setTimeout(resolve, 50 * TIMER_MULTIPLER))
5558
})
5659

5760
it("lazy callback: avoid second call", async () => {
@@ -62,13 +65,13 @@ describe("utils", () => {
6265
callNumber()
6366
},
6467
0,
65-
25
68+
25 * TIMER_MULTIPLER
6669
)
6770

6871
const now = Date.now()
6972

7073
// Must run immediately:
71-
lazy(() => expect(Date.now() - now).toBeLessThan(25))
74+
lazy(() => expect(Date.now() - now).toBeLessThan(25 * TIMER_MULTIPLER))
7275

7376
// Must be skipped: too fast call.
7477
lazy(() => {
@@ -79,11 +82,11 @@ describe("utils", () => {
7982
lazy(() => {
8083
const nowDiff = Date.now() - now
8184

82-
expect(nowDiff).toBeGreaterThanOrEqual(25)
83-
expect(nowDiff).toBeLessThan(50)
85+
expect(nowDiff).toBeGreaterThanOrEqual(25 * TIMER_MULTIPLER)
86+
expect(nowDiff).toBeLessThan(50 * TIMER_MULTIPLER)
8487
})
8588

86-
await new Promise((resolve) => setTimeout(resolve, 50))
89+
await new Promise((resolve) => setTimeout(resolve, 50 * TIMER_MULTIPLER))
8790
})
8891

8992
it("promise limit: prevent multiple simultaneous processes", async () => {
@@ -92,7 +95,7 @@ describe("utils", () => {
9295
const processesLimit = promiseLimit(2)
9396

9497
const delay = (): Promise<unknown> =>
95-
new Promise((resolve) => setTimeout(resolve, 25))
98+
new Promise((resolve) => setTimeout(resolve, 25 * TIMER_MULTIPLER))
9699

97100
const now = Date.now()
98101

@@ -105,7 +108,7 @@ describe("utils", () => {
105108
])
106109

107110
// The total time should be 50ms.
108-
expect(Date.now() - now).toBeGreaterThanOrEqual(50)
111+
expect(Date.now() - now).toBeGreaterThanOrEqual(50 * TIMER_MULTIPLER)
109112
})
110113

111114
it("promise limit: run all processes simultaneous (no limit)", async () => {
@@ -114,7 +117,7 @@ describe("utils", () => {
114117
const processesLimit = promiseLimit(0)
115118

116119
const delay = (): Promise<unknown> =>
117-
new Promise((resolve) => setTimeout(resolve, 25))
120+
new Promise((resolve) => setTimeout(resolve, 25 * TIMER_MULTIPLER))
118121

119122
const now = Date.now()
120123

@@ -126,7 +129,7 @@ describe("utils", () => {
126129
])
127130

128131
// The total time should be lower than 50ms.
129-
expect(Date.now() - now).toBeLessThan(50)
132+
expect(Date.now() - now).toBeLessThan(50 * TIMER_MULTIPLER)
130133
})
131134

132135
it("cache enabled (mock function-only)", () => {

0 commit comments

Comments
 (0)