Registry / testing / test262-parser-tests

test262-parser-tests

JSON →
library0.0.5jsnpmunverified

The `test262-parser-tests` package provides a comprehensive, standardized set of test cases specifically designed for evaluating ECMAScript parsers against the official specification. Currently at version 0.0.5, this project is maintained by TC39 and serves as a foundational resource for parser implementers. It categorizes tests into `pass/` for syntactically valid programs, `early/` for programs that adhere to grammar but trigger early errors, and `fail/` for syntactically invalid programs. A unique feature is `pass-explicit/`, offering explicitly parenthesized versions of `pass/` tests for robust equivalence checking of Abstract Syntax Tree (AST) generation. Crucially, these tests are intended solely for parsing validation and are explicitly *not* designed for runtime evaluation; most would throw errors if executed. Its release cadence is generally tied to updates and amendments of the ECMAScript specification, incorporating new syntax or parsing rules as they evolve.

npm install test262-parser-tests
INSTALL
IMPORT
SIG · TEST262-PARSER-TES
T
test262-parser-tests
testingjavascriptv0.0.5
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

This quickstart demonstrates how to programmatically access and validate test cases from the `test262-parser-tests` package using Node.js `fs` module and `assert`. It showcases parsing 'pass' tests (and comparing with 'pass-explicit'), expecting errors for 'fail' tests, and distinguishing between initial parsing and early error detection for 'early' tests. It uses `shift-parser` as a placeholder for any ECMAScript parser you wish to test; ensure you install `shift-parser` if you plan to run this specific example.

const fs = require('fs'); const path = require('path'); const assert = require('assert'); // IMPORTANT: This example uses 'shift-parser' as an example parser. // You must install it separately: `npm install shift-parser` const shift = require('shift-parser'); // Resolve the path to the installed test262-parser-tests package const packagePath = path.dirname(require.resolve('test262-parser-tests/package.json')); const passDir = path.join(packagePath, 'pass'); const passExplicitDir = path.join(packagePath, 'pass-explicit'); const failDir = path.join(packagePath, 'fail'); const earlyDir = path.join(packagePath, 'early'); // A wrapper function for your parser. Adapt this to your actual parser's API. function parseCode(src, { isModule, earlyErrors }) { try { return (isModule ? shift.parseModule : shift.parseScript)(src, { earlyErrors }); } catch (e) { throw e; // Re-throw for assert.throws to catch } } // Example: Exclusions for specific tests that might behave unexpectedly // (Typically found in the package's `LICENSES.md` or test runner configs) const passExcludes = []; const failExcludes = []; const earlyExcludes = []; // Truncated for brevity; original list is long console.log('--- Running pass tests (parsing and equivalence check) ---'); fs.readdirSync(passDir).filter(f => !passExcludes.includes(f) && (f.endsWith('.js') || f.endsWith('.module.js'))).forEach(f => { const filePath = path.join(passDir, f); const explicitFilePath = path.join(passExplicitDir, f); const isModule = f.includes('.module.js'); let firstTree, secondTree; assert.doesNotThrow(() => { firstTree = parseCode(fs.readFileSync(filePath, 'utf8'), { isModule, earlyErrors: true }); }, `PASS: Failed to parse valid test '${f}'`); assert.doesNotThrow(() => { secondTree = parseCode(fs.readFileSync(explicitFilePath, 'utf8'), { isModule, earlyErrors: true }); }, `PASS-EXPLICIT: Failed to parse valid explicit test '${f}'`); // Note: For a robust check, a custom AST comparison ignoring whitespace/comments is ideal. // assert.deepStrictEqual(firstTree, secondTree, `AST mismatch between '${f}' and its explicit version`); // console.log(`✓ Parsed and compared '${f}'`); }); console.log('All pass tests processed (parsing only). '); console.log('--- Running fail tests (expecting parse errors) ---'); fs.readdirSync(failDir).filter(f => !failExcludes.includes(f) && (f.endsWith('.js') || f.endsWith('.module.js'))).forEach(f => { const filePath = path.join(failDir, f); const isModule = f.includes('.module.js'); assert.throws(() => { parseCode(fs.readFileSync(filePath, 'utf8'), { isModule, earlyErrors: false }); }, `FAIL: Did not throw error for invalid test '${f}'`); // console.log(`✓ Failed (as expected) for '${f}'`); }); console.log('All fail tests processed. '); console.log('--- Running early error tests (should parse, then error on early error detection) ---'); fs.readdirSync(earlyDir).filter(f => !earlyExcludes.includes(f) && (f.endsWith('.js') || f.endsWith('.module.js'))).forEach(f => { const filePath = path.join(earlyDir, f); const isModule = f.includes('.module.js'); // The file should parse successfully, but contain an early error assert.doesNotThrow(() => { parseCode(fs.readFileSync(filePath, 'utf8'), { isModule, earlyErrors: false }); }, `EARLY: Failed to parse test '${f}' (should parse successfully for initial AST)`); // When earlyErrors are enabled, parsing should throw an error assert.throws(() => { parseCode(fs.readFileSync(filePath, 'utf8'), { isModule, earlyErrors: true }); }, `EARLY: Did not throw expected early error for test '${f}'`); // console.log(`✓ Processed early error test '${f}'`); }); console.log('All early error tests processed. '); console.log('Parser testing complete!');
Debug
Known issues
gotchaThe tests in this package are strictly for parser validation and are NOT intended for runtime evaluation. Most test files would throw a runtime error if executed, as their purpose is solely to check syntactic correctness and AST generation.
fix
Do not attempt to execute these test files directly. Implement a parser that consumes their content and generates an Abstract Syntax Tree (AST), then validate the AST or parser's error reporting.
affects: >=0.0.1
gotchaUnderstand the distinction between 'early/' and 'fail/' test categories. 'early/' tests describe programs that match the ECMAScript grammar but trigger early errors (e.g., duplicate parameter names). 'fail/' tests describe programs that do not conform to the ECMAScript grammar at all. Parsers that do not distinguish between these should treat 'early/' tests similarly to 'fail/' tests.
fix
Ensure your parser correctly identifies early errors after successful grammatical parsing, or, if your parser doesn't differentiate, ensure it reports an error for both 'early/' and 'fail/' cases.
affects: >=0.0.1
gotchaWhen contributing new tests, adhere to the specified normalization and explicit version generation process. New `pass/` and `early/` tests must be normalized using `normalize-parser-test`, and `pass/` tests require an `pass-explicit/` version generated by `make-explicit.js` (Node.js v5+).
fix
Follow the contribution guidelines in the package's README, utilizing `normalize-parser-test` and `make-explicit.js` to ensure consistency and correctness of new test cases.
affects: >=0.0.1
Errors
Common errors & fixes
SyntaxError: Unexpected token
Attempting to parse a file from the `fail/` directory, which contains syntactically invalid ECMAScript programs that *should* cause a parse error.
fix
This is often the expected behavior for `fail/` tests. Ensure your parser correctly reports a syntax error for these files. If your parser *doesn't* throw, it's a bug in your parser.
AssertionError: Expected no exception but an exception was thrown
Your parser failed to correctly parse a file from the `pass/` or `pass-explicit/` directories, which contain syntactically valid ECMAScript programs.
fix
There is a bug in your parser where it incorrectly rejects valid ECMAScript syntax. Debug your parser to identify why it's failing on a correct input.
AssertionError: Values not strictly deep-equal
When comparing the ASTs of a `pass/` test file and its corresponding `pass-explicit/` version, the generated trees differ beyond acceptable transformations (whitespace, comments, grouping parentheses, semicolons).
fix
Review your parser's AST generation logic. Ensure it produces equivalent ASTs for syntactically equivalent code, disregarding non-semantic differences. A custom AST comparison function might be necessary instead of `assert.deepStrictEqual` for precise equivalence.
Upgrade
Version history
0.0.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
test262-parser-tests — npm install test262-parser-tests · libregistry