-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby): validate local plugin options schema (#29787)
Co-authored-by: gatsbybot <[email protected]> Co-authored-by: LekoArts <[email protected]>
- Loading branch information
1 parent
54d4721
commit 096eb38
Showing
14 changed files
with
267 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# Structured Logging | ||
|
||
Tests to ensure a couple of functionalities from our structured logging, including (local) plugins. | ||
|
||
- Verifies IPC, logs, panic, status | ||
- Verifies plugin errors with an errorMap | ||
- Verifies plugin options. The tests will verify local plugin options schema validation by dropping file markers into the build folder with a flag that flips if the `pluginOptionsSchema` export is invoked. | ||
|
||
## Problems | ||
|
||
- Concurrent tests will need to generate a unique build folder name |
142 changes: 142 additions & 0 deletions
142
integration-tests/structured-logging/__tests__/validate-options.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
const { spawn } = require(`child_process`) | ||
const path = require(`path`) | ||
|
||
jest.setTimeout(100000) | ||
|
||
const gatsbyBin = path.join( | ||
`node_modules`, | ||
`gatsby`, | ||
`dist`, | ||
`bin`, | ||
`gatsby.js` | ||
) | ||
|
||
describe(`Validate Plugin Options`, () => { | ||
let gatsbyProcess | ||
let events = [] | ||
|
||
beforeEach(async () => { | ||
gatsbyProcess = spawn(process.execPath, [gatsbyBin, `build`], { | ||
// inherit lets us see logs in console | ||
// stdio: [`inherit`, `inherit`, `inherit`, `ipc`], | ||
stdio: [`ignore`, `ignore`, `ignore`, `ipc`], | ||
env: { | ||
...process.env, | ||
NODE_ENV: `production`, | ||
ENABLE_GATSBY_REFRESH_ENDPOINT: true, | ||
VALIDATE_PLUGIN_OPTIONS: true, | ||
}, | ||
}) | ||
|
||
await new Promise(resolve => { | ||
gatsbyProcess.on(`message`, msg => { | ||
events.push(msg) | ||
}) | ||
|
||
gatsbyProcess.on(`exit`, exitCode => { | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
|
||
it(`Errors on local plugins`, () => { | ||
expect(events).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `SET_STATUS`, | ||
payload: `FAILED`, | ||
}), | ||
}), | ||
]) | ||
) | ||
|
||
expect(events).toEqual( | ||
expect.arrayContaining([ | ||
// Local plugin with require.resolve | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `LOG`, | ||
payload: expect.objectContaining({ | ||
level: `ERROR`, | ||
category: `USER`, | ||
context: expect.objectContaining({ | ||
pluginName: expect.stringContaining("integration-tests/structured-logging/local-plugin-with-path/index.js"), | ||
validationErrors: expect.arrayContaining([ | ||
{ | ||
context: { | ||
key: "required", | ||
label: "required" | ||
}, | ||
message: "\"required\" is required", | ||
path: [ | ||
"required" | ||
], | ||
type: "any.required" | ||
}, | ||
{ | ||
context: { | ||
key: "optionalString", | ||
label: "optionalString", | ||
value: 1234 | ||
}, | ||
message: "\"optionalString\" must be a string", | ||
path: [ | ||
"optionalString" | ||
], | ||
type: "string.base" | ||
} | ||
]) | ||
}), | ||
code: `11331`, | ||
type: `PLUGIN`, | ||
}) | ||
}) | ||
}), | ||
// Local plugin with name in gatsby-config | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `LOG`, | ||
payload: expect.objectContaining({ | ||
level: `ERROR`, | ||
category: `USER`, | ||
context: expect.objectContaining({ | ||
pluginName: "local-plugin", | ||
validationErrors: expect.arrayContaining([ | ||
{ | ||
context: { | ||
key: "required", | ||
label: "required" | ||
}, | ||
message: "\"required\" is required", | ||
path: [ | ||
"required" | ||
], | ||
type: "any.required" | ||
}, | ||
{ | ||
context: { | ||
key: "optionalString", | ||
label: "optionalString", | ||
value: 1234 | ||
}, | ||
message: "\"optionalString\" must be a string", | ||
path: [ | ||
"optionalString" | ||
], | ||
type: "string.base" | ||
} | ||
]) | ||
}), | ||
code: `11331`, | ||
type: `PLUGIN`, | ||
}) | ||
}) | ||
}) | ||
]) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
const dynamicPlugins = [] | ||
|
||
if (process.env.VALIDATE_PLUGIN_OPTIONS) { | ||
dynamicPlugins.push( | ||
{ | ||
resolve: "local-plugin", | ||
options: { | ||
optionalString: 1234, | ||
}, | ||
}, | ||
{ | ||
resolve: require.resolve("./local-plugin-with-path"), | ||
options: { | ||
optionalString: 1234, | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
module.exports = { | ||
plugins: ["structured-plugin-errors"], | ||
plugins: [ | ||
"structured-plugin-errors", | ||
...dynamicPlugins | ||
], | ||
} |
6 changes: 6 additions & 0 deletions
6
integration-tests/structured-logging/local-plugin-with-path/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
exports.pluginOptionsSchema = ({ Joi }) => { | ||
return Joi.object({ | ||
required: Joi.boolean().required(), | ||
optionalString: Joi.string(), | ||
}) | ||
} |
1 change: 1 addition & 0 deletions
1
integration-tests/structured-logging/local-plugin-with-path/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// no-op |
6 changes: 6 additions & 0 deletions
6
integration-tests/structured-logging/local-plugin-with-path/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "local-plugin-with-path", | ||
"private": true, | ||
"version": "0.1.0", | ||
"main": "index.js" | ||
} |
6 changes: 6 additions & 0 deletions
6
integration-tests/structured-logging/plugins/local-plugin/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
exports.pluginOptionsSchema = ({ Joi }) => { | ||
return Joi.object({ | ||
required: Joi.boolean().required(), | ||
optionalString: Joi.string(), | ||
}) | ||
} |
1 change: 1 addition & 0 deletions
1
integration-tests/structured-logging/plugins/local-plugin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// no-op |
6 changes: 6 additions & 0 deletions
6
integration-tests/structured-logging/plugins/local-plugin/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "local-plugin", | ||
"private": true, | ||
"version": "0.1.0", | ||
"main": "index.js" | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/local-plugin/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
exports.pluginOptionsSchema = ({ Joi }) => Joi.object({ | ||
required: Joi.boolean().required(), | ||
optionalString: Joi.string() | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/local-plugin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// no-op |
6 changes: 6 additions & 0 deletions
6
packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/local-plugin/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "local-plugin", | ||
"private": true, | ||
"version": "0.1.0", | ||
"main": "index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters