Back to Repositories

Testing Site Metadata Plugin Operations in GatsbyJS

This test suite validates the Gatsby site metadata functionality, focusing on plugin resource management and parallel operations handling. It ensures proper metadata creation, updates, and cleanup in a Gatsby site context.

Test Coverage Overview

The test suite provides comprehensive coverage of Gatsby site metadata operations.

Key areas tested include:
  • End-to-end plugin resource functionality
  • Parallel metadata creation operations
  • Metadata value updates and modifications
  • Resource cleanup and destruction

Implementation Analysis

The testing approach utilizes Jest’s asynchronous testing capabilities with fixture-based setup. It implements temporary directory management for isolated testing environments and employs mock implementations for external dependencies like node-fetch.

Notable patterns include:
  • Fixture-based test data setup
  • Async/await pattern usage
  • Resource cleanup handling
  • Parallel operation testing

Technical Details

Testing tools and setup:
  • Jest test framework
  • fs-extra for file operations
  • tmp-promise for temporary directory management
  • fetch-mock-jest for HTTP request mocking
  • Custom resource test helper implementation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, comprehensive setup and teardown procedures, and effective use of fixtures.

Notable practices:
  • Isolated test environments
  • Proper resource cleanup
  • Snapshot testing for verification
  • Modular test helper utilization

gatsbyjs/gatsby

deprecated-packages/gatsby-recipes/src/providers/gatsby/site-metadata.test.js

            
const fs = require(`fs-extra`)
const path = require(`path`)
const tmp = require(`tmp-promise`)

const plugin = require(`./site-metadata`)

jest.mock(`node-fetch`, () => require(`fetch-mock-jest`).sandbox())
const { mockReadmeLoader } = require(`../../test-helper`)
mockReadmeLoader()
const resourceTestHelper = require(`../resource-test-helper`)

const STARTER_BLOG_FIXTURE = path.join(
  __dirname,
  `./fixtures/gatsby-starter-blog`
)

describe(`gatsby-plugin resource`, () => {
  let tmpDir
  let starterBlogRoot
  let emptyRoot
  beforeAll(async () => {
    tmpDir = await tmp.dir({
      unsafeCleanup: true,
    })
    starterBlogRoot = path.join(tmpDir.path, `gatsby-starter-blog`)
    emptyRoot = path.join(tmpDir.path, `empty-site-directory`)
    await fs.ensureDir(emptyRoot)
    await fs.ensureDir(starterBlogRoot)
    await fs.copy(STARTER_BLOG_FIXTURE, starterBlogRoot)
  })
  afterAll(async () => {
    if (tmpDir) {
      await tmpDir.cleanup()
    }
  })

  test(`e2e plugin resource test`, async () => {
    await resourceTestHelper({
      resourceModule: plugin,
      resourceName: `GatsbySiteMetadata`,
      context: { root: starterBlogRoot },
      initialObject: { name: `author`, value: `Fred` },
      partialUpdate: { name: `author`, value: `Velma` },
    })
  })

  test(`handles multiple parallel create calls`, async () => {
    const root = starterBlogRoot
    const resultPromise = plugin.create(
      {
        root,
      },
      {
        name: `husky`,
        value: `hi`,
      }
    )
    const result2Promise = plugin.create(
      {
        root,
      },
      {
        name: `husky2`,
        value: `hi`,
      }
    )

    const result = await resultPromise
    const result2 = await result2Promise

    expect(result).toMatchSnapshot()
    expect(result2).toMatchSnapshot()

    await plugin.destroy({ root }, result)
    await plugin.destroy({ root }, result2)
  })
})