From 3aa6dab2fc2e8f558bdd456477f09829d9fe21c5 Mon Sep 17 00:00:00 2001 From: wolfy1339 Date: Sat, 24 Feb 2024 17:35:18 -0500 Subject: [PATCH] test: adapt for latest code --- package.json | 4 ++-- test/index.test.ts | 50 ++++++++++++++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index f39aa941..218fb05d 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,8 @@ "lint": "prettier --check '{src,test,scripts}/**/*.{ts,md}' README.md *.json", "lint:fix": "prettier --write '{src,test,scripts}/**/*.{ts,md}' README.md *.json", "pretest": "npm run -s lint", - "test": "jest --coverage", - "test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2018 --moduleResolution node test/typescript-validate.ts" + "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage", + "test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2022 --module node16 --moduleResolution node16 test/typescript-validate.ts" }, "repository": "github:octokit/auth-app.js", "keywords": [ diff --git a/test/index.test.ts b/test/index.test.ts index 178f0567..9bb63108 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -2,8 +2,10 @@ import fetchMock, { type MockMatcherFunction } from "fetch-mock"; import { request } from "@octokit/request"; import { install } from "@sinonjs/fake-timers"; +import { jest } from "@jest/globals"; import { createAppAuth, createOAuthUserAuth } from "../src/index.ts"; +import type { FactoryInstallation } from "../src/types.ts"; const APP_ID = 1; const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY----- @@ -1176,10 +1178,14 @@ test("caches based on installation id", async () => { test("supports custom cache", async () => { const CACHE: { [key: string]: string } = {}; - const get = jest.fn().mockImplementation((key) => CACHE[key]); - const set = jest.fn().mockImplementation((key, value) => { - CACHE[key] = value; - }); + const get = jest + .fn<(key: string) => string>() + .mockImplementation((key) => CACHE[key]); + const set = jest + .fn<(key: string, value: string) => void>() + .mockImplementation((key, value) => { + CACHE[key] = value; + }); const requestMock = request.defaults({ headers: { @@ -1243,8 +1249,8 @@ test("supports custom cache", async () => { expect(get).toHaveBeenCalledTimes(4); expect(set).toHaveBeenCalledTimes(3); - expect(get).toBeCalledWith("123"); - expect(set).toBeCalledWith( + expect(get).toHaveBeenCalledWith("123"); + expect(set).toHaveBeenCalledWith( "123", "secret123|1970-01-01T00:00:00.000Z|1970-01-01T01:00:00.000Z|all|metadata|", ); @@ -1260,10 +1266,14 @@ test("supports custom cache", async () => { test("supports custom cache with async get/set", async () => { const CACHE: { [key: string]: string } = {}; - const get = jest.fn().mockImplementation(async (key) => CACHE[key]); - const set = jest.fn().mockImplementation(async (key, value) => { - CACHE[key] = value; - }); + const get = jest + .fn<(key: string) => Promise>() + .mockImplementation(async (key) => CACHE[key]); + const set = jest + .fn<(key: string, value: string) => Promise>() + .mockImplementation(async (key, value) => { + CACHE[key] = value; + }); const requestMock = request.defaults({ headers: { @@ -1305,8 +1315,8 @@ test("supports custom cache with async get/set", async () => { expect(get).toHaveBeenCalledTimes(2); expect(set).toHaveBeenCalledTimes(1); - expect(get).toBeCalledWith("123"); - expect(set).toBeCalledWith( + expect(get).toHaveBeenCalledWith("123"); + expect(set).toHaveBeenCalledWith( "123", "secret123|1970-01-01T00:00:00.000Z|1970-01-01T01:00:00.000Z|all|metadata|", ); @@ -2006,10 +2016,14 @@ test("oauth endpoint error", async () => { test("auth.hook() and custom cache", async () => { const CACHE: { [key: string]: string } = {}; - const get = jest.fn().mockImplementation(async (key) => CACHE[key]); - const set = jest.fn().mockImplementation(async (key, value) => { - CACHE[key] = value; - }); + const get = jest + .fn<(key: string) => Promise>() + .mockImplementation(async (key) => CACHE[key]); + const set = jest + .fn<(key: string, value: string) => Promise>() + .mockImplementation(async (key, value) => { + CACHE[key] = value; + }); const mock = fetchMock .sandbox() @@ -2200,7 +2214,9 @@ test("factory auth option", async () => { extra2: "value2", }); - const factory = jest.fn().mockReturnValue({ token: "secret" }); + const factory = jest + .fn>() + .mockReturnValue({ token: "secret" }); const customAuth = await appAuth({ type: "installation",