Test Build Result Utility

1.1.2 · active · verified Sun Apr 19

The `test-build-result` package provides a utility for comparing the output of a build process against a set of expected files or directories. This is crucial for integration testing, ensuring that build tools, compilers, or bundlers produce consistent and correct artifacts over time. It offers features for specifying actual and expected file paths, handling file content comparisons, and reporting discrepancies. As of version 1.1.2, the package has been updated with features like `testExists` to verify the presence of specific files and the ability to automatically generate expected directories if they do not exist, streamlining the setup for initial tests. The package maintains an active release cadence, with several minor updates recently, indicating ongoing maintenance and responsiveness to user needs. Its primary differentiator lies in its focused approach to asserting the correctness of filesystem-based build outputs, contrasting with more general-purpose assertion libraries.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates comparing a generated build output directory (`dist_actual`) with an expected output directory (`dist_expected`) using `testBuildResult`, showcasing how to identify discrepancies in build artifacts.

import { testBuildResult } from 'test-build-result';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';

// Mock directories for demonstration
const actualDirPath = './dist_actual';
const expectedDirPath = './dist_expected';

async function setupMockFiles() {
  await fs.mkdir(actualDirPath, { recursive: true });
  await fs.mkdir(expectedDirPath, { recursive: true });
  await fs.writeFile(path.join(actualDirPath, 'index.js'), 'console.log("Hello from actual!");');
  await fs.writeFile(path.join(actualDirPath, 'style.css'), 'body { color: blue; }');
  await fs.writeFile(path.join(expectedDirPath, 'index.js'), 'console.log("Hello from actual!");');
  await fs.writeFile(path.join(expectedDirPath, 'style.css'), 'body { color: red; }'); // Intentional difference
}

async function cleanupMockFiles() {
  await fs.rm(actualDirPath, { recursive: true, force: true });
  await fs.rm(expectedDirPath, { recursive: true, force: true });
}

async function runTest() {
  await cleanupMockFiles(); // Ensure clean slate
  await setupMockFiles();

  try {
    console.log('Running build result test...');
    await testBuildResult(actualDirPath, expectedDirPath, {
      // Optional: replace content during comparison if dynamic parts exist
      // replaceContent: { 'dynamic_timestamp': 'fixed_timestamp' }
    });
    console.log('Build result matches expected output.');
  } catch (error) {
    if (error instanceof Error) {
      console.error('Build result mismatch detected:', error.message);
      // In a real test, you'd throw or assert here
    } else {
      console.error('An unexpected error occurred:', error);
    }
  } finally {
    await cleanupMockFiles();
  }
}

runTest();

view raw JSON →