From 4acb051cfa4fe5aceaab53ef1f6a281595447cd1 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Tue, 30 Apr 2024 16:15:31 +0000 Subject: [PATCH] test: use jest built-in fake timers API --- test/index.test.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index b97f9854..40effaac 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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"; @@ -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 () => { @@ -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) => { @@ -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; @@ -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() @@ -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 () => {