-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsr.test.ts
154 lines (146 loc) · 4.42 KB
/
jsr.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { expect, expectTypeOf, describe, it } from "vitest";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import dedent from "dedent";
import RE2 from "re2";
const repositoryRoot = dirname(dirname(__dirname));
const file = readFileSync(join(repositoryRoot, "deno", "jsr.json")).toString();
const config: string[][] = JSON.parse(file)?.customManagers?.map(
(manager: { matchStrings?: string[] }) => manager.matchStrings,
);
const regexps: RE2[][] = config.map((matchStrings: string[]) =>
matchStrings.map((re) => new RE2(re)),
);
describe("check configuration existing", () => {
it("should be array", () => {
expect(Array.isArray(config));
});
it("should be array of regexp", () => {
expectTypeOf(regexps).toEqualTypeOf<RE2[][]>();
});
});
describe("jsr for import map", () => {
const testCases = [
{
title: "should accept jsr specifier",
input: `{
"imports": {
"@luca/flag": "jsr:@luca/flag@^1.0.1"
}
}`,
currentValue: "1.0.1",
depName: "@luca/flag",
},
{
title: "should accept https://jsr.io",
input: `{
"imports": {
"@luca/flag": "https://jsr.io/@luca/flag/1.0.1/main.ts"
}
}`,
currentValue: "1.0.1",
depName: "@luca/flag",
},
] as const;
it.each(testCases)("$title", ({ input, currentValue, depName }) => {
const re = regexps[0].map((r) => new RegExp(r, "gm"));
const matches = re
.map((r) => Array.from(input.matchAll(r)).map((e) => e.groups))
.filter((match) => match.length !== 0)
.flat();
expect(matches.length).toBe(1);
expect(matches[0]?.currentValue).toBe(currentValue);
expect(matches[0]?.depName).toBe(depName);
});
});
describe("jsr for js file", () => {
const testCases = [
{
title: "should accept jsr specifier",
currentValue: "1.0.1",
depName: "@luca/flag",
},
{
title: "should accept https://jsr.io",
input: `import { printProgress } from "https://jsr.io/@luca/flag/1.0.1/main.ts";`,
currentValue: "1.0.1",
depName: "@luca/flag",
},
{
title: "should accept version pinning(^)",
input: `import { printProgress } from "jsr:@luca/flag@^1.0.1";`,
currentValue: "1.0.1",
depName: "@luca/flag",
},
{
title: "should accept version pinning(~)",
input: `import { printProgress } from "jsr:@luca/flag@~1.0.1";`,
currentValue: "1.0.1",
depName: "@luca/flag",
},
{
title: "should accept only major version",
input: `import { printProgress } from "jsr:@luca/flag@1";`,
currentValue: "1",
depName: "@luca/flag",
},
{
title: "should accept jsr specifier in //@deno-types",
currentValue: "1.0.1",
depName: "@luca/flag",
},
{
title: "version pinning(^) with //@deno-types",
input: `// @deno-types="jsr:@luca/flag@^1.0.1";`,
currentValue: "1.0.1",
depName: "@luca/flag",
},
{
title: "version pinning(~) with //@deno-types",
input: `// @deno-types="jsr:@luca/flag@~1.0.1";`,
currentValue: "1.0.1",
depName: "@luca/flag",
},
{
title: "only major version with //@deno-types",
input: `// @deno-types="jsr:@luca/flag@1";`,
currentValue: "1",
depName: "@luca/flag",
},
{
title: "should update in jsdoc",
input: dedent`
/**
* \`\`\`ts
* import { assertEquals } from "jsr:@std/[email protected]/equals";
*
* assertEquals(add(1, 2), 3);
* \`\`\`
*/
export function add(a: number, b: number) {
return a + b;
}
`,
currentValue: "1.0.6",
depName: "@std/assert",
},
{
title: "should accept jsr specifier in //@ts-types",
currentValue: "0.1.0",
depName: "@some/package",
},
] as const;
it.each(testCases)("$title", ({ input, currentValue, depName }) => {
const re = regexps[1].map((r) => new RegExp(r, "gm"));
const matches = re
.map((r) => Array.from(input.matchAll(r)).map((e) => e.groups))
.filter((match) => match.length !== 0)
.flat();
expect(matches.length).toBe(1);
expect(matches[0]?.currentValue).toBe(currentValue);
expect(matches[0]?.depName).toBe(depName);
});
});