Skip to content

Commit

Permalink
build: switch to vitest (#700)
Browse files Browse the repository at this point in the history
* build: switch to vitest
* chore: use type imports
* build: update dependencies
   - Update fetch-mock to v11, closes #694
   - Removes un-used http-proxy-agent devDependency
  • Loading branch information
wolfy1339 committed Sep 18, 2024
1 parent 909fa6d commit d5658e7
Show file tree
Hide file tree
Showing 16 changed files with 3,283 additions and 5,405 deletions.
8,544 changes: 3,210 additions & 5,334 deletions package-lock.json

Large diffs are not rendered by default.

36 changes: 5 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"lint": "prettier --check '{src,test}/**/*.{ts,md}' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*.{ts,md}' README.md package.json",
"pretest": "npm run -s lint",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
"test": "vitest --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --module node16 --moduleResolution node16 --allowImportingTsExtensions test/typescript-validate.ts"
},
"repository": "github:octokit/core.js",
Expand Down Expand Up @@ -44,43 +44,17 @@
"@types/lolex": "^5.1.0",
"@types/node": "^20.0.0",
"@types/sinonjs__fake-timers": "^8.1.5",
"@vitest/coverage-v8": "^2.1.1",
"esbuild": "^0.23.0",
"fetch-mock": "^10.0.0",
"fetch-mock": "^11.0.0",
"glob": "^11.0.0",
"http-proxy-agent": "^7.0.0",
"jest": "^29.0.0",
"prettier": "3.3.3",
"proxy": "^2.0.0",
"semantic-release": "^24.0.0",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"ts-jest": "^29.0.0",
"typescript": "^5.0.0",
"undici": "^6.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"
}
"undici": "^6.0.0",
"vitest": "^2.1.1"
},
"release": {
"branches": [
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as OctokitTypes from "@octokit/types";
import { RequestError } from "@octokit/request-error";
import type * as OctokitTypes from "@octokit/types";
import type { RequestError } from "@octokit/request-error";

import type { Octokit } from "./index.js";

Expand Down
56 changes: 32 additions & 24 deletions test/agent-ca/agent-ca-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,41 @@ import { resolve } from "node:path";
import { fetch as undiciFetch, Agent } from "undici";
import { request } from "@octokit/request";
import { type AddressInfo } from "node:net";
import { afterAll, beforeAll, describe, expect, it } from "vitest";

const __dirname = new URL(".", import.meta.url).pathname;
const ca = readFileSync(resolve(__dirname, "./ca.crt"));

describe("custom client certificate", () => {
let server: Server;

beforeAll((done) => {
// Stand up a server that requires a client certificate
// requestCert forces the server to request a certificate
// rejectUnauthorized: false allows us to test with a self-signed certificate
server = createServer(
{
key: readFileSync(resolve(__dirname, "./localhost.key")),
cert: readFileSync(resolve(__dirname, "./localhost.crt")),
requestCert: true,
rejectUnauthorized: false,
},
(request: any, response: any) => {
expect(request.method).toEqual("GET");
expect(request.url).toEqual("/");
beforeAll(
() =>
new Promise((done) => {
// Stand up a server that requires a client certificate
// requestCert forces the server to request a certificate
// rejectUnauthorized: false allows us to test with a self-signed certificate
server = createServer(
{
key: readFileSync(resolve(__dirname, "./localhost.key")),
cert: readFileSync(resolve(__dirname, "./localhost.crt")),
requestCert: true,
rejectUnauthorized: false,
},
(request: any, response: any) => {
expect(request.method).toEqual("GET");
expect(request.url).toEqual("/");

response.writeHead(200);
response.write("ok");
response.end();
},
);
response.writeHead(200);
response.write("ok");
response.end();
},
);

server.listen(0, done);
});
// @ts-expect-error
server.listen(0, done);
}),
);

it("https.Agent({ca})", () => {
// Setup a dispatcher that uses the undici agent
Expand Down Expand Up @@ -85,7 +90,10 @@ describe("custom client certificate", () => {
});
});

afterAll((done) => {
server.close(done);
});
afterAll(
() =>
new Promise((done) => {
server.close(done);
}),
);
});
1 change: 1 addition & 0 deletions test/agent-proxy/agent-proxy-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { type AddressInfo } from "node:net";
import { type ProxyServer, createProxy } from "proxy";
import { ProxyAgent, fetch as undiciFetch } from "undici";
import { Octokit } from "../../src/index.ts";
import { afterEach, beforeEach, describe, expect, it } from "vitest";

describe("client proxy", () => {
let server: Server;
Expand Down
16 changes: 7 additions & 9 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createAppAuth } from "@octokit/auth-app";
import { createActionAuth } from "@octokit/auth-action";
import { createOAuthAppAuth } from "@octokit/auth-oauth-app";
import { install as installFakeTimers, type Clock } from "@sinonjs/fake-timers";
import { jest } from "@jest/globals";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";

import { Octokit } from "../src/index.ts";

Expand Down Expand Up @@ -48,7 +48,7 @@ beforeAll(() => {
// unless `token.fingerprint` option was passed. The fingerprint is
// calculated using `Math.random().toString(36).substr(2)`, so the
// default fingerprint is always `"4feornbt361"`.
Math.random = jest.fn<Math["random"]>().mockReturnValue(0.123);
Math.random = vi.fn<Math["random"]>().mockReturnValue(0.123);

// A timestamp is added to the default token note, e.g.
// "octokit 2019-07-04 4feornbt361". sinon-fake-timers mocks the Date class so
Expand Down Expand Up @@ -361,14 +361,14 @@ describe("Authentication", () => {
{ overwriteRoutes: false },
);

const mockWarnLogger = jest.fn();
const mockWarnLogger = vi.fn();

const octokit = new Octokit({
log: {
debug: jest.fn(),
info: jest.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: mockWarnLogger,
error: jest.fn(),
error: vi.fn(),
},
authStrategy: createAppAuth,
auth: {
Expand All @@ -390,7 +390,7 @@ describe("Authentication", () => {
});

it("should pass octokit and octokitOptions if a custom authStrategy was set", () => {
const authStrategy = jest.fn().mockReturnValue({
const authStrategy = vi.fn().mockReturnValue({
hook() {},
});
new Octokit({
Expand All @@ -403,15 +403,13 @@ describe("Authentication", () => {

const strategyOptions = authStrategy.mock.calls[0][0];

// @ts-expect-error The types here don't work
expect(Object.keys(strategyOptions).sort()).toStrictEqual([
"log",
"octokit",
"octokitOptions",
"request",
"secret",
]);
// @ts-expect-error The types here don't work
expect(strategyOptions.octokitOptions).toStrictEqual({
auth: {
secret: "123",
Expand Down
1 change: 1 addition & 0 deletions test/constructor.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Octokit } from "../src/index.ts";
import fetchMock from "fetch-mock";
import { describe, expect, it } from "vitest";

describe("Smoke test", () => {
it("previews option", () => {
Expand Down
1 change: 1 addition & 0 deletions test/defaults.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetchMock from "fetch-mock";
import { getUserAgent } from "universal-user-agent";
import { createActionAuth } from "@octokit/auth-action";
import { describe, expect, it } from "vitest";

import { Octokit } from "../src/index.ts";

Expand Down
1 change: 1 addition & 0 deletions test/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetchMock from "fetch-mock";
import { describe, expect, it } from "vitest";

import { Octokit } from "../src/index.ts";

Expand Down
1 change: 1 addition & 0 deletions test/hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getUserAgent } from "universal-user-agent";
import fetchMock from "fetch-mock";
import { describe, expect, it } from "vitest";

import { Octokit } from "../src/index.ts";

Expand Down
1 change: 1 addition & 0 deletions test/issues.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Octokit } from "../src/index.ts";
import fetchMock from "fetch-mock";
import { describe, expect, test } from "vitest";

/*
💖 Welcome, dear contributor!
Expand Down
10 changes: 5 additions & 5 deletions test/log.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { jest } from "@jest/globals";
import { describe, expect, it, vi } from "vitest";

describe("octokit.log", () => {
it(".debug() and .info() are no-ops by default", async () => {
const calls: String[] = [];

const debug = jest
const debug = vi
.spyOn(console, "debug")
.mockImplementation(() => calls.push("debug"));
const info = jest
const info = vi
.spyOn(console, "info")
.mockImplementation(() => calls.push("info"));
const warn = jest
const warn = vi
.spyOn(console, "warn")
.mockImplementation(() => calls.push("warn"));
const error = jest
const error = vi
.spyOn(console, "error")
.mockImplementation(() => calls.push("error"));
const Octokit = (await import("../src/index.ts")).Octokit;
Expand Down
1 change: 1 addition & 0 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, it } from "vitest";
import { Octokit } from "../src/index.ts";

const pluginFoo = () => {
Expand Down
1 change: 1 addition & 0 deletions test/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getUserAgent } from "universal-user-agent";
import fetchMock from "fetch-mock";
import { describe, expect, it } from "vitest";

import { Octokit } from "../src/index.ts";

Expand Down
1 change: 1 addition & 0 deletions test/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, it } from "vitest";
import { Octokit } from "../src/index.ts";

describe("Smoke test", () => {
Expand Down
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 d5658e7

Please sign in to comment.