Test framework integration methods for tracking test execution and collecting coverage statistics.
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)
}
}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 reportingBlanket.js provides pre-built adapters for popular test frameworks that automatically call the lifecycle methods.
// 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 adapter provides similar event hooks
// For Jasmine 1.x and 2.x versionsUsage:
<!-- Browser -->
<script src="blanket.js"></script>
<script src="adapters/jasmine-blanket.js"></script>QUnit integration through browser adapter system.
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
}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
}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
}
}When branchTracking is enabled:
{
"filename.js": {
source: ["..."],
branchData: {
5: { // Line number
0: { // Branch index
consequent: {...}, // True branch info
alternate: {...} // False branch info
}
}
}
}
}When onTestsDone() is called:
blanket.report(coverageData) with configured reporterOverride 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
}
});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
}The integration methods maintain internal state:
Each adapter handles framework-specific behaviors:
When creating custom integrations:
setupCoverage() before any testsonModuleStart() for each test suiteonTestStart() and onTestDone() for each testonTestsDone() when all tests completeFor multiple test runs in the same session:
// Reset coverage for new test run
blanket.setupCoverage(); // Resets stats
// ... run tests again