-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicenses.test.ts
80 lines (65 loc) · 2.22 KB
/
licenses.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
import { init } from "license-checker-rseidelsohn";
import { promisify } from "util";
const checkLicenses = promisify(init);
type Info = { name: string; licenses: string[] };
let packages: Array<Info>;
beforeAll(async () => {
const rawResult = await checkLicenses({
// start at the root of our project
start: ".",
// we don't need to check our own license
excludePackages: "blog-license-check-demo",
// There's a bug in license-checker-rseidelsohn
// => https://github.com/RSeidelsohn/license-checker-rseidelsohn/issues/35
// 🚨 this enables only checking direct dependencies!
direct: 0 as any,
});
packages = Object.entries(rawResult).map(([rawName, result]) => {
// e.g. @foo/[email protected] => ["", "foo/bar", "1.2.3"]
const parts = rawName.split("@");
const name = parts.slice(0, -1).join("@"); // @foo/bar
// license-checker supports multiple licenses per package:
const licenses = result.licenses ?? ["UNLICENSED"];
return {
name,
// license-checker supports multiple licenses per package:
licenses: Array.isArray(licenses) ? licenses : [licenses],
};
});
// License compilation might take a bit, so we don't want to run into Jest's 5s timeout
}, 30000);
const LICENSE_ALLOW_LIST = ["UNLICENSED", "MIT", "BSD-3-Clause", "Apache-2.0"];
expect.extend({
// using a custom matcher for better output when a test fails
toBeValidLicense: ({ licenses, name }: Info) => {
// 🚨 this is the important part:
// pass this test, when every license of the package is in the allow list
const pass = licenses.every(
(license) => license && LICENSE_ALLOW_LIST.includes(license)
);
return {
pass,
// display this message with enough info
message: () =>
`"${licenses.join(",")}" is ${
pass ? "" : "not "
}an allowed license (dependency: ${name})`,
};
},
});
// This could also go in a .d.ts file, I'll keep it here for simplicity
declare global {
namespace jest {
interface Matchers<R> {
toBeValidLicense(): R;
}
}
}
test("all licenses are allowed", async () => {
packages.forEach((info) => expect(info).toBeValidLicense());
});
test("snapshot package licenses", async () => {
expect(
packages.map(({ licenses, name }) => `${name}: ${licenses}`)
).toMatchSnapshot();
});