Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update plugin schema testing util and associated tests #27574

Merged
merged 11 commits into from
Oct 21, 2020
Prev Previous commit
Next Next commit
change plugin schema testing function to use validation function
  • Loading branch information
Laurie committed Oct 20, 2020
commit fe29c34bba3fec636958d6763278b9dd4d5f5051
8 changes: 4 additions & 4 deletions packages/gatsby-plugin-mdx/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils"
import { pluginOptionsSchema } from "../gatsby-node"

describe(`pluginOptionsSchema`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"extensions" "[0]" must be a string. "[1]" must be a string. "[2]" must be a string`,
`"defaultLayout" must be of type object`,
@@ -13,7 +13,7 @@ describe(`pluginOptionsSchema`, () => {
`"shouldBlockNodeFromTransformation" must have an arity lesser or equal to 1`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
extensions: [1, 2, 3],
defaultLayout: `this should be an object`,
gatsbyRemarkPlugins: [1, { not: `existing prop` }, `valid one`],
@@ -26,8 +26,8 @@ describe(`pluginOptionsSchema`, () => {
expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
extensions: [`.mdx`, `.mdxx`],
defaultLayout: {
posts: `../post-layout.js`,
8 changes: 4 additions & 4 deletions packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils"
import { pluginOptionsSchema } from "../gatsby-node"

describe(`gatsby-node.js`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"headers" must be of type object`,
`"allPageHeaders" must be an array`,
@@ -13,7 +13,7 @@ describe(`gatsby-node.js`, () => {
`"generateMatchPathRewrites" must be a boolean`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
headers: `this should be an object`,
allPageHeaders: `this should be an array`,
mergeSecurityHeaders: `this should be a boolean`,
@@ -26,8 +26,8 @@ describe(`gatsby-node.js`, () => {
expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
headers: {
"/some-page": [`Bearer: Some-Magic-Token`],
"/some-other-page": [`some`, `great`, `headers`],
8 changes: 4 additions & 4 deletions packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -111,15 +111,15 @@ describe(`onPostBuild`, () => {
})

describe(`pluginOptionsSchema`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"precachePages" "[0]" must be a string. "[1]" must be a string. "[2]" must be a string`,
`"appendScript" must be a string`,
`"debug" must be a boolean`,
`"workboxConfig" "importWorkboxFrom" must be a string. "globDirectory" must be a string. "globPatterns[0]" must be a string. "globPatterns[1]" must be a string. "globPatterns[2]" must be a string. "modifyURLPrefix./" must be a string. "cacheId" must be a string. "dontCacheBustURLsMatching" must be of type object. "runtimeCaching[0].handler" must be one of [StaleWhileRevalidate, CacheFirst, NetworkFirst, NetworkOnly, CacheOnly]. "runtimeCaching[1]" must be of type object. "runtimeCaching[2]" must be of type object. "skipWaiting" must be a boolean. "clientsClaim" must be a boolean`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
precachePages: [1, 2, 3],
appendScript: 1223,
debug: `This should be a boolean`,
@@ -148,8 +148,8 @@ describe(`pluginOptionsSchema`, () => {
expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
precachePages: [`/about-us/`, `/projects/*`],
appendScript: `src/custom-sw-code.js`,
debug: true,
8 changes: 4 additions & 4 deletions packages/gatsby-plugin-sass/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ describe(`gatsby-plugin-sass`, () => {
})

describe(`pluginOptionsSchema`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"implementation" must be of type object`,
`"postCssPlugins" must be an array`,
@@ -85,7 +85,7 @@ describe(`pluginOptionsSchema`, () => {
`"sourceMapRoot" must be a string`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
implementation: `This should be a require() thing`,
postCssPlugins: `This should be an array of postCss plugins`,
sassRuleTest: `This should be a regexp`,
@@ -114,8 +114,8 @@ describe(`pluginOptionsSchema`, () => {
expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
implementation: require(`../gatsby-node.js`),
postCssPlugins: [{ post: `CSS plugin` }],
sassRuleTest: /\.global\.s(a|c)ss$/,
14 changes: 7 additions & 7 deletions packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -74,14 +74,14 @@ describe(`gatsby-plugin-typescript`, () => {
})

describe(`plugin schema`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"isTSX" must be a boolean`,
`"jsxPragma" must be a string`,
`"allExtensions" must be a boolean`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
isTSX: `this should be a boolean`,
jsxPragma: 123,
allExtensions: `this should be a boolean`,
@@ -90,8 +90,8 @@ describe(`gatsby-plugin-typescript`, () => {
expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
isTSX: false,
jsxPragma: `ReactFunction`,
allExtensions: false,
@@ -100,14 +100,14 @@ describe(`gatsby-plugin-typescript`, () => {
expect(isValid).toBe(true)
})

it(`should break when isTSX doesn't match allExtensions`, () => {
const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
it(`should break when isTSX doesn't match allExtensions`, async () => {
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
isTSX: true,
jsxPragma: `ReactFunction`,
allExtensions: false,
})

expect(errors).toEqual(`"allExtensions" must be [true]`)
expect(errors).toEqual([`"allExtensions" must be [true]`])
})
})
})
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-utils/README.md
Original file line number Diff line number Diff line change
@@ -28,15 +28,15 @@ Utility to validate and test plugin options schemas. An example of a plugin opti
// This is an example using Jest (https://jestjs.io/)
import { testPluginOptionsSchema } from "gatsby-plugin-utils"

it(`should partially validate one value of a schema`, () => {
it(`should partially validate one value of a schema`, async () => {
const pluginSchema = ({ Joi }) =>
Joi.object({
someOtherValue: Joi.string()
toVerify: Joi.boolean(),
})

// Only the "toVerify" key of the schema will be verified in this test
const { isValid, errors } = testPluginOptionsSchema(pluginSchema, {
const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, {
toVerify: `abcd`,
})

Original file line number Diff line number Diff line change
@@ -2,15 +2,15 @@ import { testPluginOptionsSchema } from "../test-plugin-options-schema"
import { ObjectSchema } from "../joi"

describe(`testPluginOptionsSchema`, () => {
it(`should partially validate one value of a schema`, () => {
it(`should partially validate one value of a schema`, async () => {
const pluginSchema = ({ Joi }): ObjectSchema =>
Joi.object({
str: Joi.string(),
nb: Joi.number(),
toVerify: Joi.boolean(),
})

const { isValid, errors } = testPluginOptionsSchema(pluginSchema, {
const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, {
toVerify: `abcd`,
})

@@ -22,15 +22,15 @@ describe(`testPluginOptionsSchema`, () => {
`)
})

it(`should partially validate multiples value of a schema`, () => {
it(`should partially validate multiples value of a schema`, async () => {
const pluginSchema = ({ Joi }): ObjectSchema =>
Joi.object({
str: Joi.string(),
nb: Joi.number(),
toVerify: Joi.boolean(),
})

const { isValid, errors } = testPluginOptionsSchema(pluginSchema, {
const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, {
toVerify: `abcd`,
nb: `invalid value`,
})
@@ -44,7 +44,7 @@ describe(`testPluginOptionsSchema`, () => {
`)
})

it(`should validate half of a real world plugin schema`, () => {
it(`should validate half of a real world plugin schema`, async () => {
const pluginSchema = ({ Joi }): ObjectSchema =>
Joi.object({
trackingId: Joi.string()
@@ -85,7 +85,7 @@ describe(`testPluginOptionsSchema`, () => {
cookieDomain: Joi.string(),
})

const { isValid, errors } = testPluginOptionsSchema(pluginSchema, {
const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, {
trackingId: undefined,
head: `invalid boolean value`,
anonymize: `invalid boolean value`,
@@ -105,7 +105,7 @@ describe(`testPluginOptionsSchema`, () => {
`)
})

it(`should validate an entire real world plugin schema`, () => {
it(`should validate an entire real world plugin schema`, async () => {
const pluginSchema = ({ Joi }): ObjectSchema =>
Joi.object({
trackingId: Joi.string()
@@ -146,7 +146,7 @@ describe(`testPluginOptionsSchema`, () => {
cookieDomain: Joi.string(),
})

const { isValid, errors } = testPluginOptionsSchema(pluginSchema, {
const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, {
trackingId: undefined,
head: `invalid boolean value`,
anonymize: `invalid boolean value`,
@@ -182,13 +182,13 @@ describe(`testPluginOptionsSchema`, () => {
`)
})

it(`should check the validity of a schema`, () => {
it(`should check the validity of a schema`, async () => {
const pluginSchema = ({ Joi }): ObjectSchema =>
Joi.object({
toVerify: Joi.boolean(),
})

const { isValid, errors } = testPluginOptionsSchema(pluginSchema, {
const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, {
toVerify: false,
})

34 changes: 10 additions & 24 deletions packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import { Joi } from "./joi"
import { GatsbyNode } from "gatsby"
import { validateOptionsSchema } from "./validate"

interface ITestPluginOptionsSchemaReturnType {
errors: Array<string>
isValid: boolean
}

export function testPluginOptionsSchema<PluginOptions = object>(
export async function testPluginOptionsSchema<PluginOptions = object>(
pluginSchemaFunction: Exclude<GatsbyNode["pluginOptionsSchema"], undefined>,
pluginOptions: PluginOptions
): ITestPluginOptionsSchemaReturnType {
const pluginOptionsNames = Object.keys(pluginOptions)
): Promise<ITestPluginOptionsSchemaReturnType> {
const pluginSchema = pluginSchemaFunction({ Joi })
const errors: Array<string> = []

pluginOptionsNames.forEach(pluginOptionName => {
const partialSchema = pluginSchema.extract(pluginOptionName)
console.log(pluginOptions[pluginOptionName])
const { error } = partialSchema.validate(pluginOptions[pluginOptionName], {
abortEarly: false,
})
try {
await validateOptionsSchema(pluginSchema, pluginOptions)
} catch (e) {
const errors = e.details.map(detail => detail.message)
return { isValid: false, errors }
}

if (error) {
const errorMessage = error.message

// In the case of an array, "value" does not exist in the error message
// and so we can't replace it with the plugin option name, we have to concat it
const message = errorMessage.includes(`"value"`)
? errorMessage.replace(`"value"`, `"${pluginOptionName}"`)
: `"${pluginOptionName}" ${errorMessage}`

errors.push(message)
}
})

return { isValid: errors.length === 0, errors }
return { isValid: true, errors: [] }
}
8 changes: 4 additions & 4 deletions packages/gatsby-remark-images/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils"
import { pluginOptionsSchema } from "../gatsby-node"

describe(`pluginOptionsSchema`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"maxWidth" must be a number`,
`"linkImagesToOriginal" must be a boolean`,
@@ -20,7 +20,7 @@ describe(`pluginOptionsSchema`, () => {
`"srcSetBreakpoints" must be an array`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
maxWidth: `This should be a number`,
linkImagesToOriginal: `This should be a boolean`,
showCaptions: `This should be a boolean`,
@@ -40,8 +40,8 @@ describe(`pluginOptionsSchema`, () => {
expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
maxWidth: 700,
linkImagesToOriginal: false,
showCaptions: true,
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils"
import { pluginOptionsSchema } from "../gatsby-node"

describe(`gatsby-node.js`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"commonmark" must be a boolean`,
`"footnotes" must be a boolean`,
@@ -11,7 +11,7 @@ describe(`gatsby-node.js`, () => {
`"plugins" must be an array`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
commonmark: `this should be a boolean`,
footnotes: `this should be a boolean`,
pedantic: `this should be a boolean`,
@@ -22,8 +22,8 @@ describe(`gatsby-node.js`, () => {
expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
commonmark: false,
footnotes: false,
pedantic: false,
You are viewing a condensed version of this merge commit. You can view the full changes here.