Skip to content

Commit

Permalink
test: use jest built-in fake timers API
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored Apr 30, 2024
1 parent dffdb23 commit 4acb051
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fetchMock, { type MockMatcherFunction } from "fetch-mock";

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

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

let clock: InstalledClock;
beforeEach(() => {
clock = install({ now: 0, toFake: ["Date", "setTimeout"] });
jest.useFakeTimers().setSystemTime(0);
});

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

clock = install({ now: fakeTimeMs, toFake: ["Date", "setTimeout"] });
jest.setSystemTime(fakeTimeMs);

const mock = fetchMock
.sandbox()
.get("https://api.github.com/app", (_url, options) => {
Expand Down Expand Up @@ -1850,9 +1849,11 @@ test("auth.hook(): handle 401 in first 5 seconds (#65)", async () => {
const promise = requestWithAuth("GET /repos/octocat/hello-world");

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

const { data } = await promise;

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

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

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

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

requestWithAuth("GET /repos/octocat/hello-world").catch((error) => {
promise.catch((error) => {
expect(error.message).toBe(
`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`,
);
});

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

await promise;
});

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

0 comments on commit 4acb051

Please sign in to comment.