Registry / testing / test262-parser

test262-parser

JSON →
library2.2.0jsnpmunverified

test262-parser is a JavaScript library engineered to parse and extract structured information from test files adhering to the test262 format, as defined by TC39 for ECMAScript conformance testing. As of version 2.2.0, the package provides a stable API for programmatically accessing the various components of a test file. It can efficiently extract YAML frontmatter (which contains test attributes like description and author), copyright messages, the asynchronous status of the test, and the main executable test body from a given input, whether it's a raw string of file contents or a file object. The library offers both a direct parsing function (`parseFile`) suitable for individual test contents and a transform stream interface for processing multiple files in a streaming fashion. Its primary differentiator lies in its specialized focus on the intricacies of the test262 file format, delivering a highly structured representation of the frontmatter and core content, making it an essential utility for tools involved in the analysis, transformation, or execution of ECMAScript conformance tests.

npm install test262-parser
INSTALL
IMPORT
SIG · TEST262-PARSER
T
test262-parser
testingjavascriptv2.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseFile
const { parseFile } = require('test262-parser');
import { parseFile } from 'test262-parser';
For version 2.x, `test262-parser` is a CommonJS module. Use `require` for named exports. Direct ESM imports will not work without transpilation or specific Node.js loader configurations.
extractYAML
const { extractYAML } = require('test262-parser');
import { extractYAML } from 'test262-parser';
For version 2.x, `test262-parser` is a CommonJS module. Use `require` for named exports. Direct ESM imports will not work without transpilation or specific Node.js loader configurations.
default (Transform Stream Factory)
const test262Parser = require('test262-parser');
import test262Parser from 'test262-parser';
The default export for v2.x is a CommonJS module that acts as a factory for a transform stream. Use `require` to access it. Attempting a default ESM import will result in errors.

This quickstart demonstrates how to use `parseFile` from `test262-parser` to extract structured data from a mock test262 file. It showcases both the behavior of passing an object (which gets mutated) and passing a raw string (which returns a new parsed object), including the extraction of attributes, async status, copyright, and the trimmed test body.

const fs = require('fs'); const path = require('path'); const { parseFile } = require('test262-parser'); // Create a dummy test262 file for demonstration const dummyTestPath = path.join(__dirname, 'dummy-test.js'); const dummyTestContent = `/*---\ndocument: true\ndescription: A simple test file\n---*/\n // Copyright 2026 Acme Corp. All rights reserved.\n// This code is under the BSD license.\n function test() {\n assert.sameValue(1 + 1, 2, 'Addition works');\n assert.throws(TypeError, () => { new NonExistent(); });\n}\n\ntest();\n`; fs.writeFileSync(dummyTestPath, dummyTestContent, 'utf8'); // Read and parse the dummy test file const rawTest = fs.readFileSync(dummyTestPath, 'utf8'); console.log('--- Parsing via File Object (input mutated) ---'); // Option 1: Pass in a file object (will be mutated) const fileObject = { file: 'dummy-test.js', contents: rawTest }; parseFile(fileObject); console.log('Attributes:', fileObject.attrs); console.log('Async:', fileObject.async); console.log('Copyright (truncated):', fileObject.copyright ? fileObject.copyright.substring(0, 50) + '...' : 'N/A'); console.log('Test Body (truncated):', fileObject.contents ? fileObject.contents.substring(0, 50) + '...' : 'N/A'); console.log('\n--- Parsing via Raw String (returns new object) ---'); // Option 2: Parse test contents directly (returns a new object) const parsedFile = parseFile(rawTest); console.log('File Name:', parsedFile.file); // Will be '<unknown>' console.log('Attributes:', parsedFile.attrs); console.log('Async:', parsedFile.async); console.log('Copyright (truncated):', parsedFile.copyright ? parsedFile.copyright.substring(0, 50) + '...' : 'N/A'); console.log('Test Body (truncated):', parsedFile.contents ? parsedFile.contents.substring(0, 50) + '...' : 'N/A'); // Clean up dummy file fs.unlinkSync(dummyTestPath);
Debug
Known issues
gotchaWhen `parseFile` is invoked with an object containing `file` and `contents` properties, the input object itself is mutated. New properties like `attrs`, `async`, and `copyright` are added directly to the passed object, and `contents` is modified.
fix
If object mutation is undesired, pass the raw string contents directly to `parseFile`. This will return a new parsed object without modifying the original input. Alternatively, clone the input object before passing it.
affects: >=2.0.0
gotchaThe `contents` property of the parsed output (or mutated input object) will contain only the core JavaScript test body, with the copyright header and YAML frontmatter completely removed. If the original, complete file content is needed, it must be retained separately before parsing.
fix
Store the original `rawTest` string in a separate variable if you need to access the full unparsed content alongside the parsed components generated by `test262-parser`.
affects: >=2.0.0
gotchaVersion 2.x of `test262-parser` is a CommonJS module. Using ECMAScript Module (ESM) `import` syntax (e.g., `import { parseFile } from 'test262-parser';`) will result in `TypeError: require(...) is not a function` or similar errors in standard Node.js environments.
fix
Always use `require()` syntax for importing `test262-parser` and its named exports in Node.js environments with version 2.x. For example, `const { parseFile } = require('test262-parser');`.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: test262Parser.parseFile is not a function
This error typically occurs when attempting to use ESM `import` syntax for a CommonJS module, or when incorrectly destructuring named exports from a `require()` call (e.g., `const test262Parser = require('test262-parser'); test262Parser.parseFile();`).
fix
Ensure you are using the correct CommonJS `require()` pattern for named exports: `const { parseFile } = require('test262-parser');`.
YAMLException: bad indentation of a mapping entry at line X, column Y
The YAML frontmatter block in the test262 file being parsed contains syntax errors, such as incorrect indentation, missing colons, or invalid key-value pairs, which prevents the parser from successfully processing the metadata.
fix
Carefully review the YAML frontmatter in your test file, paying close attention to indentation, proper key-value syntax, and adherence to the YAML specification. Online YAML validators or linters can help identify issues.
TypeError: Cannot read properties of undefined (reading 'contents')
This error occurs when `parseFile` is called with an argument that is `null`, `undefined`, or not a string/object as expected, leading to a subsequent attempt to access properties on an invalid value.
fix
Ensure that the argument passed to `parseFile` is either a string containing the test file's complete contents or an object with both `file` and `contents` string properties.
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
test262-parser — npm install test262-parser · libregistry