/
load-plugins.ts
368 lines (332 loc) · 9.66 KB
/
load-plugins.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
error: jest.fn(),
panic: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
success: jest.fn(),
info: jest.fn(),
}
})
const mockProcessExit = jest.spyOn(process, `exit`).mockImplementation(() => {})
import { loadPlugins } from "../index"
import { slash } from "gatsby-core-utils"
import reporter from "gatsby-cli/lib/reporter"
import { IFlattenedPlugin } from "../types"
afterEach(() => {
Object.keys(reporter).forEach(method => {
reporter[method].mockClear()
})
})
describe(`Load plugins`, () => {
/**
* Replace the resolve path and version string.
* Resolve path will vary depending on platform.
* Version can be updated (we use external plugin in default config).
* Both will cause snapshots to differ.
*/
const replaceFieldsThatCanVary = (
plugins: Array<IFlattenedPlugin>
): Array<IFlattenedPlugin> =>
plugins.map(plugin => {
if (plugin.pluginOptions && plugin.pluginOptions.path) {
plugin.pluginOptions = {
...plugin.pluginOptions,
path: plugin.pluginOptions.path.replace(
slash(process.cwd()),
`<PROJECT_ROOT>`
),
}
}
return {
...plugin,
id: ``,
resolve: ``,
version: `1.0.0`,
}
})
it(`Load plugins for a site`, async () => {
let plugins = await loadPlugins({ plugins: [] })
plugins = replaceFieldsThatCanVary(plugins)
expect(plugins).toMatchSnapshot()
})
it(`Loads plugins defined with an object but without an options key`, async () => {
const config = {
plugins: [
{
resolve: `___TEST___`,
},
],
}
let plugins = await loadPlugins(config)
plugins = replaceFieldsThatCanVary(plugins)
expect(plugins).toMatchSnapshot()
})
it(`Throws an error when a plugin is defined with an option key`, async () => {
expect.assertions(1)
const config = {
plugins: [
{
resolve: `___TEST___`,
option: {
test: true,
},
},
],
}
try {
await loadPlugins(config)
} catch (err) {
expect(err.message).toMatchSnapshot()
}
})
it(`Overrides the options for gatsby-plugin-page-creator`, async () => {
const config = {
plugins: [
{
resolve: `gatsby-plugin-page-creator`,
options: {
path: `${__dirname}/src/pages`,
ignore: [`___Test___.(js|ts)?(x)`],
},
},
],
}
let plugins = await loadPlugins(config)
plugins = replaceFieldsThatCanVary(plugins)
expect(plugins).toMatchSnapshot()
})
describe(`TypeScript support`, () => {
it(`loads gatsby-plugin-typescript if not provided`, async () => {
const config = {
plugins: [],
}
let plugins = await loadPlugins(config)
plugins = replaceFieldsThatCanVary(plugins)
expect(plugins).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: `gatsby-plugin-typescript`,
}),
])
)
})
it(`uses the user provided plugin-typescript if provided`, async () => {
const config = {
plugins: [
{
resolve: `gatsby-plugin-typescript`,
options: {
jsxPragma: `h`,
},
},
],
}
let plugins = await loadPlugins(config)
plugins = replaceFieldsThatCanVary(plugins)
expect(plugins).toEqual(
expect.arrayContaining([
expect.objectContaining({
browserAPIs: [],
id: ``,
name: `gatsby-plugin-typescript`,
nodeAPIs: [
`pluginOptionsSchema`,
`resolvableExtensions`,
`onCreateBabelConfig`,
`onCreateWebpackConfig`,
],
pluginOptions: {
allExtensions: false,
isTSX: false,
jsxPragma: `h`,
plugins: [],
},
resolve: ``,
ssrAPIs: [],
version: `1.0.0`,
}),
])
)
})
it(`does not add gatsby-plugin-typescript if it exists in config.plugins`, async () => {
const config = {
plugins: [
`gatsby-plugin-typescript`,
{ resolve: `gatsby-plugin-typescript` },
],
}
let plugins = await loadPlugins(config)
plugins = replaceFieldsThatCanVary(plugins)
const tsplugins = plugins.filter(
(plugin: { name: string }) => plugin.name === `gatsby-plugin-typescript`
)
// TODO: I think we should probably be de-duping, so this should be 1.
// But this test is mostly here to ensure we don't add an _additional_ gatsby-plugin-typescript
expect(tsplugins.length).toEqual(2)
})
})
describe(`plugin options validation`, () => {
it(`throws a structured error with invalid plugin options`, async () => {
const invalidPlugins = [
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: 123,
anonymize: `not a boolean`,
},
},
{
resolve: `gatsby-plugin-google-analytics`,
options: {
anonymize: `still not a boolean`,
},
},
]
await loadPlugins({
plugins: invalidPlugins,
})
expect(reporter.error as jest.Mock).toHaveBeenCalledTimes(
invalidPlugins.length
)
expect((reporter.error as jest.Mock).mock.calls[0])
.toMatchInlineSnapshot(`
Array [
Object {
"context": Object {
"configDir": null,
"pluginName": "gatsby-plugin-google-analytics",
"validationErrors": Array [
Object {
"context": Object {
"key": "trackingId",
"label": "trackingId",
"value": 123,
},
"message": "\\"trackingId\\" must be a string",
"path": Array [
"trackingId",
],
"type": "string.base",
},
Object {
"context": Object {
"key": "anonymize",
"label": "anonymize",
"value": "not a boolean",
},
"message": "\\"anonymize\\" must be a boolean",
"path": Array [
"anonymize",
],
"type": "boolean.base",
},
],
},
"id": "11331",
},
]
`)
expect((reporter.error as jest.Mock).mock.calls[1])
.toMatchInlineSnapshot(`
Array [
Object {
"context": Object {
"configDir": null,
"pluginName": "gatsby-plugin-google-analytics",
"validationErrors": Array [
Object {
"context": Object {
"key": "anonymize",
"label": "anonymize",
"value": "still not a boolean",
},
"message": "\\"anonymize\\" must be a boolean",
"path": Array [
"anonymize",
],
"type": "boolean.base",
},
],
},
"id": "11331",
},
]
`)
expect(mockProcessExit).toHaveBeenCalledWith(1)
})
it(`defaults plugin options to the ones defined in the schema`, async () => {
let plugins = await loadPlugins({
plugins: [
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: `fake`,
},
},
],
})
plugins = replaceFieldsThatCanVary(plugins)
expect(
plugins.find(plugin => plugin.name === `gatsby-plugin-google-analytics`)
.pluginOptions
).toEqual({
// All the options that have defaults are defined
anonymize: false,
exclude: [],
head: false,
pageTransitionDelay: 0,
plugins: [],
respectDNT: false,
trackingId: `fake`,
})
})
it(`validates subplugin schemas`, async () => {
await loadPlugins({
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-autolink-headers`,
options: {
maintainCase: `should be boolean`,
},
},
],
},
},
],
})
expect(reporter.error as jest.Mock).toHaveBeenCalledTimes(1)
expect((reporter.error as jest.Mock).mock.calls[0])
.toMatchInlineSnapshot(`
Array [
Object {
"context": Object {
"configDir": null,
"pluginName": "gatsby-remark-autolink-headers",
"validationErrors": Array [
Object {
"context": Object {
"key": "maintainCase",
"label": "maintainCase",
"value": "should be boolean",
},
"message": "\\"maintainCase\\" must be a boolean",
"path": Array [
"maintainCase",
],
"type": "boolean.base",
},
],
},
"id": "11331",
},
]
`)
expect(mockProcessExit).toHaveBeenCalledWith(1)
})
})
})