Skip to content

Commit

Permalink
chore: replace jest with vite (#700)
Browse files Browse the repository at this point in the history
* chore: replace jest with vite

* also lint vite.config.js

* remove artifact
  • Loading branch information
Uzlopak authored Jun 29, 2024
1 parent 9ab60c9 commit 08acbc4
Show file tree
Hide file tree
Showing 10 changed files with 1,261 additions and 4,945 deletions.
4 changes: 0 additions & 4 deletions cypress.json

This file was deleted.

6,076 changes: 1,184 additions & 4,892 deletions package-lock.json

Large diffs are not rendered by default.

42 changes: 5 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"scripts": {
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json vite.config.js",
"pretest": "npm run -s lint",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage"
"test": "vitest run --coverage"
},
"repository": "github:octokit/request.js",
"keywords": [
Expand All @@ -32,47 +32,15 @@
"devDependencies": {
"@octokit/auth-app": "^7.0.0",
"@octokit/tsconfig": "^3.0.0",
"@sinonjs/fake-timers": "^11.2.2",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
"@types/sinonjs__fake-timers": "^8.1.5",
"@vitest/coverage-v8": "^1.6.0",
"esbuild": "^0.21.0",
"fetch-mock": "^10.0.0",
"glob": "^10.2.4",
"jest": "^29.0.0",
"prettier": "3.3.2",
"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
}
},
"modulePathIgnorePatterns": [
"<rootDir>/pkg"
],
"testEnvironment": "node",
"moduleNameMapper": {
"^(.+)\\.jsx?$": "$1"
}
"typescript": "^5.0.0",
"vitest": "^1.6.0"
},
"release": {
"branches": [
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function main() {
delete pkg.scripts;
delete pkg.prettier;
delete pkg.release;
delete pkg.jest;

await writeFile(
"pkg/package.json",
JSON.stringify(
Expand Down
3 changes: 0 additions & 3 deletions src/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,12 @@ function toErrorMessage(data: any) {

let suffix: string;

// istanbul ignore else - just in case
if ("documentation_url" in data) {
suffix = ` - ${data.documentation_url}`;
} else {
suffix = "";
}

// istanbul ignore else - just in case
if ("message" in data) {
if (Array.isArray(data.errors)) {
return `${data.message}: ${data.errors.map(JSON.stringify).join(", ")}${suffix}`;
Expand All @@ -201,6 +199,5 @@ function toErrorMessage(data: any) {
return `${data.message}${suffix}`;
}

// istanbul ignore next - just in case
return `Unknown error: ${JSON.stringify(data)}`;
}
1 change: 1 addition & 0 deletions test/defaults.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetchMock from "fetch-mock";

import { describe, it, expect } from "vitest";
import { request } from "../src/index.ts";

describe("endpoint.defaults()", () => {
Expand Down
1 change: 1 addition & 0 deletions test/is-plain-object.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from "vitest";
import { isPlainObject } from "../src/is-plain-object.ts";

describe("isPlainObject", () => {
Expand Down
61 changes: 54 additions & 7 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import fs from "node:fs";
import stream from "node:stream";
import { ReadableStream } from "node:stream/web";

import { describe, it, expect, vi } from "vitest";
import { getUserAgent } from "universal-user-agent";
import fetchMock from "fetch-mock";
import { createAppAuth } from "@octokit/auth-app";
import fakeTimers from "@sinonjs/fake-timers";
import type {
EndpointOptions,
RequestInterface,
ResponseHeaders,
} from "@octokit/types";

import { request } from "../src/index.ts";
import { jest } from "@jest/globals";

const userAgent = `octokit-request.js/0.0.0-development ${getUserAgent()}`;
const __filename = new URL(import.meta.url);
Expand Down Expand Up @@ -73,7 +72,7 @@ describe("request()", () => {
});

it("README authentication example", async () => {
const clock = fakeTimers.install({
const clock = vi.useFakeTimers({
now: 0,
toFake: ["Date"],
});
Expand Down Expand Up @@ -158,7 +157,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
title: "Hello from the engine room",
});
expect(mock.done()).toBe(true);
clock.reset();
vi.useRealTimers();
});

it("Request with body", () => {
Expand Down Expand Up @@ -808,7 +807,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
},
})
.then(() => {
fail("This should return error.");
throw new Error("This should return error.");
})
.catch((error) => {
expect(error).toHaveProperty(
Expand Down Expand Up @@ -840,7 +839,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
},
);

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

return request("GET /teams/{team_id}", {
headers: {
Expand Down Expand Up @@ -878,7 +877,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
},
);

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

return request("GET /teams/{team_id}", {
headers: {
Expand Down Expand Up @@ -1121,4 +1120,52 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
},
});
});

it("invalid error responses should result in errors with a message field describing the error as an unknown error", function () {
const mock = fetchMock.sandbox().mock(
"https://request-errors-test.com/repos/gr2m/sandbox/branches/gr2m-patch-1/protection",
{
status: 400,
body: {},
},
{
method: "PUT",
headers: {
accept: "application/vnd.github.v3+json",
authorization: "token secret123",
},
},
);

return request("PUT /repos/{owner}/{repo}/branches/{branch}/protection", {
baseUrl: "https://request-errors-test.com",
headers: {
authorization: "token secret123",
},
owner: "gr2m",
repo: "sandbox",
branch: "gr2m-patch-1",
required_status_checks: { strict: true, contexts: ["wip"] },
enforce_admins: true,
required_pull_request_reviews: {
required_approving_review_count: 1,
dismiss_stale_reviews: true,
require_code_owner_reviews: true,
dismissal_restrictions: { users: [], teams: [] },
},
restrictions: { users: [], teams: [] },
request: {
fetch: mock,
},
})
.then(() => {
throw new Error("This should return error.");
})
.catch((error) => {
expect(error).toHaveProperty(
"message",
`Unknown error: ${JSON.stringify({})}`,
);
});
});
});
3 changes: 2 additions & 1 deletion test/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"allowImportingTsExtensions": true
"allowImportingTsExtensions": true,
"types": ["vitest/globals"]
},
"include": ["src/**/*"]
}
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 08acbc4

Please sign in to comment.