Registry / testing / postcss-parser-tests

postcss-parser-tests

JSON →
library8.9.0jsnpmunverified

PostCSS Parser Tests is a utility package providing a comprehensive suite of base tests designed for any CSS parser, not exclusively those within the PostCSS ecosystem. It includes 24 CSS files covering extreme and edge cases of the CSS specification, alongside integration tests derived from popular website styles to evaluate real-world CSS parsing robustness. The current stable version, 8.9.0, continues to enhance test coverage, with recent updates adding more rules for semicolon cases. This package primarily facilitates the development and validation of CSS parsers by offering structured test cases and helper methods like `cases.each` for iterating through tests and `cases.real` for integration testing. It aims to standardize parser testing against a broad spectrum of valid and challenging CSS inputs, ensuring robustness and spec compliance across different parsing implementations.

npm install postcss-parser-tests
INSTALL
IMPORT
SIG · POSTCSS-PARSER-TES
P
postcss-parser-tests
testingjavascriptv8.9.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

cases
✓ import cases from 'postcss-parser-tests';
✗ const cases = require('postcss-parser-tests');
While CommonJS `require` is shown in old examples, ESM `import` is the standard for modern Node.js and browser environments. The package ships with TypeScript types.
each
✓ import cases from 'postcss-parser-tests'; cases.each((name, css, ideal) => { /* ... */ });
`each` is a method on the default exported `cases` object, not a direct named export.
real
✓ import cases from 'postcss-parser-tests'; cases.real(css => { /* ... */ });
`real` is a method on the default exported `cases` object, used for integration tests, not a direct named export.
Cases
✓ import type { Cases } from 'postcss-parser-tests';
Type import for the main exported object, useful for TypeScript users.

This quickstart demonstrates how to iterate through all standard CSS test cases using `cases.each` and run integration tests with `cases.real`, simulating a test runner and a custom parser. It shows how to use the provided CSS strings and compare parsed ASTs with the 'ideal' PostCSS AST JSON.

import cases from 'postcss-parser-tests'; // Assuming you have a CSS parser function called `parse` and a test runner like Mocha/Jest. // This example uses a simplified `parse` and `expect` for demonstration. const mockParse = (css: string, options?: { from: string }) => { // In a real scenario, this would be your actual CSS parser // For example, from 'postcss': return postcss.parse(css, options); console.log(`Parsing: ${options?.from || 'unknown'}`); return { type: 'root', nodes: [], toJSON: () => ({ /* simplified AST */ }) }; }; const mockExpect = (value: any) => ({ toEqual: (expected: any) => { console.log(`Comparing parsed output with ideal AST... ${JSON.stringify(value) === JSON.stringify(expected) ? 'Success' : 'Failure'}`); // In a real test, this would throw an error on mismatch } }); describe('PostCSS Parser Tests', () => { cases.each((name, css, ideal) => { it('parses ' + name, () => { const root = mockParse(css, { from: name }); const json = cases.jsonify(root); mockExpect(json).toEqual(ideal); }); }); // Example of integration tests with a mock parser it('runs integration tests', async () => { const results = []; await cases.real(css => { // Your parser should take CSS string and return a PostCSS Result object (or similar). // For this example, we just return the input CSS. results.push({ input: css, output: css }); return { css: css, toResult: () => ({ css: css, map: { annotation: false } }) }; }); console.log(`Completed ${results.length} integration tests.`); mockExpect(results.length).toEqual(20); // Example assertion, actual count varies. }); });
Debug
Known issues
breakingPostCSS 8 (and by extension, packages in its ecosystem like `postcss-parser-tests`) dropped support for Node.js versions 6.x, 8.x, 11.x, and 13.x. Ensure your environment uses a supported Node.js version (e.g., Node.js 12+).
fix
Upgrade your Node.js runtime to a supported LTS version (e.g., 18 or 20).
affects: >=8.0.0
gotchaThis package provides test data and utility methods, not a CSS parser itself. Users must provide their own `parse` function (typically from a PostCSS-compatible parser) when using `cases.each` or a full parser/stringify callback for `cases.real`.
fix
Ensure your test setup includes an actual CSS parser library (e.g., `postcss`) and passes its parsing function to `cases.each` or `cases.real` callbacks.
affects: >=1.0.0
gotchaWhile the package exposes test data for any CSS parser, the `ideal` AST provided by `cases.each` and the expected output for `cases.jsonify` are specifically in the PostCSS AST format. Non-PostCSS parsers will need to convert their AST to match PostCSS's structure for direct comparison, or compare parsed CSS strings directly as suggested by the README.
fix
For non-PostCSS parsers, either implement a conversion layer to PostCSS AST format before comparing with `ideal`, or compare the input CSS string directly with the stringified output of your parser.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: parse is not a function
The user did not provide a `parse` function (their CSS parser) to be used within the `cases.each` callback.
fix
Define and pass your CSS parsing function to the `cases.each` callback, or ensure it's available in the scope where `cases.each` is called. E.g., `const root = yourParser(css, { from: name });`
ReferenceError: expect is not defined
The test environment (e.g., Mocha, Jest) or an assertion library (e.g., Chai) has not been correctly set up or imported, so `expect` is not globally available or imported.
fix
Install and configure a test runner (e.g., Jest, Mocha) and an assertion library (e.g., Jest's built-in assertions, Chai) as per their respective documentation.
Upgrade
Version history
8.9.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
10
Resources
postcss-parser-tests — npm install postcss-parser-tests · libregistry