Skip to content

Commit 19bbee1

Browse files
rockingskierkeplersj
authored andcommitted
feat: add the ability to --fix in interactive mode (#45)
1 parent 7bb6f24 commit 19bbee1

6 files changed

+145
-1
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,22 @@ npx jest
157157

158158
yarn jest
159159
```
160+
161+
## Toggle `--fix` in watch mode
162+
163+
`jest-stylelint-runner` comes with a watch plugin that allows you to toggle the `--fix` value while in watch mode without having to update your configuration.
164+
165+
To use this watch plugin simply add this to your Jest configuration.
166+
167+
```js
168+
{
169+
watchPlugins: ['jest-runner-stylelint/watch-fix'],
170+
}
171+
```
172+
173+
After this run Jest in watch mode and you will see the following line in your watch usage menu.
174+
175+
```
176+
› Press F to override Stylelint --fix.
177+
```
178+
]

src/configOverrides.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class ConfigOverrides {
2+
setFix(fix) {
3+
this.fix = fix;
4+
}
5+
6+
getFix() {
7+
return this.fix;
8+
}
9+
}
10+
11+
const configOverrides = new ConfigOverrides();
12+
13+
module.exports = configOverrides;

src/run.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
const { pass, fail } = require("create-jest-runner");
22
const stylelint = require("stylelint");
3+
const configOverrides = require("./configOverrides");
34

45
module.exports = ({ testPath, config, globalConfig }) => {
56
const start = new Date();
67

78
return stylelint
89
.lint({
910
files: testPath,
10-
formatter: "string"
11+
formatter: "string",
12+
fix: configOverrides.getFix()
1113
})
1214
.then(data => {
1315
if (data.errored) {

src/watchFixPlugin.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { getVersion: getJestVersion } = require("jest");
2+
const configOverrides = require("./configOverrides");
3+
4+
const majorJestVersion = parseInt(getJestVersion().split(".")[0], 10);
5+
6+
/* istanbul ignore if */
7+
if (majorJestVersion < 23) {
8+
throw new Error(`Insufficient Jest version for jest-runner-stylelint watch plugin
9+
10+
Watch plugins are only available in Jest 23.0.0 and above.
11+
Upgrade your version of Jest in order to use it.
12+
`);
13+
}
14+
15+
class StylelintWatchFixPlugin {
16+
constructor({ stdout, config }) {
17+
this._stdout = stdout;
18+
this._key = config.key || "F";
19+
}
20+
21+
async run() {
22+
const fix = configOverrides.getFix();
23+
configOverrides.setFix(!fix);
24+
return true;
25+
}
26+
27+
getUsageInfo() {
28+
const getPrompt = () => {
29+
const fix = configOverrides.getFix();
30+
if (fix === undefined) {
31+
return "override Stylelint --fix";
32+
}
33+
if (!fix) {
34+
return "toggle Stylelint --fix (disabled)";
35+
}
36+
return "toggle Stylelint --fix (enabled)";
37+
};
38+
39+
return {
40+
key: this._key,
41+
prompt: getPrompt()
42+
};
43+
}
44+
}
45+
46+
module.exports = StylelintWatchFixPlugin;

src/watchFixPlugin.test.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
jest.useFakeTimers();
2+
3+
let WatchFixPlugin;
4+
let configOverrides;
5+
6+
describe("watchFixPlugin", () => {
7+
beforeEach(() => {
8+
jest.resetModules();
9+
configOverrides = require("./configOverrides");
10+
WatchFixPlugin = require("./watchFixPlugin");
11+
});
12+
13+
it("shows the correct prompt", async () => {
14+
const stdout = { write: jest.fn() };
15+
const config = {};
16+
const plugin = new WatchFixPlugin({ stdout, config });
17+
expect(plugin.getUsageInfo()).toEqual({
18+
key: "F",
19+
prompt: "override Stylelint --fix"
20+
});
21+
22+
await plugin.run(plugin);
23+
24+
expect(plugin.getUsageInfo()).toEqual({
25+
key: "F",
26+
prompt: "toggle Stylelint --fix (enabled)"
27+
});
28+
29+
await plugin.run(plugin);
30+
31+
expect(plugin.getUsageInfo()).toEqual({
32+
key: "F",
33+
prompt: "toggle Stylelint --fix (disabled)"
34+
});
35+
});
36+
37+
it("overrides the setting in configOverrides after each invocation", async () => {
38+
const stdout = { write: jest.fn() };
39+
const config = {};
40+
const plugin = new WatchFixPlugin({ stdout, config });
41+
expect(configOverrides.getFix()).toBeUndefined();
42+
43+
await plugin.run(plugin);
44+
45+
expect(configOverrides.getFix()).toBe(true);
46+
47+
await plugin.run(plugin);
48+
49+
expect(configOverrides.getFix()).toBe(false);
50+
});
51+
52+
it("can customize the key", () => {
53+
const stdout = { write: jest.fn() };
54+
const config = { key: "z" };
55+
const plugin = new WatchFixPlugin({ stdout, config });
56+
expect(plugin.getUsageInfo()).toEqual({
57+
key: "z",
58+
prompt: "override Stylelint --fix"
59+
});
60+
});
61+
});

watch-fix.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const StylelintWatchFixPlugin = require("./src/watchFixPlugin");
2+
3+
module.exports = StylelintWatchFixPlugin;

0 commit comments

Comments
 (0)