Skip to content

Commit

Permalink
build: switch to vitest and fetch-mock v11 (#648)
Browse files Browse the repository at this point in the history
Co-authored-by: wolfy1339 <webmaster@wolfy1339.com>
  • Loading branch information
oscard0m and wolfy1339 authored Oct 3, 2024
1 parent 2a11fbd commit c6e9688
Show file tree
Hide file tree
Showing 10 changed files with 1,910 additions and 5,964 deletions.
7,731 changes: 1,849 additions & 5,882 deletions package-lock.json

Large diffs are not rendered by default.

39 changes: 7 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
"test": "vitest run --coverage",
"test:ui": "vitest --ui --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2022 --module node16 --moduleResolution node16 --exactOptionalPropertyTypes test/typescript-validate.ts"
},
"repository": "github:octokit/auth-app.js",
Expand All @@ -36,42 +37,16 @@
},
"devDependencies": {
"@octokit/tsconfig": "^4.0.0",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
"@vitest/coverage-v8": "^2.1.2",
"@vitest/ui": "^2.1.2",
"esbuild": "^0.24.0",
"fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1",
"fetch-mock": "^11.0.0",
"glob": "^11.0.0",
"jest": "^29.0.0",
"prettier": "3.3.3",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"ts-jest": "^29.0.0",
"typescript": "^5.0.0"
},
"jest": {
"extensionsToTreatAsEsm": [
".ts"
],
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json",
"useESM": true
}
]
},
"coverageThreshold": {
"global": {
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100
}
},
"moduleNameMapper": {
"^(.+)\\.jsx?$": "$1"
}
"typescript": "^5.0.0",
"vitest": "^2.1.2"
},
"release": {
"plugins": [
Expand Down
2 changes: 1 addition & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://github.com/isaacs/node-lru-cache#readme
import { LRUCache } from "lru-cache";

/* istanbul ignore next */
/* v8 ignore next */
import type {
InstallationAuthOptions,
Cache,
Expand Down
4 changes: 2 additions & 2 deletions src/get-installation-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export async function getInstallationAuthentication(
payload,
);

/* istanbul ignore next - permissions are optional per OpenAPI spec, but we think that is incorrect */
/* v8 ignore next - permissions are optional per OpenAPI spec, but we think that is incorrect */
const permissions = permissionsOptional || {};
/* istanbul ignore next - repositorySelection are optional per OpenAPI spec, but we think that is incorrect */
/* v8 ignore next - repositorySelection are optional per OpenAPI spec, but we think that is incorrect */
const repositorySelection = repositorySelectionOptional || "all";

const repositoryIds = repositories
Expand Down
3 changes: 2 additions & 1 deletion src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async function sendRequestWithRetries(
): Promise<AnyResponse> {
const timeSinceTokenCreationInMs = +new Date() - +new Date(createdAt);

/* istanbul ignore next - due to skipped tests, see https://github.com/octokit/auth-app.js/pull/580 */
/* v8 ignore start - due to skipped tests, see https://github.com/octokit/auth-app.js/pull/580 */
try {
return await request(options);
} catch (error: any) {
Expand Down Expand Up @@ -150,4 +150,5 @@ async function sendRequestWithRetries(

return sendRequestWithRetries(state, request, options, createdAt, retries);
}
/* v8 ignore end */
}
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as OctokitTypes from "@octokit/types";
import { LRUCache } from "lru-cache";
import * as OAuthAppAuth from "@octokit/auth-oauth-app";
import type * as OctokitTypes from "@octokit/types";
import type { LRUCache } from "lru-cache";
import type * as OAuthAppAuth from "@octokit/auth-oauth-app";

// STRATEGY OPTIONS

Expand Down
64 changes: 32 additions & 32 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetchMock, { type MockMatcherFunction } from "fetch-mock";

import { request } from "@octokit/request";
import { jest } from "@jest/globals";
import { vi, beforeEach, test, expect } from "vitest";

import { createAppAuth, createOAuthUserAuth } from "../src/index.ts";
import type { FactoryInstallation } from "../src/types.ts";
Expand Down Expand Up @@ -39,7 +39,7 @@ const BEARER =
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOi0zMCwiZXhwIjo1NzAsImlzcyI6MX0.q3foRa78U3WegM5PrWLEh5N0bH1SD62OqW66ZYzArp95JBNiCbo8KAlGtiRENCIfBZT9ibDUWy82cI4g3F09mdTq3bD1xLavIfmTksIQCz5EymTWR5v6gL14LSmQdWY9lSqkgUG0XCFljWUglEP39H4yeHbFgdjvAYg3ifDS12z9oQz2ACdSpvxPiTuCC804HkPVw8Qoy0OSXvCkFU70l7VXCVUxnuhHnk8-oCGcKUspmeP6UdDnXk-Aus-eGwDfJbU2WritxxaXw6B4a3flTPojkYLSkPBr6Pi0H2-mBsW_Nvs0aLPVLKobQd4gqTkosX3967DoAG8luUMhrnxe8Q";

beforeEach(() => {
jest.useFakeTimers().setSystemTime(0);
vi.useFakeTimers().setSystemTime(0);
});

test("README example for app auth", async () => {
Expand Down Expand Up @@ -976,7 +976,7 @@ test("oauth-user device flow", async () => {
user_code: "usercode123",
verification_uri: "https://github.com/login/device",
expires_in: 900,
// use low number because jest.useFakeTimers() & jest.runAllTimers() didn't work for me
// use low number because vi.useFakeTimers() & vi.runAllTimers() didn't work for me
interval: 0.005,
},
{
Expand Down Expand Up @@ -1026,7 +1026,7 @@ test("oauth-user device flow", async () => {
}),
});

const onVerification = jest.fn();
const onVerification = vi.fn();
const authentication = await auth({
type: "oauth-user",
onVerification,
Expand Down Expand Up @@ -1176,10 +1176,10 @@ test("caches based on installation id", async () => {

test("supports custom cache", async () => {
const CACHE: { [key: string]: string } = {};
const get = jest
const get = vi
.fn<(key: string) => string>()
.mockImplementation((key) => CACHE[key]);
const set = jest
const set = vi
.fn<(key: string, value: string) => void>()
.mockImplementation((key, value) => {
CACHE[key] = value;
Expand Down Expand Up @@ -1264,10 +1264,10 @@ test("supports custom cache", async () => {

test("supports custom cache with async get/set", async () => {
const CACHE: { [key: string]: string } = {};
const get = jest
const get = vi
.fn<(key: string) => Promise<string>>()
.mockImplementation(async (key) => CACHE[key]);
const set = jest
const set = vi
.fn<(key: string, value: string) => Promise<void>>()
.mockImplementation(async (key, value) => {
CACHE[key] = value;
Expand Down Expand Up @@ -1537,7 +1537,7 @@ test("auth.hook(): handle 401 due to an exp timestamp in the past", async () =>
};
});

global.console.warn = jest.fn();
global.console.warn = vi.fn();

const auth = createAppAuth({
appId: APP_ID,
Expand Down Expand Up @@ -1579,7 +1579,7 @@ test("auth.hook(): handle 401 due to an exp timestamp in the past with 800 secon
const fakeTimeMs = 1029392939;
const githubTimeMs = fakeTimeMs + 800000;

jest.setSystemTime(fakeTimeMs);
vi.setSystemTime(fakeTimeMs);

const mock = fetchMock
.sandbox()
Expand Down Expand Up @@ -1614,7 +1614,7 @@ test("auth.hook(): handle 401 due to an exp timestamp in the past with 800 secon
};
});

global.console.warn = jest.fn();
global.console.warn = vi.fn();

const auth = createAppAuth({
appId: APP_ID,
Expand Down Expand Up @@ -1687,7 +1687,7 @@ test("auth.hook(): handle 401 due to an iat timestamp in the future", async () =
};
});

global.console.warn = jest.fn();
global.console.warn = vi.fn();

const auth = createAppAuth({
appId: APP_ID,
Expand Down Expand Up @@ -1744,7 +1744,7 @@ test("auth.hook(): throw 401 error in app auth flow without timing errors", asyn
},
});

global.console.warn = jest.fn();
global.console.warn = vi.fn();

const auth = createAppAuth({
appId: APP_ID,
Expand Down Expand Up @@ -1824,7 +1824,7 @@ test.skip("auth.hook(): handle 401 in first 5 seconds (#65)", async () => {
},
);

global.console.warn = jest.fn();
global.console.warn = vi.fn();

const auth = createAppAuth({
appId: APP_ID,
Expand All @@ -1851,10 +1851,10 @@ test.skip("auth.hook(): handle 401 in first 5 seconds (#65)", async () => {

// it takes 3 retries until a total time of more than 5s pass
// 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 vi.advanceTimersByTimeAsync(100);
await vi.advanceTimersByTimeAsync(1000);
await vi.advanceTimersByTimeAsync(2000);
await vi.advanceTimersByTimeAsync(3000);

const { data } = await promise;

Expand All @@ -1875,7 +1875,7 @@ test.skip("auth.hook(): handle 401 in first 5 seconds (#65)", async () => {
// skipping flaky test, see https://github.com/octokit/auth-app.js/pull/580
test.skip("auth.hook(): throw error with custom message after unsuccessful retries (#163)", async () => {
expect.assertions(1);
global.console.warn = jest.fn();
global.console.warn = vi.fn();

const mock = fetchMock
.sandbox()
Expand Down Expand Up @@ -1927,11 +1927,11 @@ test.skip("auth.hook(): throw error with custom message after unsuccessful retri

// it takes 3 retries until a total time of more than 5s pass
// 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 vi.advanceTimersByTimeAsync(100);
await vi.advanceTimersByTimeAsync(1000);
await vi.advanceTimersByTimeAsync(2000);
await vi.advanceTimersByTimeAsync(3000);
await vi.runAllTimersAsync();

await promise;
});
Expand Down Expand Up @@ -1969,7 +1969,7 @@ test("auth.hook(): throws on 500 error without retries", async () => {
},
});

global.console.warn = jest.fn();
global.console.warn = vi.fn();

try {
await requestWithAuth("GET /repos/octocat/hello-world");
Expand Down Expand Up @@ -2025,10 +2025,10 @@ test("oauth endpoint error", async () => {

test("auth.hook() and custom cache", async () => {
const CACHE: { [key: string]: string } = {};
const get = jest
const get = vi
.fn<(key: string) => Promise<string>>()
.mockImplementation(async (key) => CACHE[key]);
const set = jest
const set = vi
.fn<(key: string, value: string) => Promise<void>>()
.mockImplementation(async (key, value) => {
CACHE[key] = value;
Expand Down Expand Up @@ -2223,7 +2223,7 @@ test("factory auth option", async () => {
extra2: "value2",
});

const factory = jest
const factory = vi
.fn<FactoryInstallation<any>>()
.mockReturnValue({ token: "secret" });

Expand Down Expand Up @@ -2298,7 +2298,7 @@ test("Do not intercept auth.hook(request, 'POST https://github.com/login/oauth/a
});
});

it("throws helpful error if `appId` is not set (#184)", async () => {
test("throws helpful error if `appId` is not set (#184)", async () => {
expect(() => {
createAppAuth({
// @ts-ignore
Expand All @@ -2308,7 +2308,7 @@ it("throws helpful error if `appId` is not set (#184)", async () => {
}).toThrowError("[@octokit/auth-app] appId option is required");
});

it("allows passing an `appId` as a non numeric value", async () => {
test("allows passing an `appId` as a non numeric value", async () => {
expect(() => {
createAppAuth({
appId: "Iv1.0123456789abcdef",
Expand All @@ -2317,7 +2317,7 @@ it("allows passing an `appId` as a non numeric value", async () => {
}).not.toThrow();
});

it("throws helpful error if `privateKey` is not set properly (#184)", async () => {
test("throws helpful error if `privateKey` is not set properly (#184)", async () => {
expect(() => {
createAppAuth({
appId: APP_ID,
Expand All @@ -2327,7 +2327,7 @@ it("throws helpful error if `privateKey` is not set properly (#184)", async () =
}).toThrowError("[@octokit/auth-app] privateKey option is required");
});

it("throws helpful error if `installationId` is set to a falsy value in createAppAuth() (#184)", async () => {
test("throws helpful error if `installationId` is set to a falsy value in createAppAuth() (#184)", async () => {
expect(() => {
createAppAuth({
appId: APP_ID,
Expand Down
1 change: 1 addition & 0 deletions test/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createAppAuth, createOAuthUserAuth } from "../src/index.ts";
import { describe, test, expect } from "vitest";

describe("smoke tests", () => {
test("createAppAuth() is a function", () => {
Expand Down
11 changes: 0 additions & 11 deletions test/tsconfig.test.json

This file was deleted.

13 changes: 13 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "vite";

export default defineConfig({
test: {
coverage: {
include: ["src/**/*.ts"],
reporter: ["html"],
thresholds: {
100: true
},
},
},
});

0 comments on commit c6e9688

Please sign in to comment.