jest-preset-stylelint 6.1.1 → 6.3.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/getTestRule.js +5 -3
- package/getTestRuleConfigs.js +71 -0
- package/index.d.ts +34 -1
- package/jest-setup.js +3 -1
- package/package.json +17 -12
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
|
|
package/getTestRule.js
CHANGED
|
@@ -10,12 +10,14 @@ const util = require('util');
|
|
|
10
10
|
/** @type {import('.').getTestRule} */
|
|
11
11
|
module.exports = function getTestRule(options = {}) {
|
|
12
12
|
return function testRule(schema) {
|
|
13
|
+
const loadLint =
|
|
14
|
+
schema.loadLint || options.loadLint || (() => Promise.resolve(require('stylelint').lint)); // eslint-disable-line n/no-unpublished-require -- Avoid auto-install of `stylelint` peer dependency.
|
|
15
|
+
|
|
13
16
|
/** @type {import('stylelint').lint} */
|
|
14
17
|
let lint;
|
|
15
18
|
|
|
16
|
-
beforeAll(() => {
|
|
17
|
-
|
|
18
|
-
lint = require('stylelint').lint;
|
|
19
|
+
beforeAll(async () => {
|
|
20
|
+
lint = await loadLint();
|
|
19
21
|
});
|
|
20
22
|
|
|
21
23
|
describe(`${schema.ruleName}`, () => {
|
|
@@ -0,0 +1,71 @@
|
|
|
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
|
+
loadLint: schemaLoadLint,
|
|
15
|
+
}) {
|
|
16
|
+
if (accept.length === 0 && reject.length === 0) {
|
|
17
|
+
throw new TypeError('The either "accept" or "reject" property must not be empty');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const loadLint =
|
|
21
|
+
schemaLoadLint || options.loadLint || (() => Promise.resolve(require('stylelint').lint)); // eslint-disable-line n/no-unpublished-require -- Avoid auto-install of `stylelint` peer dependency.
|
|
22
|
+
|
|
23
|
+
/** @type {import('stylelint').lint} */
|
|
24
|
+
let lint;
|
|
25
|
+
|
|
26
|
+
beforeAll(async () => {
|
|
27
|
+
lint = await loadLint();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const testGroup = only ? describe.only : skip ? describe.skip : describe;
|
|
31
|
+
|
|
32
|
+
testGroup(`${ruleName} configs`, () => {
|
|
33
|
+
/**
|
|
34
|
+
* @param {import('.').ConfigCase} case
|
|
35
|
+
* @param {(warnings: unknown[]) => void} comparison
|
|
36
|
+
*/
|
|
37
|
+
function testConfig({ config, description, only: onlyTest, skip: skipTest }, comparison) {
|
|
38
|
+
const testFn = onlyTest ? test.only : skipTest ? test.skip : test;
|
|
39
|
+
|
|
40
|
+
testFn(`${description || inspect(config)}`, async () => {
|
|
41
|
+
const lintConfig = {
|
|
42
|
+
plugins,
|
|
43
|
+
rules: { [ruleName]: config },
|
|
44
|
+
};
|
|
45
|
+
const { results } = await lint({ code: '', config: lintConfig });
|
|
46
|
+
|
|
47
|
+
expect(results).toHaveLength(1);
|
|
48
|
+
comparison(results[0].invalidOptionWarnings);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe('accept', () => {
|
|
53
|
+
accept.forEach((c) => {
|
|
54
|
+
testConfig(c, (warnings) => {
|
|
55
|
+
// eslint-disable-next-line jest/no-standalone-expect
|
|
56
|
+
expect(warnings).toEqual([]);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('reject', () => {
|
|
62
|
+
reject.forEach((c) => {
|
|
63
|
+
testConfig(c, (warnings) => {
|
|
64
|
+
// eslint-disable-next-line jest/no-standalone-expect
|
|
65
|
+
expect(warnings).toEqual([{ text: expect.stringMatching(`"${ruleName}"`) }]);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -144,6 +144,16 @@ export type TestSchema = {
|
|
|
144
144
|
* @see https://jestjs.io/docs/api#testskipname-fn
|
|
145
145
|
*/
|
|
146
146
|
skip?: boolean;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Loads the lint function.
|
|
150
|
+
*/
|
|
151
|
+
loadLint?: () => Promise<import('stylelint').lint>;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
type GetTestRuleOptions = {
|
|
155
|
+
plugins?: TestSchema['plugins'];
|
|
156
|
+
loadLint?: TestSchema['loadLint'];
|
|
147
157
|
};
|
|
148
158
|
|
|
149
159
|
/**
|
|
@@ -154,8 +164,31 @@ export type TestRule = (schema: TestSchema) => void;
|
|
|
154
164
|
/**
|
|
155
165
|
* Create a `testRule()` function with any specified plugins.
|
|
156
166
|
*/
|
|
157
|
-
export function getTestRule(options?:
|
|
167
|
+
export function getTestRule(options?: GetTestRuleOptions): TestRule;
|
|
168
|
+
|
|
169
|
+
export type ConfigCase = {
|
|
170
|
+
config: unknown;
|
|
171
|
+
description?: string;
|
|
172
|
+
only?: boolean;
|
|
173
|
+
skip?: boolean;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Test configurations for a rule.
|
|
178
|
+
*/
|
|
179
|
+
export type TestRuleConfigs = (
|
|
180
|
+
schema: Pick<TestSchema, 'ruleName' | 'plugins' | 'only' | 'skip' | 'loadLint'> & {
|
|
181
|
+
accept?: ConfigCase[];
|
|
182
|
+
reject?: ConfigCase[];
|
|
183
|
+
},
|
|
184
|
+
) => void;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Create a `testRuleConfigs()` function with any specified plugins.
|
|
188
|
+
*/
|
|
189
|
+
export function getTestRuleConfigs(options?: GetTestRuleOptions): TestRuleConfigs;
|
|
158
190
|
|
|
159
191
|
declare global {
|
|
160
192
|
var testRule: TestRule;
|
|
193
|
+
var testRuleConfigs: TestRuleConfigs;
|
|
161
194
|
}
|
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.3.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,22 +56,26 @@
|
|
|
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
|
-
"@types/jest": "^29.5.
|
|
62
|
-
"eslint": "^8.
|
|
63
|
-
"eslint-config-stylelint": "^
|
|
64
|
-
"eslint-plugin-jest": "^27.2
|
|
66
|
+
"@types/jest": "^29.5.5",
|
|
67
|
+
"eslint": "^8.50.0",
|
|
68
|
+
"eslint-config-stylelint": "^20.0.0",
|
|
69
|
+
"eslint-plugin-jest": "^27.4.2",
|
|
65
70
|
"husky": "^8.0.3",
|
|
66
|
-
"jest": "^29.
|
|
67
|
-
"lint-staged": "^
|
|
71
|
+
"jest": "^29.7.0",
|
|
72
|
+
"lint-staged": "^14.0.1",
|
|
68
73
|
"np": "^8.0.4",
|
|
69
74
|
"npm-run-all": "^4.1.5",
|
|
70
|
-
"prettier": "^3.0.
|
|
71
|
-
"remark-cli": "^
|
|
72
|
-
"stylelint": "^15.10.
|
|
73
|
-
"typescript": "^5.
|
|
75
|
+
"prettier": "^3.0.3",
|
|
76
|
+
"remark-cli": "^12.0.0",
|
|
77
|
+
"stylelint": "^15.10.3",
|
|
78
|
+
"typescript": "^5.2.2"
|
|
74
79
|
},
|
|
75
80
|
"peerDependencies": {
|
|
76
81
|
"jest": "^29.0.2"
|