Skip to content

Commit 26e2f11

Browse files
authored
test: add vitest and initial utils tests (#315)
1 parent 44d48a3 commit 26e2f11

File tree

5 files changed

+977
-6
lines changed

5 files changed

+977
-6
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"lint:format": "prettier --check --cache .",
1616
"lint:types": "pnpm -r --parallel run typecheck",
1717
"release": "tsx script/release.ts",
18-
"test": "tsx e2e/publish.test.mts"
18+
"test": "tsx e2e/publish.test.mts",
19+
"test:unit": "vitest run"
1920
},
2021
"keywords": [],
2122
"author": "",
@@ -35,6 +36,7 @@
3536
"tsx": "^4.10.5",
3637
"typescript": "^5.4.5",
3738
"uncrypto": "^0.1.3",
39+
"vitest": "^3.0.2",
3840
"wait-port": "^1.1.0"
3941
},
4042
"pnpm": {

packages/utils/index.test.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import {
2+
it,
3+
describe,
4+
expect,
5+
vi,
6+
beforeEach,
7+
afterEach,
8+
type MockInstance,
9+
} from "vitest";
10+
import type { PackageManifest } from "query-registry";
11+
import * as utils from "./index.js";
12+
13+
describe("utils", () => {
14+
describe("extractOwnerAndRepo", () => {
15+
it("is null for URLs with trailing characters", () => {
16+
expect(
17+
utils.extractOwnerAndRepo("https://github.com/org/repo.gitpewpew"),
18+
).toBeNull();
19+
});
20+
21+
it("is null for URLs with leading characters", () => {
22+
expect(
23+
utils.extractOwnerAndRepo("pewpewhttps://github.com/org/repo.git"),
24+
).toBeNull();
25+
});
26+
27+
it("returns org and repo for valid https URLs", () => {
28+
expect(
29+
utils.extractOwnerAndRepo("http://github.com/org/repo.git"),
30+
).toEqual(["org", "repo"]);
31+
});
32+
33+
it("returns org and repo for valid http URLs", () => {
34+
expect(
35+
utils.extractOwnerAndRepo("https://github.com/org/repo.git"),
36+
).toEqual(["org", "repo"]);
37+
});
38+
39+
it("returns org and repo for valid git+https URLs", () => {
40+
expect(
41+
utils.extractOwnerAndRepo("git+https://github.com/org/repo.git"),
42+
).toEqual(["org", "repo"]);
43+
});
44+
45+
it("returns org and repo for valid git+http URLs", () => {
46+
expect(
47+
utils.extractOwnerAndRepo("git+http://github.com/org/repo.git"),
48+
).toEqual(["org", "repo"]);
49+
});
50+
});
51+
52+
describe("extractRepository", () => {
53+
it("returns undefined if no repository", () => {
54+
expect(utils.extractRepository({} as PackageManifest)).toBeUndefined();
55+
});
56+
57+
it("returns undefined if repository is object with no URL", () => {
58+
expect(
59+
utils.extractRepository({
60+
repository: {},
61+
} as PackageManifest),
62+
).toBeUndefined();
63+
});
64+
65+
it("returns URL if repository is string", () => {
66+
expect(
67+
utils.extractRepository({
68+
repository: "foo",
69+
} as PackageManifest),
70+
).toBe("foo");
71+
});
72+
73+
it("returns URL if repository is object with URL", () => {
74+
expect(
75+
utils.extractRepository({
76+
repository: {
77+
url: "foo",
78+
},
79+
} as PackageManifest),
80+
).toBe("foo");
81+
});
82+
});
83+
84+
describe("abbreviateCommitHash", () => {
85+
it("returns the first 7 characters of a hash", () => {
86+
expect(
87+
utils.abbreviateCommitHash("09efd0553374ff7d3e62b79378e3184f5eb57571"),
88+
).toBe("09efd05");
89+
});
90+
});
91+
92+
describe("isPullRequest", () => {
93+
it("returns true if ref is non-nan number", () => {
94+
expect(utils.isPullRequest("808")).toBe(true);
95+
});
96+
97+
it("returns false if ref is nan number", () => {
98+
expect(utils.isPullRequest("foo")).toBe(false);
99+
});
100+
});
101+
102+
describe("isWhitelisted", () => {
103+
let fetchSpy: MockInstance;
104+
let whitelist: string;
105+
106+
beforeEach(() => {
107+
whitelist = "";
108+
fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(() => {
109+
return Promise.resolve(new Response(whitelist, { status: 200 }));
110+
});
111+
});
112+
113+
afterEach(() => {
114+
vi.resetAllMocks();
115+
});
116+
117+
it("should return true if repo is in whitelist", async () => {
118+
whitelist = `
119+
foo/bar
120+
org/repo
121+
baz/zab
122+
`;
123+
const result = await utils.isWhitelisted("org", "repo");
124+
expect(result).toBe(true);
125+
});
126+
127+
it("should return false if repo is not in whitelist", async () => {
128+
const result = await utils.isWhitelisted("org", "repo");
129+
expect(result).toBe(false);
130+
});
131+
132+
it("should return false if fetch fails", async () => {
133+
fetchSpy.mockRejectedValue(new Error("bleep bloop"));
134+
const result = await utils.isWhitelisted("org", "repo");
135+
expect(result).toBe(false);
136+
});
137+
});
138+
});

packages/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PackageManifest } from "query-registry";
22

33
const githubUrlRegex =
4-
/(?:git\+)?https?:\/\/github\.com\/([^\/]+)\/([^\/]+)\.git/; // TODO: Don't trust this, it's chatgbd :)
4+
/^(?:git\+)?https?:\/\/github\.com\/([^\/]+)\/([^\/]+)\.git$/;
55

66
export function extractOwnerAndRepo(
77
repositoryUrl: string,

0 commit comments

Comments
 (0)