Skip to content

Commit 4d8adc9

Browse files
committed
fix tests
1 parent ba4c656 commit 4d8adc9

File tree

2 files changed

+18
-33
lines changed

2 files changed

+18
-33
lines changed

packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @ts-check
2+
23
import path from "path"
34
import zlib from "zlib"
45
import os from "os"
@@ -347,8 +348,6 @@ describe(`fetch-remote-file`, () => {
347348
jest.runAllTimers()
348349
await requests[0]
349350

350-
jest.useRealTimers()
351-
352351
// we still expect 2 fetches because cache can't save fast enough
353352
expect(gotStream).toBeCalledTimes(2)
354353
expect(fsMove).toBeCalledTimes(1)
@@ -408,20 +407,12 @@ describe(`fetch-remote-file`, () => {
408407
jest.runAllTimers()
409408
await requests[0]
410409

411-
jest.useRealTimers()
412-
413410
// we still expect 4 fetches because cache can't save fast enough
414411
expect(gotStream).toBeCalledTimes(4)
415412
expect(fsMove).toBeCalledTimes(2)
416413
})
417414

418415
it(`doesn't keep lock when file download failed`, async () => {
419-
jest.setTimeout(7000)
420-
421-
// we don't want to wait for polling to finish
422-
jest.useFakeTimers()
423-
jest.runAllTimers()
424-
425416
const cacheInternals = new Map()
426417
const workerCache = {
427418
get(key) {
@@ -443,17 +434,13 @@ describe(`fetch-remote-file`, () => {
443434
})
444435
).rejects.toThrow()
445436

446-
jest.runAllTimers()
447-
448437
await expect(
449438
fetchRemoteFileInstanceTwo({
450439
url: `http://external.com/500.jpg`,
451440
cache: workerCache,
452441
})
453442
).rejects.toThrow()
454443

455-
jest.useRealTimers()
456-
457444
expect(gotStream).toBeCalledTimes(3)
458445
expect(fsMove).toBeCalledTimes(0)
459446
})
@@ -468,8 +455,6 @@ describe(`fetch-remote-file`, () => {
468455
})
469456

470457
it(`fails when 500 is triggered`, async () => {
471-
jest.setTimeout(7000)
472-
473458
await expect(
474459
fetchRemoteFile({
475460
url: `http://external.com/500.jpg`,
@@ -529,13 +514,13 @@ Fetch details:
529514
})
530515

531516
it(`Retries when server returns 503 error till server returns 200`, async () => {
532-
jest.setTimeout(7000)
533-
534-
const filePath = await fetchRemoteFile({
517+
const fetchRemoteFileInstance = fetchRemoteFile({
535518
url: `http://external.com/503-twice.svg`,
536519
cache,
537520
})
538521

522+
const filePath = await fetchRemoteFileInstance
523+
539524
expect(path.basename(filePath)).toBe(`503-twice.svg`)
540525
expect(getFileSize(filePath)).resolves.toBe(
541526
await getFileSize(path.join(__dirname, `./fixtures/gatsby-logo.svg`))
@@ -544,8 +529,6 @@ Fetch details:
544529
})
545530

546531
it(`Stops retry when maximum attempts is reached`, async () => {
547-
jest.setTimeout(7000)
548-
549532
await expect(
550533
fetchRemoteFile({
551534
url: `http://external.com/503-forever.svg`,
@@ -579,9 +562,7 @@ Fetch details:
579562
expect(gotStream).toBeCalledTimes(3)
580563
})
581564
// @todo retry on network errors
582-
it.skip(`Retries on network errors`, async () => {
583-
jest.setTimeout(7000)
584-
565+
it(`Retries on network errors`, async () => {
585566
await expect(
586567
fetchRemoteFile({
587568
url: `http://external.com/network-error.svg`,

packages/gatsby-core-utils/src/fetch-remote-file.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const INCOMPLETE_RETRY_LIMIT = process.env.GATSBY_INCOMPLETE_RETRY_LIMIT
4848
? parseInt(process.env.GATSBY_INCOMPLETE_RETRY_LIMIT, 10)
4949
: 3
5050

51+
// jest doesn't allow us to run all timings infinitely, so we set it 0 in tests
52+
const BACKOFF_TIME = process.env.NODE_ENV === `test` ? 0 : 1000
53+
5154
function range(start: number, end: number): Array<number> {
5255
return Array(end - start)
5356
.fill(null)
@@ -396,16 +399,17 @@ function requestRemoteNode(
396399
// (error.code && ERROR_CODES_TO_RETRY.includes(error.code))
397400
) {
398401
if (attempt < INCOMPLETE_RETRY_LIMIT) {
399-
// @todo The tests don't like the delay. I tried several ways and positions.
400-
new Promise(resolve => setTimeout(resolve, 1000 * attempt)).then(() =>
401-
requestRemoteNode(
402-
url,
403-
headers,
404-
tmpFilename,
405-
httpOptions,
406-
attempt + 1
402+
setTimeout(() => {
403+
resolve(
404+
requestRemoteNode(
405+
url,
406+
headers,
407+
tmpFilename,
408+
httpOptions,
409+
attempt + 1
410+
)
407411
)
408-
)
412+
}, BACKOFF_TIME * attempt)
409413

410414
return undefined
411415
} else {

0 commit comments

Comments
 (0)