Test262 Test Suite Stream API
test262-stream offers a Node.js API for programmatically traversing and consuming the Test262 JavaScript conformance test suite. It enables developers to stream individual test cases, their complete source code (including any 'includes' files), associated metadata, and copyright information. This library is particularly valuable for JavaScript engine developers, transpiler authors, or anyone constructing tools that require automated execution or analysis of Test262 tests. The current stable version is 1.4.0. Given its specialized function of parsing a specific external test suite, its release cadence is likely synchronized with updates or structural modifications within the Test262 repository itself, rather than frequent independent feature releases. Its primary differentiator lies in offering stream-based, granular access to Test262 content, abstracting away the complexities of file system traversal and frontmatter parsing, which distinguishes it from manual file reading or lower-level Test262 utilities.
Common errors
-
Error: provided Test262 directory does not contain 'test' subdirectory
cause The `test262Dir` provided to the `TestStream` constructor is not the root of a valid Test262 repository clone, or the 'test' subdirectory is missing/misplaced.fixEnsure `test262Dir` points to the exact root of your Test262 repository clone, which typically contains 'test/', 'harness/', and other standard Test262 directories. Double-check the path for typos. -
Error: Unsupported Test262 version
cause The Test262 repository you are streaming has a version that is not officially supported by this `test262-stream` library version.fixUpdate your `test262-stream` package to the latest version, or, as a temporary workaround (with caution), provide the `acceptVersion` option with the version string of your Test262 clone. However, updating the library is the recommended approach for compatibility.
Warnings
- gotcha Using the `acceptVersion` option with an outdated or incorrect version can lead to the stream emitting invalid tests that may not accurately reflect the Test262 specification. It's generally safer to update the library to ensure compatibility.
- gotcha The `test262Dir` path must point to the root directory of a cloned Test262 repository. An incorrect path will result in errors as the library won't be able to locate tests or harness files.
- gotcha Setting `omitRuntime: true` will prevent the insertion of necessary execution code (like assertion functions and 'include' files) into `test.contents`. This is useful for static analysis but will make the `contents` property unsuitable for direct execution.
- deprecated The usage example shows `var TestStream = require('test262-stream');`. While functional in CommonJS environments, modern Node.js development strongly recommends ESM `import` syntax.
Install
-
npm install test262-stream -
yarn add test262-stream -
pnpm add test262-stream
Imports
- TestStream
const TestStream = require('test262-stream');import TestStream from 'test262-stream';
- Test262Stream
import TestStream from 'test262-stream';
- stream events
for await (const test of stream) { ... }stream.on('data', function(test) { ... }); stream.on('end', function() { ... }); stream.on('error', function(err) { ... });
Quickstart
import TestStream from 'test262-stream';
import path from 'path';
// Replace with your actual path to the Test262 repository
const test262Dir = process.env.TEST262_DIR || '/path/to/test262';
const stream = new TestStream(test262Dir, {
includesDir: path.join(test262Dir, 'harness'),
paths: ['test/built-ins/eval', 'test/language/statements/empty/S12.3_A1.js'],
omitRuntime: true,
acceptVersion: '2.0.0'
});
stream.on('data', function(test) {
console.log(`File: ${test.file}`);
console.log(`Scenario: ${test.scenario}`);
// console.log(`Contents:\n${test.contents}`); // Uncomment for full contents
// console.log(`Attributes: ${JSON.stringify(test.attrs, null, 2)}`);
// console.log(`Copyright: ${test.copyright}`);
// console.log(`Insertion Index: ${test.insertionIndex}`);
});
stream.on('end', function() {
console.log('No further tests.');
});
stream.on('error', function(err) {
console.error('Something went wrong:', err.message);
process.exit(1);
});