Back to Repositories

Testing Directory Resource Operations in GatsbyJS

This test suite validates the directory resource functionality in Gatsby Recipes, focusing on directory creation, reading, updating, and deletion operations. It ensures proper handling of file system operations through comprehensive end-to-end testing.

Test Coverage Overview

The test suite provides complete coverage of directory resource lifecycle operations:

  • Directory creation and validation
  • Resource reading and verification
  • Update operations with path changes
  • Cleanup and resource destruction
  • Error handling and validation checks

Implementation Analysis

The testing approach utilizes Jest’s mocking capabilities to simulate file system operations. It implements a structured testing pattern with setup, execution, and verification phases for each operation. The tests leverage Joi schema validation to ensure response integrity.

Technical Details

  • Testing Framework: Jest
  • Validation: @hapi/joi
  • File System: fs-extra (mocked)
  • Resource Schema: Custom implementation
  • Path handling: Node.js path module

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated testing environment using mocks
  • Comprehensive lifecycle testing
  • Schema-based response validation
  • Clear test organization and structure
  • Proper cleanup after test execution

gatsbyjs/gatsby

deprecated-packages/gatsby-recipes/src/providers/fs/directory.test.js

            
import * as directory from "./directory"
import resourceSchema from "../resource-schema"
import Joi from "@hapi/joi"
import fs from "fs-extra"
import path from "path"
jest.mock(`fs-extra`)

const root = `fakeDir`

describe(`directory resource`, () => {
  test(`e2e directory resource test`, async () => {
    const context = { root }
    const initialObject = { path: `directory` }
    const partialUpdate = { path: `directory1` }

    const fullPath = path.join(root, initialObject.path)

    const createPlan = await directory.plan(context, initialObject)
    expect(createPlan).toBeTruthy()

    expect(createPlan).toMatchInlineSnapshot(`
      Object {
        "describe": "Create directory \\"directory\\"",
      }
    `)

    // Test creating the resource
    const createResponse = await directory.create(context, initialObject)
    const validateResult = Joi.validate(createResponse, {
      ...directory.schema,
      ...resourceSchema,
    })
    expect(validateResult.error).toBeNull()
    expect(fs.ensureDir).toHaveBeenCalledWith(fullPath)

    expect(createResponse).toMatchInlineSnapshot(`
      Object {
        "_message": "Created directory \\"directory\\"",
        "id": "directory",
        "path": "directory",
      }
    `)

    // Test reading the resource
    const readResponse = await directory.read(context, createResponse.id)
    expect(readResponse).toEqual(createResponse)

    // Test updating the resource
    const updatedResource = { ...readResponse, ...partialUpdate }
    const updatePlan = await directory.plan(context, updatedResource)
    expect(updatePlan).toMatchInlineSnapshot(`
      Object {
        "describe": "Create directory \\"directory1\\"",
      }
    `)

    fs.ensureDir.mockReset()
    const updateResponse = await directory.update(context, updatedResource)
    expect(fs.ensureDir).toHaveBeenCalledWith(fullPath)

    await directory.destroy(context, updateResponse)
    expect(fs.rmdir).toHaveBeenCalledWith(fullPath)
  })
})