jest-preset-stylelint 6.1.1 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -6
- package/getTestRuleConfigs.js +68 -0
- package/index.d.ts +23 -0
- package/jest-setup.js +3 -1
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.org/package/jest-preset-stylelint) [](https://github.com/stylelint/jest-preset-stylelint/actions)
|
|
4
4
|
|
|
5
|
-
[Jest](https://
|
|
5
|
+
[Jest](https://jestjs.io/) preset for [Stylelint](https://stylelint.io/) plugins.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -22,14 +22,16 @@ Add the preset to your `jest.config.js` or `jest` field in `package.json`:
|
|
|
22
22
|
}
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
Optionally, you can avoid specifying `plugins` in every schema by defining your own setup file to configure the `testRule`
|
|
25
|
+
Optionally, you can avoid specifying `plugins` in every schema by defining your own setup file to configure the `testRule`/`testRuleConfigs` functions.
|
|
26
|
+
This is useful if you have many tests. There are two additional steps to do this:
|
|
26
27
|
|
|
27
|
-
1. Create `jest.setup.js` in the root of your project. Provide `plugins` option to `getTestRule
|
|
28
|
+
1. Create `jest.setup.js` in the root of your project. Provide `plugins` option to `getTestRule`/`getTestRuleConfigs`:
|
|
28
29
|
|
|
29
30
|
```js
|
|
30
31
|
const { getTestRule } = require("jest-preset-stylelint");
|
|
31
32
|
|
|
32
33
|
global.testRule = getTestRule({ plugins: ["./"] });
|
|
34
|
+
global.testRuleConfigs = getTestRuleConfigs({ plugins: ["./"] });
|
|
33
35
|
```
|
|
34
36
|
|
|
35
37
|
2. Add `jest.setup.js` to your `jest.config.js` or `jest` field in `package.json`:
|
|
@@ -43,7 +45,13 @@ Optionally, you can avoid specifying `plugins` in every schema by defining your
|
|
|
43
45
|
|
|
44
46
|
## Usage
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
This preset exposes the following global functions as a helper.
|
|
49
|
+
|
|
50
|
+
See also the [type definitions](index.d.ts) for more details.
|
|
51
|
+
|
|
52
|
+
### `testRule`
|
|
53
|
+
|
|
54
|
+
The `testRule` function enables you to efficiently test your plugin using a schema.
|
|
47
55
|
|
|
48
56
|
For example, we can test a plugin that enforces and autofixes kebab-case class selectors:
|
|
49
57
|
|
|
@@ -104,9 +112,34 @@ testRule({
|
|
|
104
112
|
});
|
|
105
113
|
```
|
|
106
114
|
|
|
107
|
-
|
|
115
|
+
### `testRuleConfigs`
|
|
116
|
+
|
|
117
|
+
The `testRuleConfigs` function enables you to test invalid configs for a rule.
|
|
118
|
+
|
|
119
|
+
For example:
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
testInvalidRuleConfigs({
|
|
123
|
+
plugins: ["."],
|
|
124
|
+
ruleName,
|
|
125
|
+
|
|
126
|
+
accept: [
|
|
127
|
+
{
|
|
128
|
+
config: "valid"
|
|
129
|
+
}
|
|
130
|
+
],
|
|
108
131
|
|
|
109
|
-
|
|
132
|
+
reject: [
|
|
133
|
+
{
|
|
134
|
+
config: "invalid"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
config: [/invalid/],
|
|
138
|
+
description: "regex is not allowed"
|
|
139
|
+
}
|
|
140
|
+
]
|
|
141
|
+
});
|
|
142
|
+
```
|
|
110
143
|
|
|
111
144
|
## [Changelog](CHANGELOG.md)
|
|
112
145
|
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { inspect } = require('util');
|
|
4
|
+
|
|
5
|
+
/** @type {import('.').getTestRuleConfigs} */
|
|
6
|
+
module.exports = function getTestRuleConfigs(options = {}) {
|
|
7
|
+
return function testRuleConfigs({
|
|
8
|
+
ruleName,
|
|
9
|
+
accept = [],
|
|
10
|
+
reject = [],
|
|
11
|
+
only = false,
|
|
12
|
+
skip = false,
|
|
13
|
+
plugins = options.plugins,
|
|
14
|
+
}) {
|
|
15
|
+
if (accept.length === 0 && reject.length === 0) {
|
|
16
|
+
throw new TypeError('The either "accept" or "reject" property must not be empty');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @type {import('stylelint').lint} */
|
|
20
|
+
let lint;
|
|
21
|
+
|
|
22
|
+
beforeAll(() => {
|
|
23
|
+
// eslint-disable-next-line n/no-unpublished-require
|
|
24
|
+
lint = require('stylelint').lint;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const testGroup = only ? describe.only : skip ? describe.skip : describe;
|
|
28
|
+
|
|
29
|
+
testGroup(`${ruleName} configs`, () => {
|
|
30
|
+
/**
|
|
31
|
+
* @param {import('.').ConfigCase} case
|
|
32
|
+
* @param {(warnings: unknown[]) => void} comparison
|
|
33
|
+
*/
|
|
34
|
+
function testConfig({ config, description, only: onlyTest, skip: skipTest }, comparison) {
|
|
35
|
+
const testFn = onlyTest ? test.only : skipTest ? test.skip : test;
|
|
36
|
+
|
|
37
|
+
testFn(`${description || inspect(config)}`, async () => {
|
|
38
|
+
const lintConfig = {
|
|
39
|
+
plugins,
|
|
40
|
+
rules: { [ruleName]: config },
|
|
41
|
+
};
|
|
42
|
+
const { results } = await lint({ code: '', config: lintConfig });
|
|
43
|
+
|
|
44
|
+
expect(results).toHaveLength(1);
|
|
45
|
+
comparison(results[0].invalidOptionWarnings);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe('accept', () => {
|
|
50
|
+
accept.forEach((c) => {
|
|
51
|
+
testConfig(c, (warnings) => {
|
|
52
|
+
// eslint-disable-next-line jest/no-standalone-expect
|
|
53
|
+
expect(warnings).toEqual([]);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('reject', () => {
|
|
59
|
+
reject.forEach((c) => {
|
|
60
|
+
testConfig(c, (warnings) => {
|
|
61
|
+
// eslint-disable-next-line jest/no-standalone-expect
|
|
62
|
+
expect(warnings).toEqual([{ text: expect.stringMatching(`"${ruleName}"`) }]);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -156,6 +156,29 @@ export type TestRule = (schema: TestSchema) => void;
|
|
|
156
156
|
*/
|
|
157
157
|
export function getTestRule(options?: { plugins?: TestSchema['plugins'] }): TestRule;
|
|
158
158
|
|
|
159
|
+
export type ConfigCase = {
|
|
160
|
+
config: unknown;
|
|
161
|
+
description?: string;
|
|
162
|
+
only?: boolean;
|
|
163
|
+
skip?: boolean;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Test configurations for a rule.
|
|
168
|
+
*/
|
|
169
|
+
export type TestRuleConfigs = (
|
|
170
|
+
schema: Pick<TestSchema, 'ruleName' | 'plugins' | 'only' | 'skip'> & {
|
|
171
|
+
accept?: ConfigCase[];
|
|
172
|
+
reject?: ConfigCase[];
|
|
173
|
+
},
|
|
174
|
+
) => void;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Create a `testRuleConfigs()` function with any specified plugins.
|
|
178
|
+
*/
|
|
179
|
+
export function getTestRuleConfigs(options?: { plugins?: TestSchema['plugins'] }): TestRuleConfigs;
|
|
180
|
+
|
|
159
181
|
declare global {
|
|
160
182
|
var testRule: TestRule;
|
|
183
|
+
var testRuleConfigs: TestRuleConfigs;
|
|
161
184
|
}
|
package/jest-setup.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jest-preset-stylelint",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"description": "Jest preset for Stylelint plugins.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"stylelint",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"main": "index.js",
|
|
15
15
|
"types": "index.d.ts",
|
|
16
16
|
"files": [
|
|
17
|
+
"getTestRuleConfigs.js",
|
|
17
18
|
"getTestRule.js",
|
|
18
19
|
"jest-preset.js",
|
|
19
20
|
"jest-setup.js",
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
"lint:js": "eslint .",
|
|
28
29
|
"lint:md": "remark . --quiet --frail",
|
|
29
30
|
"lint:types": "tsc",
|
|
30
|
-
"release": "np",
|
|
31
|
+
"release": "np --no-release-draft",
|
|
31
32
|
"pretest": "npm run lint",
|
|
32
33
|
"test": "jest",
|
|
33
34
|
"watch": "jest --watch",
|
|
@@ -55,19 +56,23 @@
|
|
|
55
56
|
"@stylelint/remark-preset"
|
|
56
57
|
]
|
|
57
58
|
},
|
|
59
|
+
"jest": {
|
|
60
|
+
"preset": "./jest-preset.js",
|
|
61
|
+
"testRegex": ".*\\.test\\.js$"
|
|
62
|
+
},
|
|
58
63
|
"devDependencies": {
|
|
59
64
|
"@stylelint/prettier-config": "^3.0.0",
|
|
60
65
|
"@stylelint/remark-preset": "^4.0.0",
|
|
61
66
|
"@types/jest": "^29.5.3",
|
|
62
|
-
"eslint": "^8.
|
|
63
|
-
"eslint-config-stylelint": "^19.
|
|
67
|
+
"eslint": "^8.46.0",
|
|
68
|
+
"eslint-config-stylelint": "^19.1.0",
|
|
64
69
|
"eslint-plugin-jest": "^27.2.3",
|
|
65
70
|
"husky": "^8.0.3",
|
|
66
|
-
"jest": "^29.6.
|
|
71
|
+
"jest": "^29.6.2",
|
|
67
72
|
"lint-staged": "^13.2.3",
|
|
68
73
|
"np": "^8.0.4",
|
|
69
74
|
"npm-run-all": "^4.1.5",
|
|
70
|
-
"prettier": "^3.0.
|
|
75
|
+
"prettier": "^3.0.1",
|
|
71
76
|
"remark-cli": "^11.0.0",
|
|
72
77
|
"stylelint": "^15.10.2",
|
|
73
78
|
"typescript": "^5.1.6"
|