Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch plugin for toggling --fix #53

Merged
merged 10 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ module.exports = {
yarn jest
```

## Toggle `--fix` in watch mode

`jest-eslint-runner` comes with a watch plugin that allows you to toggle the `--fix` value while in watch mode without having to update your configuration.

![fix](https://user-images.githubusercontent.com/574806/46181271-93205080-c279-11e8-8d73-b4c5e11086c4.gif)

To use this watch plugin simply add this to your Jest configuration.

```js
{
watchPlugins: ['jest-runner-eslint/watch-fix'],
}
```

After this run Jest in watch mode and you will see the following line in your watch usage menu

```
› Press F to override ESLint --fix.
```

## Options

This project uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig), so you can provide config via:
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
'jest-watch-select-projects',
'./watch-fix',
],
projects: [
{
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jest-runner-eslint",
"version": "0.6.0",
"main": "build/index.js",
"main": "build/runner",
"author": "Rogelio Guzman <[email protected]>",
"description": "An experimental ESLint runner for Jest",
"license": "MIT",
Expand All @@ -23,6 +23,7 @@
"format": "prettier --write \"**/*.js\""
},
"dependencies": {
"chalk": "^2.4.1",
"cosmiconfig": "^5.0.0",
"create-jest-runner": "^0.4.1",
"eslint": "^5.6.0",
Expand Down
5 changes: 0 additions & 5 deletions src/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');

const runESLintRunnerWithMockedEngine = options => {
jest.resetModules();
jest.doMock('../utils/getLocalESLint', () => () => {
jest.doMock('../../utils/getLocalESLint', () => () => {
return {
CLIEngine: class {
isPathIgnored(file) {
Expand All @@ -24,7 +24,7 @@ const runESLintRunnerWithMockedEngine = options => {
});
const runESLint = require('../runESLint');

return runESLint(options.runESLint);
return runESLint({ extraOptions: {}, ...options.runESLint });
};

it('Requires the config setupTestFrameworkScriptFile when specified', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/runner/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { createJestRunner } = require('create-jest-runner');
const configOverrides = require('../utils/configOverrides');

const runner = createJestRunner(require.resolve('./runESLint'), {
getExtraOptions: () => ({ fix: configOverrides.getFix() }),
});

module.exports = runner;
20 changes: 11 additions & 9 deletions src/runESLint.js → src/runner/runESLint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { pass, fail, skip } = require('create-jest-runner');
const getLocalESLint = require('./utils/getLocalESLint');
const getESLintOptions = require('./utils/getESLintOptions');
const getLocalESLint = require('../utils/getLocalESLint');
const getESLintOptions = require('../utils/getESLintOptions');

const getComputedFixValue = ({ fix, quiet, fixDryRun }) => {
if (fix || fixDryRun) {
Expand All @@ -9,7 +9,7 @@ const getComputedFixValue = ({ fix, quiet, fixDryRun }) => {
return undefined;
};

const runESLint = ({ testPath, config }) => {
const runESLint = ({ testPath, config, extraOptions }) => {
const start = Date.now();

if (config.setupTestFrameworkScriptFile) {
Expand All @@ -18,12 +18,14 @@ const runESLint = ({ testPath, config }) => {
}

const { CLIEngine } = getLocalESLint(config);
const { cliOptions } = getESLintOptions(config);
const cli = new CLIEngine(
Object.assign({}, cliOptions, {
fix: getComputedFixValue(cliOptions),
}),
);
const { cliOptions: baseCliOptions } = getESLintOptions(config);
const cliOptions = {
...baseCliOptions,
fix: getComputedFixValue(baseCliOptions),
...extraOptions,
};

const cli = new CLIEngine(cliOptions);

if (cli.isPathIgnored(testPath)) {
const end = Date.now();
Expand Down
13 changes: 13 additions & 0 deletions src/utils/configOverrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ConfigOverrides {
setFix(fix) {
this.fix = fix;
}

getFix() {
return this.fix;
}
}

const configOverrides = new ConfigOverrides();

module.exports = configOverrides;
66 changes: 66 additions & 0 deletions src/watchFixPlugin/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-disable global-require */
const chalk = require('chalk');

jest.doMock('chalk', () => new chalk.constructor({ enabled: false }));

jest.useFakeTimers();

let WatchFixPlugin;
let configOverrides;

describe('watchFixPlugin', () => {
beforeEach(() => {
jest.resetModules();
configOverrides = require('../../utils/configOverrides');
WatchFixPlugin = require('../');
});

it('shows the correct prompt', async () => {
const stdout = { write: jest.fn() };
const config = {};
const plugin = new WatchFixPlugin({ stdout, config });
expect(plugin.getUsageInfo()).toEqual({
key: 'F',
prompt: 'override ESLint --fix',
});

await plugin.run(plugin);

expect(plugin.getUsageInfo()).toEqual({
key: 'F',
prompt: 'toggle ESLint --fix (enabled)',
});

await plugin.run(plugin);

expect(plugin.getUsageInfo()).toEqual({
key: 'F',
prompt: 'toggle ESLint --fix (disabled)',
});
});

it('overrides the setting in configOverrides after each invocation', async () => {
const stdout = { write: jest.fn() };
const config = {};
const plugin = new WatchFixPlugin({ stdout, config });
expect(configOverrides.getFix()).toBeUndefined();

await plugin.run(plugin);

expect(configOverrides.getFix()).toBe(true);

await plugin.run(plugin);

expect(configOverrides.getFix()).toBe(false);
});

it('can customize the key', () => {
const stdout = { write: jest.fn() };
const config = { key: 'z' };
const plugin = new WatchFixPlugin({ stdout, config });
expect(plugin.getUsageInfo()).toEqual({
key: 'z',
prompt: 'override ESLint --fix',
});
});
});
36 changes: 36 additions & 0 deletions src/watchFixPlugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const chalk = require('chalk');
const configOverrides = require('../utils/configOverrides');

class ESLintWatchFixPlugin {
constructor({ stdout, config }) {
this._stdout = stdout;
this._key = config.key || 'F';
}

// eslint-disable-next-line class-methods-use-this
ljharb marked this conversation as resolved.
Show resolved Hide resolved
async run() {
const fix = configOverrides.getFix();
configOverrides.setFix(!fix);
return true;
}

getUsageInfo() {
const getPrompt = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is defined at module level, then it should be a bit faster.

const fix = configOverrides.getFix();
if (fix === undefined) {
return 'override ESLint --fix';
}
if (!fix) {
return `toggle ESLint --fix ${chalk.italic('(disabled)')}`;
}
return `toggle ESLint --fix ${chalk.italic('(enabled)')}`;
};

return {
key: this._key,
prompt: getPrompt(),
};
}
}

module.exports = ESLintWatchFixPlugin;
3 changes: 3 additions & 0 deletions watch-fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const ESLintWatchFixPlugin = require('./build/watchFixPlugin');

module.exports = ESLintWatchFixPlugin;