or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

browser-utilities.mdconfiguration.mdindex.mdinstrumentation.mdpattern-matching.mdreporting.mdtest-integration.md
tile.json

test-integration.mddocs/

Test Integration

Test framework integration methods for tracking test execution and collecting coverage statistics.

Capabilities

Coverage Setup

Initialize coverage tracking before running tests.

/**
 * Initialize coverage tracking with default stats object
 * Sets up coverage statistics structure and prepares for test execution
 */
function setupCoverage();

Usage:

// Call before running any tests
blanket.setupCoverage();

This method initializes the coverage statistics object with:

{
  stats: {
    suites: 0,     // Number of test suites
    tests: 0,      // Total number of tests
    passes: 0,     // Number of passed tests
    pending: 0,    // Number of pending tests
    failures: 0,   // Number of failed tests
    start: Date,   // Test execution start time
    end: Date      // Test execution end time (set by onTestsDone)
  }
}

Test Lifecycle Methods

Track individual test execution and results.

/**
 * Mark the beginning of a test
 * Increments test counter and pending counter
 */
function onTestStart();

/**
 * Mark test completion with results
 * Updates pass/fail counters based on test results
 * @param total - Total number of assertions in the test
 * @param passed - Number of assertions that passed (or boolean for single assertion)
 */
function onTestDone(total, passed);

/**
 * Mark the beginning of a test suite/module
 * Increments suite counter
 */
function onModuleStart();

/**
 * Mark completion of all tests and trigger reporting
 * Sets end timestamp and triggers coverage report generation
 */
function onTestsDone();

Usage Examples:

// Test lifecycle example
blanket.setupCoverage();

// Starting a test suite
blanket.onModuleStart();

// Running individual tests
blanket.onTestStart();
// ... run test ...
blanket.onTestDone(5, 5); // 5 assertions, all passed

blanket.onTestStart();
// ... run test ...
blanket.onTestDone(3, 2); // 3 assertions, 2 passed

// All tests complete
blanket.onTestsDone(); // Triggers reporting

Test Framework Adapters

Blanket.js provides pre-built adapters for popular test frameworks that automatically call the lifecycle methods.

Mocha Integration

// Mocha adapter (src/adapters/mocha-blanket.js)
// Automatically hooks into Mocha events:
runner.on('start', function() {
  blanket.setupCoverage();
});

runner.on('suite', function() {
  blanket.onModuleStart();
});

runner.on('test', function() {
  blanket.onTestStart();
});

runner.on('test end', function(test) {
  blanket.onTestDone(test.parent.tests.length, test.state === 'passed');
});

runner.on('end', function() {
  blanket.onTestsDone();
});

Usage:

<!-- Browser -->
<script src="blanket.js"></script>
<script src="adapters/mocha-blanket.js"></script>
// Node.js
const blanket = require("blanket");
// Adapter loaded automatically when available

Jasmine Integration

// Jasmine adapter provides similar event hooks
// For Jasmine 1.x and 2.x versions

Usage:

<!-- Browser -->
<script src="blanket.js"></script>
<script src="adapters/jasmine-blanket.js"></script>

QUnit Integration

QUnit integration through browser adapter system.

Custom Test Framework Integration

For custom test frameworks, implement the lifecycle manually:

// Custom test framework integration
function runTests() {
  blanket.setupCoverage();
  
  for (const suite of testSuites) {
    blanket.onModuleStart();
    
    for (const test of suite.tests) {
      blanket.onTestStart();
      
      try {
        test.run();
        blanket.onTestDone(1, true); // Test passed
      } catch (error) {
        blanket.onTestDone(1, false); // Test failed
      }
    }
  }
  
  blanket.onTestsDone(); // Trigger reporting
}

Coverage Data Collection

Coverage Statistics

The test integration methods collect statistics in the coverage data object:

interface CoverageStats {
  suites: number;    // Number of test suites run
  tests: number;     // Total number of individual tests
  passes: number;    // Number of tests that passed
  pending: number;   // Number of tests currently pending
  failures: number;  // Number of tests that failed
  start: Date;       // When test execution started
  end: Date;         // When test execution completed
}

Line Coverage Data

During test execution, instrumented code populates coverage arrays:

// Coverage data structure for each file
{
  "filename.js": {
    source: ["line 1", "line 2", "..."],  // Original source lines
    1: 5,        // Line 1 executed 5 times
    2: 3,        // Line 2 executed 3 times
    3: 0,        // Line 3 never executed
    // ... more line counters
  }
}

Branch Coverage Data

When branchTracking is enabled:

{
  "filename.js": {
    source: ["..."],
    branchData: {
      5: {           // Line number
        0: {         // Branch index
          consequent: {...},  // True branch info
          alternate: {...}    // False branch info
        }
      }
    }
  }
}

Reporting Integration

Automatic Reporting

When onTestsDone() is called:

  1. Browser: Calls blanket.report(coverageData) with configured reporter
  2. Node.js: Calls configured reporter function with coverage data

Custom Reporting

Override the reporting behavior:

// Custom reporter function
blanket.options("reporter", function(coverageData) {
  console.log("Total tests:", coverageData.stats.tests);
  console.log("Pass rate:", 
    (coverageData.stats.passes / coverageData.stats.tests * 100).toFixed(2) + "%");
  
  // Process file coverage
  for (const filename in coverageData.files) {
    const fileData = coverageData.files[filename];
    console.log("File:", filename);
    // ... process coverage data
  }
});

Error Handling

Setup Validation

The lifecycle methods validate that setupCoverage() was called:

function onTestStart() {
  if (!coverageInfo.stats) {
    throw new Error("You must call blanket.setupCoverage() first.");
  }
  // ... continue with test tracking
}

State Management

The integration methods maintain internal state:

  • Track pending tests to ensure accurate counts
  • Handle overlapping test execution
  • Manage suite and test counters properly

Framework-Specific Handling

Each adapter handles framework-specific behaviors:

  • Mocha: Handles async tests and hooks properly
  • Jasmine: Manages spec and suite lifecycle
  • QUnit: Integrates with QUnit's module system

Best Practices

Integration Order

  1. Load Blanket.js
  2. Configure options
  3. Load test framework adapter
  4. Load source files (will be instrumented)
  5. Load test files
  6. Run tests (coverage tracked automatically)

Manual Integration

When creating custom integrations:

  1. Always call setupCoverage() before any tests
  2. Call onModuleStart() for each test suite
  3. Call onTestStart() and onTestDone() for each test
  4. Call onTestsDone() when all tests complete
  5. Handle async tests appropriately

Multiple Test Runs

For multiple test runs in the same session:

// Reset coverage for new test run
blanket.setupCoverage(); // Resets stats
// ... run tests again