Skip to content

Commit 4acb051

Browse files
authored
test: use jest built-in fake timers API
1 parent dffdb23 commit 4acb051

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

test/index.test.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fetchMock, { type MockMatcherFunction } from "fetch-mock";
22

33
import { request } from "@octokit/request";
4-
import { install, type InstalledClock } from "@sinonjs/fake-timers";
54
import { jest } from "@jest/globals";
65

76
import { createAppAuth, createOAuthUserAuth } from "../src/index.ts";
@@ -39,9 +38,8 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
3938
const BEARER =
4039
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOi0zMCwiZXhwIjo1NzAsImlzcyI6MX0.q3foRa78U3WegM5PrWLEh5N0bH1SD62OqW66ZYzArp95JBNiCbo8KAlGtiRENCIfBZT9ibDUWy82cI4g3F09mdTq3bD1xLavIfmTksIQCz5EymTWR5v6gL14LSmQdWY9lSqkgUG0XCFljWUglEP39H4yeHbFgdjvAYg3ifDS12z9oQz2ACdSpvxPiTuCC804HkPVw8Qoy0OSXvCkFU70l7VXCVUxnuhHnk8-oCGcKUspmeP6UdDnXk-Aus-eGwDfJbU2WritxxaXw6B4a3flTPojkYLSkPBr6Pi0H2-mBsW_Nvs0aLPVLKobQd4gqTkosX3967DoAG8luUMhrnxe8Q";
4140

42-
let clock: InstalledClock;
4341
beforeEach(() => {
44-
clock = install({ now: 0, toFake: ["Date", "setTimeout"] });
42+
jest.useFakeTimers().setSystemTime(0);
4543
});
4644

4745
test("README example for app auth", async () => {
@@ -1581,7 +1579,8 @@ test("auth.hook(): handle 401 due to an exp timestamp in the past with 800 secon
15811579
const fakeTimeMs = 1029392939;
15821580
const githubTimeMs = fakeTimeMs + 800000;
15831581

1584-
clock = install({ now: fakeTimeMs, toFake: ["Date", "setTimeout"] });
1582+
jest.setSystemTime(fakeTimeMs);
1583+
15851584
const mock = fetchMock
15861585
.sandbox()
15871586
.get("https://api.github.com/app", (_url, options) => {
@@ -1850,9 +1849,11 @@ test("auth.hook(): handle 401 in first 5 seconds (#65)", async () => {
18501849
const promise = requestWithAuth("GET /repos/octocat/hello-world");
18511850

18521851
// it takes 3 retries until a total time of more than 5s pass
1853-
await clock.tickAsync(1000);
1854-
await clock.tickAsync(2000);
1855-
await clock.tickAsync(3000);
1852+
// Note sure why the first advance is needed, but it helped unblock https://github.com/octokit/auth-app.js/pull/580
1853+
await jest.advanceTimersByTimeAsync(100);
1854+
await jest.advanceTimersByTimeAsync(1000);
1855+
await jest.advanceTimersByTimeAsync(2000);
1856+
await jest.advanceTimersByTimeAsync(3000);
18561857

18571858
const { data } = await promise;
18581859

@@ -1872,6 +1873,7 @@ test("auth.hook(): handle 401 in first 5 seconds (#65)", async () => {
18721873

18731874
test("auth.hook(): throw error with custom message after unsuccessful retries (#163)", async () => {
18741875
expect.assertions(1);
1876+
global.console.warn = jest.fn();
18751877

18761878
const mock = fetchMock
18771879
.sandbox()
@@ -1913,18 +1915,23 @@ test("auth.hook(): throw error with custom message after unsuccessful retries (#
19131915
},
19141916
});
19151917

1916-
global.console.warn = jest.fn();
1918+
const promise = requestWithAuth("GET /repos/octocat/hello-world");
19171919

1918-
requestWithAuth("GET /repos/octocat/hello-world").catch((error) => {
1920+
promise.catch((error) => {
19191921
expect(error.message).toBe(
19201922
`After 3 retries within 6s of creating the installation access token, the response remains 401. At this point, the cause may be an authentication problem or a system outage. Please check https://www.githubstatus.com for status information`,
19211923
);
19221924
});
19231925

19241926
// it takes 3 retries until a total time of more than 5s pass
1925-
await clock.tickAsync(1000);
1926-
await clock.tickAsync(2000);
1927-
await clock.tickAsync(3000);
1927+
// Note sure why the first advance is needed, but it helped unblock https://github.com/octokit/auth-app.js/pull/580
1928+
await jest.advanceTimersByTimeAsync(100);
1929+
await jest.advanceTimersByTimeAsync(1000);
1930+
await jest.advanceTimersByTimeAsync(2000);
1931+
await jest.advanceTimersByTimeAsync(3000);
1932+
await jest.runAllTimersAsync();
1933+
1934+
await promise;
19281935
});
19291936

19301937
test("auth.hook(): throws on 500 error without retries", async () => {

0 commit comments

Comments
 (0)