Back to Repositories

Validating GraphQL Type Definitions for Gatsby Recipes in GatsbyJS

This test suite validates the GraphQL type definitions and schema structure for Gatsby Recipes, focusing on mutation and query types for various Gatsby and Contentful resources. The tests ensure proper type creation and schema organization for the recipe system.

Test Coverage Overview

The test suite provides comprehensive coverage of GraphQL type definitions, examining both mutation and query types.

Key areas tested include:
  • Mutation types for creating, updating, and destroying resources
  • Query types for individual and collection-based resource retrieval
  • Type definitions for Gatsby, Contentful, NPM, and file system resources
  • Input type validation and resolver function presence

Implementation Analysis

The testing approach uses Jest’s snapshot testing to verify the structure of generated GraphQL types. It implements a single test case that validates the entire type system output, comparing it against an inline snapshot to ensure consistency in the schema definition.

Technical Details

Testing tools and configuration:
  • Jest as the testing framework
  • toMatchInlineSnapshot for schema validation
  • Separate validation for mutation and query types
  • Type definitions include input types, resolvers, and return types

Best Practices Demonstrated

The test exemplifies several testing best practices:
  • Snapshot testing for complex object structures
  • Comprehensive type system validation
  • Clear separation of mutation and query type testing
  • Efficient test organization with single responsibility principle

gatsbyjs/gatsby

deprecated-packages/gatsby-recipes/src/create-types.test.js

            
import createTypes from "./create-types"

test(`create-types`, () => {
  const result = createTypes()
  expect(result.mutationTypes).toMatchInlineSnapshot(`
    Object {
      "createContentfulEntry": Object {
        "args": Object {
          "contentfulEntry": Object {
            "type": "ContentfulEntryInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulEntry",
      },
      "createContentfulEnvironment": Object {
        "args": Object {
          "contentfulEnvironment": Object {
            "type": "ContentfulEnvironmentInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulEnvironment",
      },
      "createContentfulSpace": Object {
        "args": Object {
          "contentfulSpace": Object {
            "type": "ContentfulSpaceInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulSpace",
      },
      "createContentfulType": Object {
        "args": Object {
          "contentfulType": Object {
            "type": "ContentfulTypeInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulType",
      },
      "createDirectory": Object {
        "args": Object {
          "directory": Object {
            "type": "DirectoryInput",
          },
        },
        "resolve": [Function],
        "type": "Directory",
      },
      "createFile": Object {
        "args": Object {
          "file": Object {
            "type": "FileInput",
          },
        },
        "resolve": [Function],
        "type": "File",
      },
      "createGatsbyPage": Object {
        "args": Object {
          "gatsbyPage": Object {
            "type": "GatsbyPageInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyPage",
      },
      "createGatsbyPlugin": Object {
        "args": Object {
          "gatsbyPlugin": Object {
            "type": "GatsbyPluginInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyPlugin",
      },
      "createGatsbyShadowFile": Object {
        "args": Object {
          "gatsbyShadowFile": Object {
            "type": "GatsbyShadowFileInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyShadowFile",
      },
      "createGatsbySiteMetadata": Object {
        "args": Object {
          "gatsbySiteMetadata": Object {
            "type": "GatsbySiteMetadataInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbySiteMetadata",
      },
      "createGitIgnore": Object {
        "args": Object {
          "gitIgnore": Object {
            "type": "GitIgnoreInput",
          },
        },
        "resolve": [Function],
        "type": "GitIgnore",
      },
      "createNpmPackage": Object {
        "args": Object {
          "npmPackage": Object {
            "type": "NPMPackageInput",
          },
        },
        "resolve": [Function],
        "type": "NPMPackage",
      },
      "createNpmPackageJson": Object {
        "args": Object {
          "npmPackageJson": Object {
            "type": "NPMPackageJsonInput",
          },
        },
        "resolve": [Function],
        "type": "NPMPackageJson",
      },
      "createNpmScript": Object {
        "args": Object {
          "npmScript": Object {
            "type": "NPMScriptInput",
          },
        },
        "resolve": [Function],
        "type": "NPMScript",
      },
      "destroyContentfulEntry": Object {
        "args": Object {
          "contentfulEntry": Object {
            "type": "ContentfulEntryInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulEntry",
      },
      "destroyContentfulEnvironment": Object {
        "args": Object {
          "contentfulEnvironment": Object {
            "type": "ContentfulEnvironmentInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulEnvironment",
      },
      "destroyContentfulSpace": Object {
        "args": Object {
          "contentfulSpace": Object {
            "type": "ContentfulSpaceInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulSpace",
      },
      "destroyContentfulType": Object {
        "args": Object {
          "contentfulType": Object {
            "type": "ContentfulTypeInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulType",
      },
      "destroyDirectory": Object {
        "args": Object {
          "directory": Object {
            "type": "DirectoryInput",
          },
        },
        "resolve": [Function],
        "type": "Directory",
      },
      "destroyFile": Object {
        "args": Object {
          "file": Object {
            "type": "FileInput",
          },
        },
        "resolve": [Function],
        "type": "File",
      },
      "destroyGatsbyPage": Object {
        "args": Object {
          "gatsbyPage": Object {
            "type": "GatsbyPageInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyPage",
      },
      "destroyGatsbyPlugin": Object {
        "args": Object {
          "gatsbyPlugin": Object {
            "type": "GatsbyPluginInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyPlugin",
      },
      "destroyGatsbyShadowFile": Object {
        "args": Object {
          "gatsbyShadowFile": Object {
            "type": "GatsbyShadowFileInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyShadowFile",
      },
      "destroyGatsbySiteMetadata": Object {
        "args": Object {
          "gatsbySiteMetadata": Object {
            "type": "GatsbySiteMetadataInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbySiteMetadata",
      },
      "destroyGitIgnore": Object {
        "args": Object {
          "gitIgnore": Object {
            "type": "GitIgnoreInput",
          },
        },
        "resolve": [Function],
        "type": "GitIgnore",
      },
      "destroyNpmPackage": Object {
        "args": Object {
          "npmPackage": Object {
            "type": "NPMPackageInput",
          },
        },
        "resolve": [Function],
        "type": "NPMPackage",
      },
      "destroyNpmPackageJson": Object {
        "args": Object {
          "npmPackageJson": Object {
            "type": "NPMPackageJsonInput",
          },
        },
        "resolve": [Function],
        "type": "NPMPackageJson",
      },
      "destroyNpmScript": Object {
        "args": Object {
          "npmScript": Object {
            "type": "NPMScriptInput",
          },
        },
        "resolve": [Function],
        "type": "NPMScript",
      },
      "updateContentfulEntry": Object {
        "args": Object {
          "contentfulEntry": Object {
            "type": "ContentfulEntryInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulEntry",
      },
      "updateContentfulEnvironment": Object {
        "args": Object {
          "contentfulEnvironment": Object {
            "type": "ContentfulEnvironmentInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulEnvironment",
      },
      "updateContentfulSpace": Object {
        "args": Object {
          "contentfulSpace": Object {
            "type": "ContentfulSpaceInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulSpace",
      },
      "updateContentfulType": Object {
        "args": Object {
          "contentfulType": Object {
            "type": "ContentfulTypeInput",
          },
        },
        "resolve": [Function],
        "type": "ContentfulType",
      },
      "updateDirectory": Object {
        "args": Object {
          "directory": Object {
            "type": "DirectoryInput",
          },
        },
        "resolve": [Function],
        "type": "Directory",
      },
      "updateFile": Object {
        "args": Object {
          "file": Object {
            "type": "FileInput",
          },
        },
        "resolve": [Function],
        "type": "File",
      },
      "updateGatsbyPage": Object {
        "args": Object {
          "gatsbyPage": Object {
            "type": "GatsbyPageInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyPage",
      },
      "updateGatsbyPlugin": Object {
        "args": Object {
          "gatsbyPlugin": Object {
            "type": "GatsbyPluginInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyPlugin",
      },
      "updateGatsbyShadowFile": Object {
        "args": Object {
          "gatsbyShadowFile": Object {
            "type": "GatsbyShadowFileInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbyShadowFile",
      },
      "updateGatsbySiteMetadata": Object {
        "args": Object {
          "gatsbySiteMetadata": Object {
            "type": "GatsbySiteMetadataInput",
          },
        },
        "resolve": [Function],
        "type": "GatsbySiteMetadata",
      },
      "updateGitIgnore": Object {
        "args": Object {
          "gitIgnore": Object {
            "type": "GitIgnoreInput",
          },
        },
        "resolve": [Function],
        "type": "GitIgnore",
      },
      "updateNpmPackage": Object {
        "args": Object {
          "npmPackage": Object {
            "type": "NPMPackageInput",
          },
        },
        "resolve": [Function],
        "type": "NPMPackage",
      },
      "updateNpmPackageJson": Object {
        "args": Object {
          "npmPackageJson": Object {
            "type": "NPMPackageJsonInput",
          },
        },
        "resolve": [Function],
        "type": "NPMPackageJson",
      },
      "updateNpmScript": Object {
        "args": Object {
          "npmScript": Object {
            "type": "NPMScriptInput",
          },
        },
        "resolve": [Function],
        "type": "NPMScript",
      },
    }
  `)
  expect(result.queryTypes).toMatchInlineSnapshot(`
    Object {
      "allContentfulEntry": Object {
        "resolve": [Function],
        "type": "ContentfulEntryConnection",
      },
      "allContentfulEnvironment": Object {
        "resolve": [Function],
        "type": "ContentfulEnvironmentConnection",
      },
      "allContentfulSpace": Object {
        "resolve": [Function],
        "type": "ContentfulSpaceConnection",
      },
      "allContentfulType": Object {
        "resolve": [Function],
        "type": "ContentfulTypeConnection",
      },
      "allGatsbyPage": Object {
        "resolve": [Function],
        "type": "GatsbyPageConnection",
      },
      "allGatsbyPlugin": Object {
        "resolve": [Function],
        "type": "GatsbyPluginConnection",
      },
      "allGatsbySiteMetadata": Object {
        "resolve": [Function],
        "type": "GatsbySiteMetadataConnection",
      },
      "allGitIgnore": Object {
        "resolve": [Function],
        "type": "GitIgnoreConnection",
      },
      "allNPMPackageJson": Object {
        "resolve": [Function],
        "type": "NPMPackageJsonConnection",
      },
      "allNPMScript": Object {
        "resolve": [Function],
        "type": "NPMScriptConnection",
      },
      "contentfulEntry": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "ContentfulEntry",
      },
      "contentfulEnvironment": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "ContentfulEnvironment",
      },
      "contentfulSpace": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "ContentfulSpace",
      },
      "contentfulType": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "ContentfulType",
      },
      "directory": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "Directory",
      },
      "file": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "File",
      },
      "gatsbyPage": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "GatsbyPage",
      },
      "gatsbyPlugin": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "GatsbyPlugin",
      },
      "gatsbyShadowFile": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "GatsbyShadowFile",
      },
      "gatsbySiteMetadata": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "GatsbySiteMetadata",
      },
      "gitIgnore": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "GitIgnore",
      },
      "npmPackage": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "NPMPackage",
      },
      "npmPackageJson": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "NPMPackageJson",
      },
      "npmScript": Object {
        "args": Object {
          "id": Object {
            "type": "String",
          },
        },
        "resolve": [Function],
        "type": "NPMScript",
      },
    }
  `)
})