From 2d967cbf3be81bb036f1f1cbc108a5e36c49785c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Wed, 23 Nov 2022 16:01:13 +0100 Subject: [PATCH] tests: Update pluginOptionsSchema tests (#27904) Co-authored-by: LekoArts --- .../configuring-usage-with-plugin-options.md | 12 +- .../src/__tests__/gatsby-node.js | 12 +- .../src/__tests__/gatsby-node.js | 54 +++-- .../src/__tests__/gatsby-node.js | 3 +- .../src/__tests__/gatsby-node.js | 128 +++++----- .../src/__tests__/gatsby-node.js | 132 +++++----- .../src/__tests__/plugin-options.ts | 20 +- .../src/__tests__/gatsby-node.js | 3 +- .../src/__tests__/gatsby-node.js | 52 ++-- packages/gatsby-plugin-utils/README.md | 3 +- .../__tests__/test-plugin-options-schema.ts | 76 +++--- .../src/__tests__/gatsby-node.js | 24 +- .../src/__tests__/gatsby-node.js | 229 +++++++++++------- .../__tests__/plugin-options-schema.test.js | 13 +- .../src/__tests__/gatsby-node.js | 42 ++-- 15 files changed, 448 insertions(+), 355 deletions(-) diff --git a/docs/docs/how-to/plugins-and-themes/configuring-usage-with-plugin-options.md b/docs/docs/how-to/plugins-and-themes/configuring-usage-with-plugin-options.md index af67181ce09e1..0dd766bd421ac 100644 --- a/docs/docs/how-to/plugins-and-themes/configuring-usage-with-plugin-options.md +++ b/docs/docs/how-to/plugins-and-themes/configuring-usage-with-plugin-options.md @@ -249,17 +249,19 @@ describe(`pluginOptionsSchema`, () => { message: 123, // Should be a string optionB: `not a boolean`, // Should be a boolean } + const expectedErrors = [ + `"optionA" is required`, + `"message" must be a string`, + `"optionB" must be a boolean`, + ] + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, options ) expect(isValid).toBe(false) - expect(errors).toEqual([ - `"optionA" is required`, - `"message" must be a string`, - `"optionB" must be a boolean`, - ]) + expect(errors).toEqual(expectedErrors) }) it(`should validate correct options`, async () => { diff --git a/packages/gatsby-plugin-cxs/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-cxs/src/__tests__/gatsby-node.js index e04d2ef2d5b19..d6757796d9a5e 100644 --- a/packages/gatsby-plugin-cxs/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-cxs/src/__tests__/gatsby-node.js @@ -5,15 +5,14 @@ import { pluginOptionsSchema } from "../gatsby-node" it(`should provide meaningful errors when fields are invalid`, async () => { const expectedWarnings = [`"optionA" is not allowed`] - const { warnings, isValid, hasWarnings } = await testPluginOptionsSchema( - pluginOptionsSchema, - { + const { warnings, isValid, hasWarnings, errors } = + await testPluginOptionsSchema(pluginOptionsSchema, { optionA: `This options shouldn't exist`, - } - ) + }) expect(isValid).toBe(true) expect(hasWarnings).toBe(true) expect(warnings).toEqual(expectedWarnings) + expect(errors).toEqual([]) }) it.each` @@ -21,10 +20,11 @@ it.each` ${undefined} ${{}} `(`should validate the schema: $options`, async ({ options }) => { - const { isValid } = await testPluginOptionsSchema( + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, options ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) diff --git a/packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js index e89a60fe7011e..42f7247148619 100644 --- a/packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js @@ -3,35 +3,43 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils" describe(`pluginOptionsSchema`, () => { it(`should validate valid options`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - id: `YOUR_GOOGLE_TAGMANAGER_ID`, - includeInDevelopment: false, - defaultDataLayer: { platform: `gatsby` }, - gtmAuth: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING`, - gtmPreview: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME`, - dataLayerName: `YOUR_DATA_LAYER_NAME`, - routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`, - enableWebVitalsTracking: true, - selfHostedOrigin: `YOUR_SELF_HOSTED_ORIGIN`, - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + id: `YOUR_GOOGLE_TAGMANAGER_ID`, + includeInDevelopment: false, + defaultDataLayer: { platform: `gatsby` }, + gtmAuth: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING`, + gtmPreview: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME`, + dataLayerName: `YOUR_DATA_LAYER_NAME`, + routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`, + enableWebVitalsTracking: true, + selfHostedOrigin: `YOUR_SELF_HOSTED_ORIGIN`, + } + ) expect(isValid).toEqual(true) + expect(errors).toEqual([]) }) it(`should support defaultDataLayer as a function`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - defaultDataLayer: () => { - return { - originalLocation: - document.location.protocol + - `//` + - document.location.hostname + - document.location.pathname + - document.location.search, - } - }, - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + defaultDataLayer: () => { + return { + originalLocation: + document.location.protocol + + `//` + + document.location.hostname + + document.location.pathname + + document.location.search, + } + }, + } + ) expect(isValid).toEqual(true) + expect(errors).toEqual([]) }) }) diff --git a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js index b6591d4cec3c4..fe0a9484fd457 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js @@ -527,11 +527,12 @@ describe(`Test plugin manifest options`, () => { describe(`pluginOptionsSchema`, () => { it(`validates options correctly`, async () => { - const { isValid } = await testPluginOptionsSchema( + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, manifestOptions ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) }) diff --git a/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js index 4d6c02c0a37d0..b264416d5b07e 100644 --- a/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js @@ -133,74 +133,82 @@ describe(`pluginOptionsSchema`, () => { `"workboxConfig.clientsClaim" must be a boolean`, ] - const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, { - precachePages: [1, 2, 3], - appendScript: 1223, - debug: `This should be a boolean`, - workboxConfig: { - importWorkboxFrom: 123, - globDirectory: 456, - globPatterns: [1, 2, 3], - modifyURLPrefix: { - "/": 123, - }, - cacheId: 123, - dontCacheBustURLsMatching: `This should be a regexp`, - runtimeCaching: [ - { - urlPattern: /(\.js$|\.css$|static\/)/, - handler: `Something Invalid`, + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + precachePages: [1, 2, 3], + appendScript: 1223, + debug: `This should be a boolean`, + workboxConfig: { + importWorkboxFrom: 123, + globDirectory: 456, + globPatterns: [1, 2, 3], + modifyURLPrefix: { + "/": 123, }, - 2, - 3, - ], - skipWaiting: `This should be a boolean`, - clientsClaim: `This should be a boolean`, - }, - }) + cacheId: 123, + dontCacheBustURLsMatching: `This should be a regexp`, + runtimeCaching: [ + { + urlPattern: /(\.js$|\.css$|static\/)/, + handler: `Something Invalid`, + }, + 2, + 3, + ], + skipWaiting: `This should be a boolean`, + clientsClaim: `This should be a boolean`, + }, + } + ) + expect(isValid).toBe(false) expect(errors).toEqual(expectedErrors) }) it(`should validate the schema`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - precachePages: [`/about-us/`, `/projects/*`], - appendScript: `src/custom-sw-code.js`, - debug: true, - workboxConfig: { - importWorkboxFrom: `local`, - globDirectory: `rootDir`, - globPatterns: [`a`, `b`, `c`], - modifyURLPrefix: { - "/": `pathPrefix/`, - }, - cacheId: `gatsby-plugin-offline`, - dontCacheBustURLsMatching: /(\.js$|\.css$|static\/)/, - maximumFileSizeToCacheInBytes: 4800, - runtimeCaching: [ - { - urlPattern: /(\.js$|\.css$|static\/)/, - handler: `CacheFirst`, - }, - { - urlPattern: /^https?:.*\/page-data\/.*\.json/, - handler: `StaleWhileRevalidate`, - }, - { - urlPattern: - /^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/, - handler: `StaleWhileRevalidate`, - }, - { - urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/, - handler: `StaleWhileRevalidate`, + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + precachePages: [`/about-us/`, `/projects/*`], + appendScript: `src/custom-sw-code.js`, + debug: true, + workboxConfig: { + importWorkboxFrom: `local`, + globDirectory: `rootDir`, + globPatterns: [`a`, `b`, `c`], + modifyURLPrefix: { + "/": `pathPrefix/`, }, - ], - skipWaiting: true, - clientsClaim: true, - }, - }) + cacheId: `gatsby-plugin-offline`, + dontCacheBustURLsMatching: /(\.js$|\.css$|static\/)/, + maximumFileSizeToCacheInBytes: 4800, + runtimeCaching: [ + { + urlPattern: /(\.js$|\.css$|static\/)/, + handler: `CacheFirst`, + }, + { + urlPattern: /^https?:.*\/page-data\/.*\.json/, + handler: `StaleWhileRevalidate`, + }, + { + urlPattern: + /^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/, + handler: `StaleWhileRevalidate`, + }, + { + urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/, + handler: `StaleWhileRevalidate`, + }, + ], + skipWaiting: true, + clientsClaim: true, + }, + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) }) diff --git a/packages/gatsby-plugin-sass/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-sass/src/__tests__/gatsby-node.js index 51fa2fbcaf325..25a95ffba12e2 100644 --- a/packages/gatsby-plugin-sass/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-sass/src/__tests__/gatsby-node.js @@ -99,77 +99,85 @@ describe(`pluginOptionsSchema`, () => { `"sassOptions.sourceMapRoot" must be a string`, ] - const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, { - additionalData: 123, - implementation: `This should be a require() thing`, - postCssPlugins: `This should be an array of postCss plugins`, - cssLoaderOptions: `This should be an object of css-loader options`, - sassRuleTest: `This should be a regexp`, - sassRuleModulesTest: `This should be a regexp`, - useResolveUrlLoader: `This should be a boolean`, - sassOptions: { - file: 123, // should be a string - data: 123, // should be a string - importer: `This should be a function`, - functions: `This should be an object of { string: function }`, - includePaths: 123, // should be an array of string - indentedSyntax: `"useResolveUrlLoader" must be a boolean`, - indentType: 123, // this should be a string - indentWidth: 40, - linefeed: `This should be cr, crlf, lf or lfcr`, - omitSourceMapUrl: `This should be a boolean`, - outFile: 123, // This should be a string - outputStyle: `This should be nested, expanded, compact or compressed`, - precision: `This should be a number`, - sourceComments: `This should be a boolean`, - sourceMap: 123, // This should be a string or a boolean - sourceMapContents: `This should be a boolean`, - sourceMapEmbed: `This should be a boolean`, - sourceMapRoot: 123, // This should be a string - }, - }) + const { errors, isValid } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + additionalData: 123, + implementation: `This should be a require() thing`, + postCssPlugins: `This should be an array of postCss plugins`, + cssLoaderOptions: `This should be an object of css-loader options`, + sassRuleTest: `This should be a regexp`, + sassRuleModulesTest: `This should be a regexp`, + useResolveUrlLoader: `This should be a boolean`, + sassOptions: { + file: 123, // should be a string + data: 123, // should be a string + importer: `This should be a function`, + functions: `This should be an object of { string: function }`, + includePaths: 123, // should be an array of string + indentedSyntax: `"useResolveUrlLoader" must be a boolean`, + indentType: 123, // this should be a string + indentWidth: 40, + linefeed: `This should be cr, crlf, lf or lfcr`, + omitSourceMapUrl: `This should be a boolean`, + outFile: 123, // This should be a string + outputStyle: `This should be nested, expanded, compact or compressed`, + precision: `This should be a number`, + sourceComments: `This should be a boolean`, + sourceMap: 123, // This should be a string or a boolean + sourceMapContents: `This should be a boolean`, + sourceMapEmbed: `This should be a boolean`, + sourceMapRoot: 123, // This should be a string + }, + } + ) + expect(isValid).toBe(false) expect(errors).toEqual(expectedErrors) }) it(`should validate the schema`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - additionalData: `$test: #000;`, - implementation: require(`../gatsby-node.js`), - cssLoaderOptions: { camelCase: false }, - postCssPlugins: [require(`autoprefixer`)], - sassRuleTest: /\.global\.s(a|c)ss$/, - sassRuleModulesTest: /\.mod\.s(a|c)ss$/, - useResolveUrlLoader: false, - sassOptions: { - file: `../path-to-file`, - data: `{ some: data }`, - importer: function () { - return { file: `path-to-file`, contents: `data` } - }, - functions: { - "headings($from: 0, $to: 6)": function () { - return [] + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + additionalData: `$test: #000;`, + implementation: require(`../gatsby-node.js`), + cssLoaderOptions: { camelCase: false }, + postCssPlugins: [require(`autoprefixer`)], + sassRuleTest: /\.global\.s(a|c)ss$/, + sassRuleModulesTest: /\.mod\.s(a|c)ss$/, + useResolveUrlLoader: false, + sassOptions: { + file: `../path-to-file`, + data: `{ some: data }`, + importer: function () { + return { file: `path-to-file`, contents: `data` } }, + functions: { + "headings($from: 0, $to: 6)": function () { + return [] + }, + }, + includePaths: [`some`, `path`], + indentedSyntax: true, + indentType: `tabs`, + indentWidth: 7, + linefeed: `crlf`, + omitSourceMapUrl: true, + outFile: `somewhere-around.css`, + outputStyle: `expanded`, + precision: 12, + sourceComments: true, + sourceMap: true, + sourceMapContents: true, + sourceMapEmbed: true, + sourceMapRoot: `some-source-map-root`, }, - includePaths: [`some`, `path`], - indentedSyntax: true, - indentType: `tabs`, - indentWidth: 7, - linefeed: `crlf`, - omitSourceMapUrl: true, - outFile: `somewhere-around.css`, - outputStyle: `expanded`, - precision: 12, - sourceComments: true, - sourceMap: true, - sourceMapContents: true, - sourceMapEmbed: true, - sourceMapRoot: `some-source-map-root`, - }, - }) + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`should allow unknown options`, async () => { diff --git a/packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts b/packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts index 02f0376579477..b640c198c8eb8 100644 --- a/packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts +++ b/packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts @@ -34,12 +34,7 @@ describe(`pluginOptionsSchema`, () => { avifOptions: 1, }, } - const { isValid, errors } = await testPluginOptionsSchema( - pluginOptionsSchema, - options - ) - expect(isValid).toBe(false) - expect(errors).toEqual([ + const expectedErrors = [ `"defaults.formats[0]" must be one of [auto, png, jpg, webp, avif]`, `"defaults.placeholder" must be one of [tracedSVG, dominantColor, blurred, none]`, `"defaults.quality" must be a number`, @@ -51,16 +46,25 @@ describe(`pluginOptionsSchema`, () => { `"defaults.jpgOptions" must be of type object`, `"defaults.pngOptions" must be of type object`, `"defaults.avifOptions" must be of type object`, - ]) + ] + + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + options + ) + + expect(isValid).toBe(false) + expect(errors).toEqual(expectedErrors) }) it(`should accept correct options`, async () => { const options = { defaults } - const { isValid } = await testPluginOptionsSchema( + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, options ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) }) diff --git a/packages/gatsby-plugin-twitter/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-twitter/src/__tests__/gatsby-node.js index e340053e76973..a9bf4fdfaddee 100644 --- a/packages/gatsby-plugin-twitter/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-twitter/src/__tests__/gatsby-node.js @@ -22,10 +22,11 @@ it.each` ${undefined} ${{}} `(`should validate the schema: $options`, async ({ options }) => { - const { isValid } = await testPluginOptionsSchema( + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, options ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) diff --git a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js index 61e8bf8ab7ef2..eb0ab5f24cd3c 100644 --- a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js @@ -80,36 +80,50 @@ describe(`gatsby-plugin-typescript`, () => { `"allExtensions" must be a boolean`, ] - const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, { - isTSX: `this should be a boolean`, - jsxPragma: 123, - allExtensions: `this should be a boolean`, - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + isTSX: `this should be a boolean`, + jsxPragma: 123, + allExtensions: `this should be a boolean`, + } + ) + expect(isValid).toBe(false) expect(errors).toEqual(expectedErrors) }) it(`should validate the schema`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - isTSX: false, - jsxPragma: `ReactFunction`, - allExtensions: false, - allowNamespaces: false, - allowDeclareFields: false, - onlyRemoveTypeImports: false, - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + isTSX: false, + jsxPragma: `ReactFunction`, + allExtensions: false, + allowNamespaces: false, + allowDeclareFields: false, + onlyRemoveTypeImports: false, + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`should break when isTSX doesn't match allExtensions`, async () => { - const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, { - isTSX: true, - jsxPragma: `ReactFunction`, - allExtensions: false, - }) + const expectedErrors = [`"allExtensions" must be [true]`] + + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + isTSX: true, + jsxPragma: `ReactFunction`, + allExtensions: false, + } + ) - expect(errors).toEqual([`"allExtensions" must be [true]`]) + expect(isValid).toBe(false) + expect(errors).toEqual(expectedErrors) }) }) }) diff --git a/packages/gatsby-plugin-utils/README.md b/packages/gatsby-plugin-utils/README.md index edf49f5b3fce4..8b06b8776581c 100644 --- a/packages/gatsby-plugin-utils/README.md +++ b/packages/gatsby-plugin-utils/README.md @@ -34,6 +34,7 @@ it(`should partially validate one value of a schema`, async () => { someOtherValue: Joi.string(), toVerify: Joi.boolean(), }) + const expectedErrors = [`"toVerify" must be a boolean`] // Only the "toVerify" key of the schema will be verified in this test const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, { @@ -41,7 +42,7 @@ it(`should partially validate one value of a schema`, async () => { }) expect(isValid).toBe(false) - expect(errors).toEqual([`"toVerify" must be a boolean`]) + expect(errors).toEqual(expectedErrors) }) ``` diff --git a/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts b/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts index 610947ea94025..77666ccd1e303 100644 --- a/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts +++ b/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts @@ -9,17 +9,14 @@ describe(`testPluginOptionsSchema`, () => { nb: Joi.number(), toVerify: Joi.boolean(), }) + const expectedErrors = [`"toVerify" must be a boolean`] const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, { toVerify: `abcd`, }) expect(isValid).toBe(false) - expect(errors).toMatchInlineSnapshot(` - Array [ - "\\"toVerify\\" must be a boolean", - ] - `) + expect(errors).toEqual(expectedErrors) }) it(`should partially validate multiples value of a schema`, async () => { @@ -29,6 +26,10 @@ describe(`testPluginOptionsSchema`, () => { nb: Joi.number(), toVerify: Joi.boolean(), }) + const expectedErrors = [ + `"nb" must be a number`, + `"toVerify" must be a boolean`, + ] const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, { toVerify: `abcd`, @@ -36,12 +37,7 @@ describe(`testPluginOptionsSchema`, () => { }) expect(isValid).toBe(false) - expect(errors).toMatchInlineSnapshot(` - Array [ - "\\"nb\\" must be a number", - "\\"toVerify\\" must be a boolean", - ] - `) + expect(errors).toEqual(expectedErrors) }) it(`should validate half of a real world plugin schema`, async () => { @@ -84,6 +80,15 @@ describe(`testPluginOptionsSchema`, () => { siteSpeedSampleRate: Joi.number(), cookieDomain: Joi.string(), }) + const expectedErrors = [ + `"trackingId" is required`, + `"head" must be a boolean`, + `"anonymize" must be a boolean`, + `"respectDNT" must be a boolean`, + `"exclude[0]" must be a string`, + `"exclude[1]" must be a string`, + `"exclude[2]" must be a string`, + ] const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, { trackingId: undefined, @@ -94,17 +99,7 @@ describe(`testPluginOptionsSchema`, () => { }) expect(isValid).toBe(false) - expect(errors).toMatchInlineSnapshot(` - Array [ - "\\"trackingId\\" is required", - "\\"head\\" must be a boolean", - "\\"anonymize\\" must be a boolean", - "\\"respectDNT\\" must be a boolean", - "\\"exclude[0]\\" must be a string", - "\\"exclude[1]\\" must be a string", - "\\"exclude[2]\\" must be a string", - ] - `) + expect(errors).toEqual(expectedErrors) }) it(`should validate an entire real world plugin schema`, async () => { @@ -147,6 +142,23 @@ describe(`testPluginOptionsSchema`, () => { siteSpeedSampleRate: Joi.number(), cookieDomain: Joi.string(), }) + const expectedErrors = [ + `"trackingId" is required`, + `"head" must be a boolean`, + `"anonymize" must be a boolean`, + `"respectDNT" must be a boolean`, + `"exclude[0]" must be a string`, + `"exclude[1]" must be a string`, + `"exclude[2]" must be a string`, + `"pageTransitionDelay" must be a number`, + `"optimizeId" must be a string`, + `"experimentId" must be a string`, + `"variationId" must be a string`, + `"defer" must be a boolean`, + `"sampleRate" must be a number`, + `"siteSpeedSampleRate" must be a number`, + `"cookieDomain" must be a string`, + ] const { isValid, errors } = await testPluginOptionsSchema(pluginSchema, { trackingId: undefined, @@ -165,25 +177,7 @@ describe(`testPluginOptionsSchema`, () => { }) expect(isValid).toBe(false) - expect(errors).toMatchInlineSnapshot(` - Array [ - "\\"trackingId\\" is required", - "\\"head\\" must be a boolean", - "\\"anonymize\\" must be a boolean", - "\\"respectDNT\\" must be a boolean", - "\\"exclude[0]\\" must be a string", - "\\"exclude[1]\\" must be a string", - "\\"exclude[2]\\" must be a string", - "\\"pageTransitionDelay\\" must be a number", - "\\"optimizeId\\" must be a string", - "\\"experimentId\\" must be a string", - "\\"variationId\\" must be a string", - "\\"defer\\" must be a boolean", - "\\"sampleRate\\" must be a number", - "\\"siteSpeedSampleRate\\" must be a number", - "\\"cookieDomain\\" must be a string", - ] - `) + expect(errors).toEqual(expectedErrors) }) it(`should check the validity of a schema`, async () => { diff --git a/packages/gatsby-remark-autolink-headers/src/__tests__/gatsby-node.js b/packages/gatsby-remark-autolink-headers/src/__tests__/gatsby-node.js index 5e9f87a880cec..3041fabd335c7 100644 --- a/packages/gatsby-remark-autolink-headers/src/__tests__/gatsby-node.js +++ b/packages/gatsby-remark-autolink-headers/src/__tests__/gatsby-node.js @@ -35,6 +35,17 @@ describe(`pluginOptionsSchema`, () => { }) it(`should invalidate an invalid config`, async () => { + const expectedErrors = [ + `"offsetY" must be a number`, + `"icon" must be one of [string, boolean]`, + `"className" must be a string`, + `"maintainCase" must be a boolean`, + `"removeAccents" must be a boolean`, + `"isIconAfterHeader" must be a boolean`, + `"elements[0]" must be a string`, + `"elements[1]" must be a string`, + ] + // Only the "toVerify" key of the schema will be verified in this test const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, @@ -50,17 +61,6 @@ describe(`pluginOptionsSchema`, () => { ) expect(isValid).toBe(false) - expect(errors).toMatchInlineSnapshot(` - Array [ - "\\"offsetY\\" must be a number", - "\\"icon\\" must be one of [string, boolean]", - "\\"className\\" must be a string", - "\\"maintainCase\\" must be a boolean", - "\\"removeAccents\\" must be a boolean", - "\\"isIconAfterHeader\\" must be a boolean", - "\\"elements[0]\\" must be a string", - "\\"elements[1]\\" must be a string", - ] - `) + expect(errors).toEqual(expectedErrors) }) }) diff --git a/packages/gatsby-remark-images/src/__tests__/gatsby-node.js b/packages/gatsby-remark-images/src/__tests__/gatsby-node.js index 8aeeaca9a7bf5..d3b60e8a826c3 100644 --- a/packages/gatsby-remark-images/src/__tests__/gatsby-node.js +++ b/packages/gatsby-remark-images/src/__tests__/gatsby-node.js @@ -22,55 +22,67 @@ describe(`pluginOptionsSchema`, () => { `"srcSetBreakpoints" must be an array`, ] - const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, { - maxWidth: `This should be a number`, - linkImagesToOriginal: `This should be a boolean`, - showCaptions: `This should be a boolean`, - markdownCaptions: `This should be a boolean`, - wrapperStyle: true, - backgroundColor: 123, - quality: `This should be a number`, - withWebp: `This should be a boolean or an object`, - withAvif: `This should be a boolean or an object`, - tracedSVG: `This should be a boolean`, - loading: `This should be lazy, eager or auto`, - decoding: `This should be async, sync or auto`, - disableBgImageOnAlpha: `This should be a boolean`, - disableBgImage: `This should be a boolean`, - srcSetBreakpoints: `This should be an array`, - }) - + const { errors, isValid } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + maxWidth: `This should be a number`, + linkImagesToOriginal: `This should be a boolean`, + showCaptions: `This should be a boolean`, + markdownCaptions: `This should be a boolean`, + wrapperStyle: true, + backgroundColor: 123, + quality: `This should be a number`, + withWebp: `This should be a boolean or an object`, + withAvif: `This should be a boolean or an object`, + tracedSVG: `This should be a boolean`, + loading: `This should be lazy, eager or auto`, + decoding: `This should be async, sync or auto`, + disableBgImageOnAlpha: `This should be a boolean`, + disableBgImage: `This should be a boolean`, + srcSetBreakpoints: `This should be an array`, + } + ) + + expect(isValid).toBe(false) expect(errors).toEqual(expectedErrors) }) it(`should validate the schema`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - maxWidth: 700, - linkImagesToOriginal: false, - showCaptions: true, - markdownCaptions: true, - wrapperStyle: { marginTop: `1rem`, padding: `1.5rem`, color: `blue` }, - backgroundColor: `red`, - quality: 77, - withWebp: true, - withAvif: true, - tracedSVG: true, - loading: `eager`, - decoding: `async`, - disableBgImageOnAlpha: true, - disableBgImage: true, - srcSetBreakpoints: [400, 600, 800], - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + maxWidth: 700, + linkImagesToOriginal: false, + showCaptions: true, + markdownCaptions: true, + wrapperStyle: { marginTop: `1rem`, padding: `1.5rem`, color: `blue` }, + backgroundColor: `red`, + quality: 77, + withWebp: true, + withAvif: true, + tracedSVG: true, + loading: `eager`, + decoding: `async`, + disableBgImageOnAlpha: true, + disableBgImage: true, + srcSetBreakpoints: [400, 600, 800], + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`should validate the withWebp prop`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - withWebp: { quality: 100 }, - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + withWebp: { quality: 100 }, + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`should validate the withAvif prop`, async () => { @@ -83,35 +95,55 @@ describe(`pluginOptionsSchema`, () => { describe(`allow to use array of valid strings for "showCaptions"`, () => { it(`["title", "alt"]`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - showCaptions: [`title`, `alt`], - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + showCaptions: [`title`, `alt`], + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`["title"]`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - showCaptions: [`title`], - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + showCaptions: [`title`], + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`["alt"]`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - showCaptions: [`alt`], - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + showCaptions: [`alt`], + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`["not valid"] (should fail validation)`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - showCaptions: [`not valid`], - }) + const expectedErrors = [ + `"showCaptions[0]" does not match any of the allowed types`, + ] + + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + showCaptions: [`not valid`], + } + ) expect(isValid).toBe(false) + expect(errors).toEqual(expectedErrors) }) }) @@ -121,52 +153,66 @@ describe(`pluginOptionsSchema`, () => { [`true`, true], [`false`, false], ])(`%s`, async (_title, booleanValue) => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - tracedSVG: booleanValue, - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + tracedSVG: booleanValue, + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) }) describe(`supports object notation`, () => { it(`should validate when all fields are set`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - tracedSVG: { - turnPolicy: Potrace.TURNPOLICY_RIGHT, - turdSize: 50, - alphaMax: 0.5, - optCurve: false, - optTolerance: 0.9, - threshold: 230, - blackOnWhite: false, - color: `red`, - background: `green`, - }, - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + tracedSVG: { + turnPolicy: Potrace.TURNPOLICY_RIGHT, + turdSize: 50, + alphaMax: 0.5, + optCurve: false, + optTolerance: 0.9, + threshold: 230, + blackOnWhite: false, + color: `red`, + background: `green`, + }, + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`should validate when some fields are set`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - tracedSVG: { - turnPolicy: Potrace.TURNPOLICY_RIGHT, - turdSize: 50, - // alphaMax: 0.5, - // optCurve: 0.2, - // optTolerance: 0.9, - // threshold: 230, - // blackOnWhite: false, - color: `red`, - background: `green`, - }, - }) + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + tracedSVG: { + turnPolicy: Potrace.TURNPOLICY_RIGHT, + turdSize: 50, + // alphaMax: 0.5, + // optCurve: 0.2, + // optTolerance: 0.9, + // threshold: 230, + // blackOnWhite: false, + color: `red`, + background: `green`, + }, + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`should fail validation when unknown fields are set`, async () => { + const expectedErrors = [`"tracedSVG.foo" is not allowed`] + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, { @@ -177,11 +223,7 @@ describe(`pluginOptionsSchema`, () => { ) expect(isValid).toBe(false) - expect(errors).toMatchInlineSnapshot(` - Array [ - "\\"tracedSVG.foo\\" is not allowed", - ] - `) + expect(errors).toEqual(expectedErrors) }) describe(`turnPolicy variants`, () => { @@ -193,7 +235,7 @@ describe(`pluginOptionsSchema`, () => { `TURNPOLICY_MINORITY`, `TURNPOLICY_MAJORITY`, ])(`supports setting by policy name (%s)`, async name => { - const { isValid } = await testPluginOptionsSchema( + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, { tracedSVG: { turnPolicy: name }, @@ -201,6 +243,7 @@ describe(`pluginOptionsSchema`, () => { ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it.each([ @@ -211,7 +254,7 @@ describe(`pluginOptionsSchema`, () => { Potrace.TURNPOLICY_MINORITY, Potrace.TURNPOLICY_MAJORITY, ])(`supports setting by policy value (%s)`, async value => { - const { isValid } = await testPluginOptionsSchema( + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, { tracedSVG: { turnPolicy: value }, @@ -219,9 +262,14 @@ describe(`pluginOptionsSchema`, () => { ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) it(`Doesn't support arbitrary string values`, async () => { + const expectedErrors = [ + `"tracedSVG.turnPolicy" must be one of [TURNPOLICY_BLACK, TURNPOLICY_WHITE, TURNPOLICY_LEFT, TURNPOLICY_RIGHT, TURNPOLICY_MINORITY, TURNPOLICY_MAJORITY, black, white, left, right, minority, majority]`, + ] + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, { @@ -230,11 +278,7 @@ describe(`pluginOptionsSchema`, () => { ) expect(isValid).toBe(false) - expect(errors).toMatchInlineSnapshot(` - Array [ - "\\"tracedSVG.turnPolicy\\" must be one of [TURNPOLICY_BLACK, TURNPOLICY_WHITE, TURNPOLICY_LEFT, TURNPOLICY_RIGHT, TURNPOLICY_MINORITY, TURNPOLICY_MAJORITY, black, white, left, right, minority, majority]", - ] - `) + expect(errors).toEqual(expectedErrors) }) }) @@ -272,7 +316,7 @@ describe(`pluginOptionsSchema`, () => { value = titleAndMaybeValue } - const { isValid } = await testPluginOptionsSchema( + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, { tracedSVG: { threshold: value }, @@ -280,6 +324,7 @@ describe(`pluginOptionsSchema`, () => { ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) // invalid settings diff --git a/packages/gatsby-source-wordpress/__tests__/plugin-options-schema.test.js b/packages/gatsby-source-wordpress/__tests__/plugin-options-schema.test.js index aee923ce17d57..3b11c5ee5ee24 100644 --- a/packages/gatsby-source-wordpress/__tests__/plugin-options-schema.test.js +++ b/packages/gatsby-source-wordpress/__tests__/plugin-options-schema.test.js @@ -3,25 +3,24 @@ import { pluginOptionsSchema } from "gatsby-source-wordpress/dist/steps/declare- describe(`pluginOptionsSchema`, () => { it(`should validate a minimal, valid config`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { + const { isValid, errors } = await testPluginOptionsSchema(pluginOptionsSchema, { url: `http://localhost:8000/graphql`, }) expect(isValid).toEqual(true) + expect(errors).toEqual([]) }) it(`should invalidate a config missing required vars`, async () => { + const expectedErrors = [`"url" is required`,] + const { isValid, errors } = await testPluginOptionsSchema( pluginOptionsSchema, {} ) expect(isValid).toEqual(false) - expect(errors).toMatchInlineSnapshot(` - Array [ - "\\"url\\" is required", - ] - `) + expect(errors).toEqual(expectedErrors) }) it(`should validate a fully custom config`, async () => { @@ -114,7 +113,7 @@ describe(`pluginOptionsSchema`, () => { } ) - expect(errors).toEqual([]) expect(isValid).toEqual(true) + expect(errors).toEqual([]) }) }) diff --git a/packages/gatsby-transformer-remark/src/__tests__/gatsby-node.js b/packages/gatsby-transformer-remark/src/__tests__/gatsby-node.js index d288f29ab165a..7e4ff8648b75d 100644 --- a/packages/gatsby-transformer-remark/src/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-remark/src/__tests__/gatsby-node.js @@ -9,30 +9,38 @@ describe(`gatsby-node.js`, () => { `"plugins" must be an array`, ] - const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, { - footnotes: `this should be a boolean`, - gfm: `this should be a boolean`, - plugins: `this should be an array`, - }) + const { errors, isValid } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + footnotes: `this should be a boolean`, + gfm: `this should be a boolean`, + plugins: `this should be an array`, + } + ) + expect(isValid).toBe(false) expect(errors).toEqual(expectedErrors) }) it(`should validate the schema`, async () => { - const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { - footnotes: false, - gfm: false, - plugins: [ - `gatsby-remark-copy-linked-files`, - { - resolve: `gatsby-remark-images`, - options: { - maxWidth: 756, + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + { + footnotes: false, + gfm: false, + plugins: [ + `gatsby-remark-copy-linked-files`, + { + resolve: `gatsby-remark-images`, + options: { + maxWidth: 756, + }, }, - }, - ], - }) + ], + } + ) expect(isValid).toBe(true) + expect(errors).toEqual([]) }) })