Skip to content

Commit c6e9688

Browse files
oscard0mwolfy1339
andauthored
build: switch to vitest and fetch-mock v11 (#648)
Co-authored-by: wolfy1339 <[email protected]>
1 parent 2a11fbd commit c6e9688

10 files changed

Lines changed: 1910 additions & 5964 deletions

package-lock.json

Lines changed: 1849 additions & 5882 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"lint": "prettier --check '{src,test,scripts}/**/*.{ts,md}' README.md *.json",
1313
"lint:fix": "prettier --write '{src,test,scripts}/**/*.{ts,md}' README.md *.json",
1414
"pretest": "npm run -s lint",
15-
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
15+
"test": "vitest run --coverage",
16+
"test:ui": "vitest --ui --coverage",
1617
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2022 --module node16 --moduleResolution node16 --exactOptionalPropertyTypes test/typescript-validate.ts"
1718
},
1819
"repository": "github:octokit/auth-app.js",
@@ -36,42 +37,16 @@
3637
},
3738
"devDependencies": {
3839
"@octokit/tsconfig": "^4.0.0",
39-
"@types/fetch-mock": "^7.3.1",
40-
"@types/jest": "^29.0.0",
4140
"@types/node": "^20.0.0",
41+
"@vitest/coverage-v8": "^2.1.2",
42+
"@vitest/ui": "^2.1.2",
4243
"esbuild": "^0.24.0",
43-
"fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1",
44+
"fetch-mock": "^11.0.0",
4445
"glob": "^11.0.0",
45-
"jest": "^29.0.0",
4646
"prettier": "3.3.3",
4747
"semantic-release-plugin-update-version-in-files": "^1.0.0",
48-
"ts-jest": "^29.0.0",
49-
"typescript": "^5.0.0"
50-
},
51-
"jest": {
52-
"extensionsToTreatAsEsm": [
53-
".ts"
54-
],
55-
"transform": {
56-
"^.+\\.(ts|tsx)$": [
57-
"ts-jest",
58-
{
59-
"tsconfig": "test/tsconfig.test.json",
60-
"useESM": true
61-
}
62-
]
63-
},
64-
"coverageThreshold": {
65-
"global": {
66-
"statements": 100,
67-
"branches": 100,
68-
"functions": 100,
69-
"lines": 100
70-
}
71-
},
72-
"moduleNameMapper": {
73-
"^(.+)\\.jsx?$": "$1"
74-
}
48+
"typescript": "^5.0.0",
49+
"vitest": "^2.1.2"
7550
},
7651
"release": {
7752
"plugins": [

src/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://github.com/isaacs/node-lru-cache#readme
22
import { LRUCache } from "lru-cache";
33

4-
/* istanbul ignore next */
4+
/* v8 ignore next */
55
import type {
66
InstallationAuthOptions,
77
Cache,

src/get-installation-authentication.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ export async function getInstallationAuthentication(
108108
payload,
109109
);
110110

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

116116
const repositoryIds = repositories

src/hook.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async function sendRequestWithRetries(
121121
): Promise<AnyResponse> {
122122
const timeSinceTokenCreationInMs = +new Date() - +new Date(createdAt);
123123

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

151151
return sendRequestWithRetries(state, request, options, createdAt, retries);
152152
}
153+
/* v8 ignore end */
153154
}

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as OctokitTypes from "@octokit/types";
2-
import { LRUCache } from "lru-cache";
3-
import * as OAuthAppAuth from "@octokit/auth-oauth-app";
1+
import type * as OctokitTypes from "@octokit/types";
2+
import type { LRUCache } from "lru-cache";
3+
import type * as OAuthAppAuth from "@octokit/auth-oauth-app";
44

55
// STRATEGY OPTIONS
66

test/index.test.ts

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

33
import { request } from "@octokit/request";
4-
import { jest } from "@jest/globals";
4+
import { vi, beforeEach, test, expect } from "vitest";
55

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

4141
beforeEach(() => {
42-
jest.useFakeTimers().setSystemTime(0);
42+
vi.useFakeTimers().setSystemTime(0);
4343
});
4444

4545
test("README example for app auth", async () => {
@@ -976,7 +976,7 @@ test("oauth-user device flow", async () => {
976976
user_code: "usercode123",
977977
verification_uri: "https://github.com/login/device",
978978
expires_in: 900,
979-
// use low number because jest.useFakeTimers() & jest.runAllTimers() didn't work for me
979+
// use low number because vi.useFakeTimers() & vi.runAllTimers() didn't work for me
980980
interval: 0.005,
981981
},
982982
{
@@ -1026,7 +1026,7 @@ test("oauth-user device flow", async () => {
10261026
}),
10271027
});
10281028

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

11771177
test("supports custom cache", async () => {
11781178
const CACHE: { [key: string]: string } = {};
1179-
const get = jest
1179+
const get = vi
11801180
.fn<(key: string) => string>()
11811181
.mockImplementation((key) => CACHE[key]);
1182-
const set = jest
1182+
const set = vi
11831183
.fn<(key: string, value: string) => void>()
11841184
.mockImplementation((key, value) => {
11851185
CACHE[key] = value;
@@ -1264,10 +1264,10 @@ test("supports custom cache", async () => {
12641264

12651265
test("supports custom cache with async get/set", async () => {
12661266
const CACHE: { [key: string]: string } = {};
1267-
const get = jest
1267+
const get = vi
12681268
.fn<(key: string) => Promise<string>>()
12691269
.mockImplementation(async (key) => CACHE[key]);
1270-
const set = jest
1270+
const set = vi
12711271
.fn<(key: string, value: string) => Promise<void>>()
12721272
.mockImplementation(async (key, value) => {
12731273
CACHE[key] = value;
@@ -1537,7 +1537,7 @@ test("auth.hook(): handle 401 due to an exp timestamp in the past", async () =>
15371537
};
15381538
});
15391539

1540-
global.console.warn = jest.fn();
1540+
global.console.warn = vi.fn();
15411541

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

1582-
jest.setSystemTime(fakeTimeMs);
1582+
vi.setSystemTime(fakeTimeMs);
15831583

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

1617-
global.console.warn = jest.fn();
1617+
global.console.warn = vi.fn();
16181618

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

1690-
global.console.warn = jest.fn();
1690+
global.console.warn = vi.fn();
16911691

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

1747-
global.console.warn = jest.fn();
1747+
global.console.warn = vi.fn();
17481748

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

1827-
global.console.warn = jest.fn();
1827+
global.console.warn = vi.fn();
18281828

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

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

18591859
const { data } = await promise;
18601860

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

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

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

19361936
await promise;
19371937
});
@@ -1969,7 +1969,7 @@ test("auth.hook(): throws on 500 error without retries", async () => {
19691969
},
19701970
});
19711971

1972-
global.console.warn = jest.fn();
1972+
global.console.warn = vi.fn();
19731973

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

20262026
test("auth.hook() and custom cache", async () => {
20272027
const CACHE: { [key: string]: string } = {};
2028-
const get = jest
2028+
const get = vi
20292029
.fn<(key: string) => Promise<string>>()
20302030
.mockImplementation(async (key) => CACHE[key]);
2031-
const set = jest
2031+
const set = vi
20322032
.fn<(key: string, value: string) => Promise<void>>()
20332033
.mockImplementation(async (key, value) => {
20342034
CACHE[key] = value;
@@ -2223,7 +2223,7 @@ test("factory auth option", async () => {
22232223
extra2: "value2",
22242224
});
22252225

2226-
const factory = jest
2226+
const factory = vi
22272227
.fn<FactoryInstallation<any>>()
22282228
.mockReturnValue({ token: "secret" });
22292229

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

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

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

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

2330-
it("throws helpful error if `installationId` is set to a falsy value in createAppAuth() (#184)", async () => {
2330+
test("throws helpful error if `installationId` is set to a falsy value in createAppAuth() (#184)", async () => {
23312331
expect(() => {
23322332
createAppAuth({
23332333
appId: APP_ID,

test/smoke.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createAppAuth, createOAuthUserAuth } from "../src/index.ts";
2+
import { describe, test, expect } from "vitest";
23

34
describe("smoke tests", () => {
45
test("createAppAuth() is a function", () => {

test/tsconfig.test.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

vite.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
test: {
5+
coverage: {
6+
include: ["src/**/*.ts"],
7+
reporter: ["html"],
8+
thresholds: {
9+
100: true
10+
},
11+
},
12+
},
13+
});

0 commit comments

Comments
 (0)