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

tessl/npm-blanket

Seamless JavaScript code coverage library for both browser and Node.js environments

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/blanket@1.2.x

To install, run

npx @tessl/cli install tessl/npm-blanket@1.2.0

index.mddocs/

Blanket.js

Blanket.js is a seamless JavaScript code coverage library that aims to be easy to install, use, and understand. It works by loading source files, parsing and instrumenting the code using Esprima and node-falafel to add code tracking lines, then connecting to test runner hooks to output coverage details after tests complete.

Package Information

  • Package Name: blanket
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install blanket
  • Environments: Browser and Node.js

Core Imports

Node.js (CommonJS):

const blanket = require("blanket");

Browser (global variable):

<script src="blanket.js"></script>
<script>
// blanket is now available globally
</script>

Basic Usage

Node.js with Mocha

const blanket = require("blanket");

// Configure coverage options
blanket.options({
  filter: "src/",
  antifilter: "test/"
});

// Setup coverage tracking
blanket.setupCoverage();

// Your tests run here...
// Blanket will automatically instrument and track coverage

// Generate report after tests complete
blanket.onTestsDone();

Browser with QUnit

<script src="blanket.js"></script>
<script>
// Configure before loading source files
blanket.options({
  filter: "//src/",
  reporter: "./path/to/reporter.js"
});
</script>

<!-- Your source files will be automatically instrumented -->
<script src="src/myfile.js" data-cover></script>

<!-- Test files -->
<script src="test/tests.js"></script>

Architecture

Blanket.js operates in a three-step process:

  1. File Loading: Intercepts file loading through require.extensions (Node.js) or script tags (browser)
  2. Code Instrumentation: Parses JavaScript using Esprima and instruments with tracking code via node-falafel
  3. Coverage Reporting: Connects to test runner hooks to collect and output coverage statistics

Key components:

  • Core Engine: Main instrumentation and tracking logic
  • Node.js Adapter: require.extensions integration and pattern matching
  • Browser Utilities: Script loading, CORS handling, and manual file loading
  • Test Framework Adapters: Integration with Mocha, Jasmine, QUnit, YUI Test
  • Reporters: LCOV and JSON output formatters
  • File Loaders: Support for CoffeeScript, CJSX, and other transpiled languages

Capabilities

Configuration

Core configuration methods for setting up coverage options, filters, and behaviors.

/**
 * Get or set configuration options
 * @param key - Option key (string) or options object
 * @param value - Option value (when key is string)
 * @returns Option value (when used as getter)
 */
function options(key, value);

Configuration

Code Instrumentation

JavaScript code instrumentation engine that adds coverage tracking to source files.

/**
 * Instrument JavaScript code for coverage tracking
 * @param config - Configuration object
 * @param config.inputFile - Source code string to instrument
 * @param config.inputFileName - Filename for the source code
 * @param next - Optional callback function receiving instrumented code
 * @returns Instrumented code (when used synchronously)
 */
function instrument(config, next);

/**
 * Synchronous code instrumentation
 * @param config - Configuration object
 * @param next - Optional callback function
 * @returns Instrumented code string
 */
function instrumentSync(config, next);

Code Instrumentation

Test Integration

Methods for integrating with test frameworks and tracking test execution.

/**
 * Initialize coverage tracking with default stats
 */
function setupCoverage();

/**
 * Mark the beginning of a test
 */
function onTestStart();

/**
 * Mark test completion with results
 * @param total - Total number of assertions
 * @param passed - Number of passed assertions
 */
function onTestDone(total, passed);

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

/**
 * Mark completion of all tests and trigger reporting
 */
function onTestsDone();

Test Integration

Pattern Matching (Node.js)

File pattern matching utilities for determining which files to instrument.

/**
 * Match filename against pattern
 * @param filename - File path to test
 * @param pattern - Pattern to match (string, array, regex, or function)
 * @returns Whether filename matches pattern
 */
function matchPattern(filename, pattern);

/**
 * Normalize backslashes in file paths
 * @param str - File path string
 * @returns Normalized path with forward slashes
 */
function normalizeBackslashes(str);

Pattern Matching

Browser Utilities

Browser-specific functionality for script loading, CORS handling, and manual file selection.

/**
 * Track files being loaded for coverage
 * @param filename - File being loaded (optional)
 * @param done - Mark file as loaded (optional)
 */
function requiringFile(filename, done);

/**
 * Check if all required files have been loaded
 * @returns Whether all files are loaded
 */
function requireFilesLoaded();

/**
 * Setup coverage before test runner starts
 * @param opts - Configuration options
 * @param opts.callback - Completion callback
 * @param opts.coverage - Enable/disable coverage (default: true)
 */
function beforeStartTestRunner(opts);

Browser Utilities

Reporting

Coverage report generation and custom reporter support.

/**
 * Generate coverage report using configured reporter
 * @param coverage_data - Coverage statistics and file data
 */
function report(coverage_data);

/**
 * Get coverage variable name
 * @returns Coverage variable name string
 */
function getCovVar();

Reporting

Types

// Configuration options object
interface BlanketOptions {
  filter?: string | string[] | RegExp | Function;
  antifilter?: string | string[] | RegExp | Function;
  reporter?: string | Function;
  adapter?: string;
  loader?: string | string[];
  ignoreScriptError?: boolean;
  autoStart?: boolean;
  branchTracking?: boolean;
  debug?: boolean;
  engineOnly?: boolean;
  customVariable?: string;
  timeout?: number;
  ecmaVersion?: number;
  reporter_options?: object;
}

// Instrumentation configuration
interface InstrumentConfig {
  inputFile: string;
  inputFileName: string;
}

// Test runner setup options
interface TestRunnerOptions {
  callback?: Function;
  coverage?: boolean;
  condition?: Function;
  bindEvent?: Function;
  checkRequirejs?: boolean;
}

// Coverage data structure
interface CoverageData {
  files: {
    [filename: string]: {
      source: string[];
      [lineNumber: number]: number;
      branchData?: object;
    };
  };
  stats: {
    suites: number;
    tests: number;
    passes: number;
    pending: number;
    failures: number;
    start: Date;
    end: Date;
  };
}