-
Notifications
You must be signed in to change notification settings - Fork 201
/
load-plugin.test.ts
203 lines (179 loc) · 4.74 KB
/
load-plugin.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import path from "path";
import endent from "endent";
import { execSync } from "child_process";
import { getInstalledPlugins, loadPlugin, listPlugins } from "../load-plugins";
import { dummyLog, setLogLevel } from "../logger";
const logger = dummyLog();
jest.mock(
"auto-plugin-foo",
() => ({
default: class {
name = "foo";
},
}),
{
virtual: true,
}
);
jest.mock(
"@my-scope/auto-plugin-bar",
() => ({
default: class {
name = "bar";
},
}),
{ virtual: true }
);
jest.mock(
"@auto-it/baz",
() => ({
default: class {
name = "baz";
},
}),
{
virtual: true,
}
);
describe("loadPlugins", () => {
beforeEach(() => {
setLogLevel("quiet");
});
test("should load official plugins", () => {
expect(loadPlugin(["baz", {}], logger)?.name).toBe("baz");
expect(loadPlugin(["@auto-it/baz", {}], logger)?.name).toBe("baz");
});
test("should load community plugins", () => {
expect(loadPlugin(["foo", {}], logger)?.name).toBe("foo");
expect(loadPlugin(["auto-plugin-foo", {}], logger)?.name).toBe("foo");
});
test("should load plugin stored relative to extended config package.json", () => {
expect(
loadPlugin(
["./some-plugin.js", {}],
logger,
path.join(__dirname, "../test-config/package.json")
)?.name
).toBe("test-1");
});
test("should load plugin stored relative to extended config dir", () => {
expect(
loadPlugin(
["./some-other-plugin.js", {}],
logger,
path.join(__dirname, "../test-config")
)?.name
).toBe("test-2");
});
test("should load scoped plugins", () => {
expect(loadPlugin(["@my-scope/auto-plugin-bar", {}], logger)?.name).toBe(
"bar"
);
});
test("should require custom plugins -- fallback to cwd", () => {
expect(
loadPlugin([path.join(__dirname, "./test-plugin.ts"), {}], logger)
).toStrictEqual(
expect.objectContaining({
name: "foo",
config: {},
})
);
});
test("should require custom plugins -- surface errors", () => {
expect(() =>
loadPlugin(
[path.join(__dirname, "./test-plugin-malformed.js"), {}],
logger
)
).toThrow();
});
test("should load config", () => {
expect(
loadPlugin(
[path.join(__dirname, "./test-plugin.ts"), "do the thing"],
logger
)
).toStrictEqual(
expect.objectContaining({
name: "foo",
config: "do the thing",
})
);
});
});
const exec = jest.fn();
jest.mock("child_process");
// @ts-ignore
execSync.mockImplementation(exec);
describe("getInstalledPlugins", () => {
test("should find local modules", () => {
exec.mockReturnValueOnce(
"/some/folder/node_modules/@auto-it/npm\n/some/folder/node_modules/@auto-it/released"
);
expect(getInstalledPlugins()).toStrictEqual([
{
name: "npm",
path: "/some/folder/node_modules/@auto-it/npm",
},
{
name: "released",
path: "/some/folder/node_modules/@auto-it/released",
},
]);
});
test("should exclude non plugins", () => {
exec.mockReturnValueOnce(endent`
/some/folder/node_modules/@auto-it/npm
/some/folder/node_modules/@auto-it/core
/some/folder/node_modules/autoprefixer
`);
expect(getInstalledPlugins()).toStrictEqual([
{
name: "npm",
path: "/some/folder/node_modules/@auto-it/npm",
},
]);
});
test("should find modules even with errors", () => {
exec.mockImplementationOnce(() => {
const error = new Error();
// @ts-ignore
error.stdout =
"/some/folder/node_modules/@auto-it/gradle\n/some/folder/node_modules/@auto-it/chrome";
throw error;
});
expect(getInstalledPlugins(true)).toStrictEqual([
{
name: "gradle",
path: "/some/folder/node_modules/@auto-it/gradle",
},
{
name: "chrome",
path: "/some/folder/node_modules/@auto-it/chrome",
},
]);
});
});
const log = jest.fn();
console.log = log;
describe("listPlugins", () => {
beforeEach(() => {
log.mockClear();
});
test("should get plugins from autorc", async () => {
exec.mockReturnValue("");
await listPlugins({ plugins: ["npm"] } as any, dummyLog());
expect(log.mock.calls[0]).toMatchSnapshot();
});
test("should get plugins from local module", async () => {
process.cwd = () => "/home";
exec.mockReturnValue(
"/some/folder/node_modules/@auto-it/npm\n/some/folder/node_modules/@auto-it/released"
);
await listPlugins({ plugins: ["npm"] } as any, dummyLog());
expect(log.mock.calls[0]).toMatchSnapshot();
expect(log.mock.calls[2]).toMatchSnapshot();
expect(log.mock.calls[4]).toMatchSnapshot();
});
});