Back to Repositories

Testing ContentGraph Node Management in Parcel Bundler

This test suite validates the ContentGraph class functionality in Parcel’s core graph package, focusing on content key management and node operations. The tests ensure proper node addition, duplicate handling, and removal behaviors.

Test Coverage Overview

The test suite provides comprehensive coverage of ContentGraph’s core operations.

Key areas tested include:
  • Node addition with content keys
  • Duplicate content key handling
  • Node removal and content key cleanup
  • Graph state validation
Edge cases covered include attempting to add duplicate content keys and verifying cleanup after node removal.

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern with focused unit tests for isolated functionality verification. The implementation leverages Node.js assert module for expectations and employs straightforward test cases with clear setup and verification steps.

Technical patterns include:
  • Instance-based testing with fresh ContentGraph objects
  • Error condition validation
  • State verification before and after operations

Technical Details

Testing tools and configuration:
  • Jest test framework
  • Flow strict-local type checking
  • Node.js assert module for assertions
  • Isolated test cases with individual graph instances
  • ES6+ syntax with modern JavaScript features

Best Practices Demonstrated

The test suite exemplifies several testing best practices for maintaining code quality and reliability.

Notable practices include:
  • Single responsibility per test case
  • Clear test descriptions
  • Proper setup and teardown with fresh instances
  • Explicit state verification
  • Error case coverage
  • Consistent testing patterns

parcel-bundler/parcel

packages/core/graph/test/ContentGraph.test.js

            
// @flow strict-local

import assert from 'assert';
import ContentGraph from '../src/ContentGraph';

describe('ContentGraph', () => {
  it('should addNodeByContentKey if no node exists with the content key', () => {
    let graph = new ContentGraph();

    const node = {};

    const nodeId1 = graph.addNodeByContentKey('contentKey', node);

    assert.deepEqual(graph.getNode(nodeId1), node);
    assert(graph.hasContentKey('contentKey'));
    assert.deepEqual(graph.getNodeByContentKey('contentKey'), node);
  });

  it('should throw if a node with the content key already exists', () => {
    let graph = new ContentGraph();

    graph.addNodeByContentKey('contentKey', {});

    assert.throws(() => {
      graph.addNodeByContentKey('contentKey', {});
    }, /already has content key/);
  });

  it('should remove the content key from graph when node is removed', () => {
    let graph = new ContentGraph();

    const node1 = {};
    const nodeId1 = graph.addNodeByContentKey('contentKey', node1);

    assert.equal(graph.getNode(nodeId1), node1);
    assert(graph.hasContentKey('contentKey'));

    graph.removeNode(nodeId1);

    assert(!graph.hasContentKey('contentKey'));
  });
});