Registry / testing / stylelint-test-rule-node

stylelint-test-rule-node

JSON →
library1.1.0jsnpmunverified

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-node
INSTALL
IMPORT
SIG · STYLELINT-TEST-RUL
S
stylelint-test-rule-node
testingjavascriptv1.1.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.

testRule
import { testRule } from 'stylelint-test-rule-node';
const { testRule } = require('stylelint-test-rule-node');
Primarily designed for ESM usage. CommonJS `require` might lead to issues with Node.js `node:test` runner setup, which favors ESM.
testRuleConfigs
import { testRuleConfigs } from 'stylelint-test-rule-node';
import testRuleConfigs from 'stylelint-test-rule-node';
This is a named export, not a default export. Ensure curly braces are used.
TestRuleOptions
import type { TestRuleOptions } from 'stylelint-test-rule-node';
For TypeScript users, import types separately using `import type` for clarity and better tree-shaking.

This example demonstrates how to use `testRule` to test a Stylelint plugin that enforces kebab-case class selectors, including autofixing and multiple warnings.

import { testRule } from "stylelint-test-rule-node"; import myPlugin from "./my-plugin.js"; const plugins = [myPlugin]; const { ruleName, rule: { messages } } = myPlugin; testRule({ plugins, ruleName, config: [true, { type: "kebab" }], fix: true, accept: [ { code: ".class {}", description: "simple class selector" }, { code: ".my-class {}", description: "kebab class selector" } ], reject: [ { code: ".myClass {}", fixed: ".my-class {}", description: "camel case class selector", message: messages.expected(), line: 1, column: 1, endLine: 1, endColumn: 8 }, { code: ".MyClass,\n.MyOtherClass {}", fixed: ".my-class,\n.my-other-class {}", description: "two pascal class selectors in a selector list", warnings: [ { message: messages.expected(), line: 1, column: 1, endLine: 1, endColumn: 8 }, { message: messages.expected(), line: 2, column: 1, endLine: 2, endColumn: 13 } ] } ] }); // To run this test: node --test my-plugin.test.js
Debug
Known issues
breakingSupport for Node.js versions less than 20.19.0 was removed. Ensure your development environment uses a compatible Node.js version.
fix
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.
affects: >=1.0.0
breakingThe peer dependency on `stylelint` less than `17.0.0` was removed. Your project must use `stylelint` version 17.0.0 or higher.
fix
Update your `stylelint` dependency to `^17.0.0` or the latest compatible version: `npm install stylelint@^17.0.0`.
affects: >=1.0.0
gotchaWhile the `stylelint` peer dependency is now optional, it is still required for tests to function correctly. Marking it optional primarily helps in specific monorepo setups or when `stylelint` might be provided indirectly.
fix
Always install `stylelint` alongside `stylelint-test-rule-node` in your dev dependencies: `npm install stylelint-test-rule-node stylelint --save-dev`.
affects: >=1.1.0
gotchaThe `codeFilename` option was added to `accept` and `reject` cases. If your tests rely on `codeFilename` for specific scenarios (e.g., path-dependent rules), ensure you update your test cases.
fix
Add the `codeFilename` property to your `accept` or `reject` test cases where relevant, specifying the desired filename string.
affects: >=0.3.0
Errors
Common errors & fixes
Error: Cannot find module 'stylelint' from '...' or peer dependency 'stylelint' not found
The `stylelint` package is a peer dependency and must be installed explicitly, even though it's marked as optional since v1.1.0.
fix
Install stylelint as a dev dependency: `npm install stylelint --save-dev`.
SyntaxError: Cannot use import statement outside a module
Your test file or project is being run as CommonJS, but `stylelint-test-rule-node` is an ESM-first package and expects an ESM environment, especially when using `node --test`.
fix
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`.
TypeError: testRule is not a function
This usually indicates an incorrect import, such as trying to `require` a named ESM export or a default import where a named import is expected.
fix
Confirm you are using `import { testRule } from 'stylelint-test-rule-node';` and your environment is set up for ESM.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
stylelintoptionalRequired for running the rule tests; specified as a peer dependency.
Agent activity
9 hits · last 30 days
node
8
Resources
stylelint-test-rule-node — npm install stylelint-test-rule-node · libregistry