Back to Repositories

Testing Recipe Transform Operations in GatsbyJS

A comprehensive test suite for validating the transformation of render output in Gatsby Recipes, focusing on NPM package installations and file operations. The tests ensure proper structure conversion and resource definition handling for the recipe execution plan.

Test Coverage Overview

The test suite provides coverage for transforming fixture data into a plan structure, specifically handling NPM package definitions and file operations.

  • Validates NPM package resource definitions and state management
  • Verifies correct transformation of file operations with content and path handling
  • Tests array structure preservation and length validation

Implementation Analysis

The testing approach utilizes Jest’s assertion framework to validate the transform function’s output structure.

The implementation follows a destructuring pattern to separately test NPM package and file transformations, employing specific assertions for resource properties and state values.

Technical Details

  • Testing Framework: Jest
  • Fixture Structure: Nested object with children arrays
  • Transform Function: Converts MDX-like structure to plan format
  • Assertion Methods: toEqual, toHaveLength

Best Practices Demonstrated

The test demonstrates clean testing practices with clear separation of concerns and focused assertions.

  • Single responsibility principle in test cases
  • Proper test fixture organization
  • Clear expected vs actual value comparisons
  • Efficient use of Jest matchers

gatsbyjs/gatsby

deprecated-packages/gatsby-recipes/src/renderer/transform-to-plan-structure.test.js

            
import transform from "./transform-to-plan-structure"

const fixture = {
  children: [
    {
      children: [
        {
          type: `NPMPackage`,
          children: [
            {
              text: `{"newState": "gatsby@latest", "_props": {"name": "gatsby"}}`,
            },
          ],
        },
        {
          type: `File`,
          children: [
            {
              text: `{"currentState":"","newState":"/** foo */","describe":"Write foo.js","diff":"- Original  - 0/n+ Modified  + 1/n/n+ /** foo */","_props":{"path":"foo.js","content":"/** foo */"}}`,
            },
          ],
        },
        {
          type: `File`,
          children: [
            {
              text: `{"currentState":"","newState":"/** foo2 */","describe":"Write foo2.js","diff":"- Original  - 0/n+ Modified  + 1/n/n+ /** foo2 */","_props":{"path":"foo2.js","content":"/** foo2 */"}}`,
            },
          ],
        },
      ],
    },
  ],
}

test(`transforms the render output`, async () => {
  const [npmPackage, ...files] = transform(fixture)

  expect(npmPackage.resourceDefinitions.name).toEqual(`gatsby`)
  expect(npmPackage.newState).toEqual(`gatsby@latest`)
  expect(files).toHaveLength(2)
})