How to test schema defaults for a Gatsby Plugins

The recommended way to test your pluginSchemaOption function is to use testPluginOptionsSchema from the gatsby-plugin-utils package.

For information on how to get started with plugin testing and how to use the testPluginOptionsSchema, read Add tests to your Gatsby Plugin project βœ… πŸ§ͺ.

However, when writing tests for the gatsby-plugin-cloudinary plugin this week, I realized it does not help with testing the default configuration or any other modifiers.

Luckily testing the pluginSchemaOption without the help of testPluginOptionsSchema was possible:

import Joi from "joi";
import { pluginOptionsSchema } from "./gatsby-node";

describe("pluginOptionsSchema", () => {
  test("should add defaults", async () => {
    const schema = pluginOptionsSchema({ Joi });
    const options = {
      cloudName: "cloudName",
      apiKey: "apiKey",
      apiSecret: "apiSecret",
      maxResults: 50,
    };
    const { value } = schema.validate(options);

    expect(value).toEqual({
      ...options,
      resourceType: "image",
      maxResults: 50,
      resultsPerPage: 50,
      tags: false,
    });
  });
});

It was extra important for this plugin as resultsPerPageis actually dependent on maxResults using Joi.ref('maxResults').

Let me know if this was of use to you!

Β 

All the best,
Queen Raae

Β 

PS: Tomorrow, we’ll get a sneak peek at the almost fully cooked Gatsby Slices API πŸ•