Skip to content

Commit b6f2753

Browse files
committed
test: add vitest and initial utils tests
This adds vitest to the monorepo and uses workspaces to run it from there. It also adds an initial set of simple tests to the utils package.
1 parent 44d48a3 commit b6f2753

File tree

5 files changed

+945
-6
lines changed

5 files changed

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

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@github.com:|https?:\/\/github.com\/)([^\/]+)\/([^\/]+)\.git$/;
55

66
export function extractOwnerAndRepo(
77
repositoryUrl: string,

0 commit comments

Comments
 (0)