Back to Repositories

Validating Recipe Resource Declarations in GatsbyJS

This test suite validates the recipe validation module in Gatsby Recipes, ensuring proper handling of resource declarations. It verifies error detection for invalid resources and successful validation of legitimate recipe configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of recipe validation functionality.

Key areas tested include:
  • Invalid resource detection and error messaging
  • Successful validation of valid recipe configurations
  • Multiple resource type handling (File and NPMPackage)
Edge cases focus on unknown resource types and empty validation responses.

Implementation Analysis

The testing approach uses Jest’s describe/it pattern for structured test organization. Tests validate both error and success paths using mock recipe configurations. The implementation leverages Jest’s expect assertions for validation checking and error message verification.

Technical Details

Testing tools and setup:
  • Jest testing framework
  • Module import/export pattern
  • Mock recipe data structures
  • Assertion-based validation

Best Practices Demonstrated

The test suite demonstrates strong testing practices including isolated test cases, clear test descriptions, and comprehensive validation scenarios. Notable practices include:
  • Descriptive test case naming
  • Separate positive and negative test paths
  • Structured test organization
  • Clear assertion expectations

gatsbyjs/gatsby

deprecated-packages/gatsby-recipes/src/validate-recipe.test.js

            
import validateRecipe from "./validate-recipe"

describe(`validate module validates recipes with resource declarations`, () => {
  it(`returns errors for unknown resources`, () => {
    const recipe = [{ Fake: [{}] }]

    const result = validateRecipe(recipe)[0]

    expect(result.validationError).toMatch(`Unknown resource Fake`)
  })

  it(`returns empty array if there's no errors`, () => {
    const recipe = [
      { File: [{ path: `yo.md`, content: `pizza` }] },
      { NPMPackage: [{ name: `wee-package` }] },
    ]
    const validationResponse = validateRecipe(recipe)
    expect(validationResponse).toHaveLength(0)
  })
})