Back to Repositories

Testing NPM Package Management Workflow in GatsbyJS

This test suite validates the NPM package management functionality in Gatsby Recipes, focusing on package installation, dependency management, and package manager command generation. The tests ensure proper handling of both production and development dependencies across npm and yarn package managers.

Test Coverage Overview

The test suite provides comprehensive coverage of NPM package operations within Gatsby Recipes.

Key areas tested include:
  • Package installation planning and execution
  • Multiple package installation handling
  • Production vs development dependency management
  • Package version management
  • Package manager command generation for both npm and yarn

Implementation Analysis

The testing approach utilizes Jest’s describe/test blocks to organize related test cases logically. The implementation leverages snapshot testing for command validation and employs async/await patterns for package operations.

Notable patterns include:
  • Resource test helper for E2E validation
  • Isolated test environments using temporary directories
  • Promise-based concurrent installation testing
  • Command generation verification through snapshots

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • fs-extra for file system operations
  • uuid for unique test directory generation
  • Custom resource test helper for standardized testing
  • Snapshot testing for command validation
  • Temporary directory isolation for test cases

Best Practices Demonstrated

The test suite exemplifies several testing best practices for package management verification.

Notable practices include:
  • Isolation of test environments
  • Comprehensive command generation testing
  • Parallel test execution for efficiency
  • Clear test case organization
  • Proper timeout handling for async operations
  • Snapshot testing for command verification

gatsbyjs/gatsby

deprecated-packages/gatsby-recipes/src/providers/npm/package.test.js

            
const os = require(`os`)
const path = require(`path`)
const uuid = require(`uuid`)
const fs = require(`fs-extra`)

const pkg = require(`./package`)
const resourceTestHelper = require(`../resource-test-helper`)

const root = path.join(os.tmpdir(), uuid.v4())
fs.mkdirSync(root)
const pkgResource = { name: `div` }

test(`plan returns a description`, async () => {
  const result = await pkg.plan({ root }, pkgResource)

  expect(result.describe).toEqual(expect.stringContaining(`Install div`))
})

describe(`npm package resource`, () => {
  test(`e2e npm package resource test`, async () => {
    await resourceTestHelper({
      resourceModule: pkg,
      resourceName: `NPMPackage`,
      context: { root },
      initialObject: { name: `is-sorted`, version: `1.0.0` },
      partialUpdate: { name: `is-sorted`, version: `1.0.2` },
    })
  })
  test(`installs 2 resources, one prod & one dev`, async () => {
    await Promise.all([
      pkg.create({ root }, { name: `div` }),
      pkg.create({ root }, { name: `is-odd`, dependencyType: `development` }),
    ])

    const divResource = await pkg.read({ root }, `div`)
    const isOddResource = await pkg.read({ root }, `is-odd`)

    expect(divResource).toMatchSnapshot()
    expect(isOddResource).toMatchSnapshot()
  }, 20000)
})

describe(`package manager client commands`, () => {
  it(`generates the correct commands for yarn`, () => {
    const yarnInstall = pkg.generateClientComands({
      packageManager: `yarn`,
      depType: ``,
      packageNames: [`gatsby`],
    })

    const yarnDevInstall = pkg.generateClientComands({
      packageManager: `yarn`,
      depType: `development`,
      packageNames: [`eslint`],
    })

    expect(yarnInstall).toMatchSnapshot()
    expect(yarnDevInstall).toMatchSnapshot()
  })

  it(`generates the correct commands for npm`, () => {
    const yarnInstall = pkg.generateClientComands({
      packageManager: `npm`,
      depType: ``,
      packageNames: [`gatsby`],
    })

    const yarnDevInstall = pkg.generateClientComands({
      packageManager: `npm`,
      depType: `development`,
      packageNames: [`eslint`],
    })

    expect(yarnInstall).toMatchSnapshot()
    expect(yarnDevInstall).toMatchSnapshot()
  })
})