stylelint-test-rule-node is a utility package designed to facilitate the testing of Stylelint rules using Node.js's built-in `node:test` runner. It provides a structured API, `testRule`, for defining test cases that include code samples, expected messages, line/column information, and autofix scenarios. Additionally, `testRuleConfigs` allows for testing rule configuration validity. The package is currently at version 1.1.0 and is actively maintained by the Stylelint team. Releases typically follow major Stylelint versions or as new features for testing are introduced. Its key differentiator is leveraging the native Node.js test runner, avoiding external test frameworks, making it a lightweight and integrated solution for developers already within the Node.js ecosystem, particularly for those contributing to or extending Stylelint.
npm install stylelint-test-rule-nodeVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `testRule` to test a Stylelint plugin that enforces kebab-case class selectors, including autofixing and multiple warnings.
Upgrade Node.js to version 20.19.0 or higher: `nvm install 20.19.0 && nvm use 20.19.0` or update your CI/CD configuration.
Update your `stylelint` dependency to `^17.0.0` or the latest compatible version: `npm install stylelint@^17.0.0`.
Always install `stylelint` alongside `stylelint-test-rule-node` in your dev dependencies: `npm install stylelint-test-rule-node stylelint --save-dev`.
Add the `codeFilename` property to your `accept` or `reject` test cases where relevant, specifying the desired filename string.
Install stylelint as a dev dependency: `npm install stylelint --save-dev`.
Ensure your `package.json` has `"type": "module"` or name your test files with a `.mjs` extension. Run tests with `node --test your-rule.test.js`.
Confirm you are using `import { testRule } from 'stylelint-test-rule-node';` and your environment is set up for ESM.