Skip to content

Commit

Permalink
fix: hooks (#195)
Browse files Browse the repository at this point in the history
* fix: use hook `afterCompile` instead of `afterEmit`

* fix: use hook `watchRun` instead of `emit` for lintDirtyModulesOnly

* test: refactor
  • Loading branch information
ricardogobbosouza committed Nov 11, 2019
1 parent 80f7c4f commit 792fe19
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 49 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class StylelintWebpackPlugin {

if (options.lintDirtyModulesOnly) {
const lintDirty = new LintDirtyModulesPlugin(compiler, options);
compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {

/* istanbul ignore next */
compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => {
lintDirty.apply(compilation, callback);
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function linter(options, compiler, callback) {
})
.catch(callback);

compiler.hooks.afterEmit.tapAsync(
compiler.hooks.afterCompile.tapAsync(
'StylelintWebpackPlugin',
(compilation, next) => {
if (warnings.length) {
Expand Down
59 changes: 59 additions & 0 deletions test/LintDirtyModulesPlugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import LintDirtyModulesPlugin from '../src/LintDirtyModulesPlugin';
import linter from '../src/linter';

jest.mock('../src/linter');

describe('lint dirty modules only', () => {
let plugin;
let callback;

beforeAll(() => {
callback = jest.fn();

plugin = new LintDirtyModulesPlugin(null, { files: ['**\\*.s?(c|a)ss'] });
});

beforeEach(() => {
linter.mockRestore();
callback.mockRestore();
});

it('skips linting on initial run', () => {
expect(plugin.isFirstRun).toBe(true);
expect(callback).not.toBeCalled();

plugin.apply({}, callback);

expect(plugin.isFirstRun).toBe(false);
expect(callback).toBeCalledTimes(1);
});

it('linting on change file', () => {
const fileTimestamps = new Map([
['foo/changed.scss', 1],
['bar\\changed.scss', 1],
['new-file.scss'],
]);

plugin.isFirstRun = false;
plugin.prevTimestamps = new Map([
['foo/changed.scss', 2],
['bar\\changed.scss', 2],
]);
plugin.apply({ fileTimestamps }, callback);

expect(linter).toBeCalledTimes(1);
expect(callback).not.toBeCalled();
});

it('not linter if files are not changed', () => {
const fileTimestamps = new Map([['not-changed.scss', 1]]);

plugin.isFirstRun = false;
plugin.prevTimestamps = fileTimestamps;
plugin.apply({ fileTimestamps }, callback);

expect(linter).not.toBeCalled();
expect(callback).toBeCalledTimes(1);
});
});
47 changes: 0 additions & 47 deletions test/lint-dirty-modules-only.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import LintDirtyModulesPlugin from '../src/LintDirtyModulesPlugin';
import linter from '../src/linter';

import pack from './utils/pack';

jest.mock('../src/linter');

describe('lint dirty modules only', () => {
let plugin;
let callback;

beforeAll(() => {
callback = jest.fn();

plugin = new LintDirtyModulesPlugin(null, { files: ['**\\*.s?(c|a)ss'] });
plugin.isFirstRun = false;
});

beforeEach(() => {
linter.mockRestore();
callback.mockRestore();
});

it('skips linting on initial run', (done) => {
const compiler = pack('error', { lintDirtyModulesOnly: true });

Expand All @@ -30,31 +10,4 @@ describe('lint dirty modules only', () => {
done();
});
});

it('linting on change file', () => {
const fileTimestamps = new Map([
['foo/changed.scss', 1],
['bar\\changed.scss', 1],
['new-file.scss'],
]);

plugin.prevTimestamps = new Map([
['foo/changed.scss', 2],
['bar\\changed.scss', 2],
]);
plugin.apply({ fileTimestamps }, callback);

expect(linter).toBeCalledTimes(1);
expect(callback).not.toBeCalled();
});

it('not linter if files are not changed', () => {
const fileTimestamps = new Map([['not-changed.scss', 1]]);

plugin.prevTimestamps = fileTimestamps;
plugin.apply({ fileTimestamps }, callback);

expect(linter).not.toBeCalled();
expect(callback).toBeCalledTimes(1);
});
});

0 comments on commit 792fe19

Please sign in to comment.