Skip to content

Commit 792fe19

Browse files
fix: hooks (#195)
* fix: use hook `afterCompile` instead of `afterEmit` * fix: use hook `watchRun` instead of `emit` for lintDirtyModulesOnly * test: refactor
1 parent 80f7c4f commit 792fe19

File tree

4 files changed

+63
-49
lines changed

4 files changed

+63
-49
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class StylelintWebpackPlugin {
2323

2424
if (options.lintDirtyModulesOnly) {
2525
const lintDirty = new LintDirtyModulesPlugin(compiler, options);
26-
compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {
26+
27+
/* istanbul ignore next */
28+
compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => {
2729
lintDirty.apply(compilation, callback);
2830
});
2931
} else {

src/linter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function linter(options, compiler, callback) {
2121
})
2222
.catch(callback);
2323

24-
compiler.hooks.afterEmit.tapAsync(
24+
compiler.hooks.afterCompile.tapAsync(
2525
'StylelintWebpackPlugin',
2626
(compilation, next) => {
2727
if (warnings.length) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import LintDirtyModulesPlugin from '../src/LintDirtyModulesPlugin';
2+
import linter from '../src/linter';
3+
4+
jest.mock('../src/linter');
5+
6+
describe('lint dirty modules only', () => {
7+
let plugin;
8+
let callback;
9+
10+
beforeAll(() => {
11+
callback = jest.fn();
12+
13+
plugin = new LintDirtyModulesPlugin(null, { files: ['**\\*.s?(c|a)ss'] });
14+
});
15+
16+
beforeEach(() => {
17+
linter.mockRestore();
18+
callback.mockRestore();
19+
});
20+
21+
it('skips linting on initial run', () => {
22+
expect(plugin.isFirstRun).toBe(true);
23+
expect(callback).not.toBeCalled();
24+
25+
plugin.apply({}, callback);
26+
27+
expect(plugin.isFirstRun).toBe(false);
28+
expect(callback).toBeCalledTimes(1);
29+
});
30+
31+
it('linting on change file', () => {
32+
const fileTimestamps = new Map([
33+
['foo/changed.scss', 1],
34+
['bar\\changed.scss', 1],
35+
['new-file.scss'],
36+
]);
37+
38+
plugin.isFirstRun = false;
39+
plugin.prevTimestamps = new Map([
40+
['foo/changed.scss', 2],
41+
['bar\\changed.scss', 2],
42+
]);
43+
plugin.apply({ fileTimestamps }, callback);
44+
45+
expect(linter).toBeCalledTimes(1);
46+
expect(callback).not.toBeCalled();
47+
});
48+
49+
it('not linter if files are not changed', () => {
50+
const fileTimestamps = new Map([['not-changed.scss', 1]]);
51+
52+
plugin.isFirstRun = false;
53+
plugin.prevTimestamps = fileTimestamps;
54+
plugin.apply({ fileTimestamps }, callback);
55+
56+
expect(linter).not.toBeCalled();
57+
expect(callback).toBeCalledTimes(1);
58+
});
59+
});
Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
1-
import LintDirtyModulesPlugin from '../src/LintDirtyModulesPlugin';
2-
import linter from '../src/linter';
3-
41
import pack from './utils/pack';
52

6-
jest.mock('../src/linter');
7-
83
describe('lint dirty modules only', () => {
9-
let plugin;
10-
let callback;
11-
12-
beforeAll(() => {
13-
callback = jest.fn();
14-
15-
plugin = new LintDirtyModulesPlugin(null, { files: ['**\\*.s?(c|a)ss'] });
16-
plugin.isFirstRun = false;
17-
});
18-
19-
beforeEach(() => {
20-
linter.mockRestore();
21-
callback.mockRestore();
22-
});
23-
244
it('skips linting on initial run', (done) => {
255
const compiler = pack('error', { lintDirtyModulesOnly: true });
266

@@ -30,31 +10,4 @@ describe('lint dirty modules only', () => {
3010
done();
3111
});
3212
});
33-
34-
it('linting on change file', () => {
35-
const fileTimestamps = new Map([
36-
['foo/changed.scss', 1],
37-
['bar\\changed.scss', 1],
38-
['new-file.scss'],
39-
]);
40-
41-
plugin.prevTimestamps = new Map([
42-
['foo/changed.scss', 2],
43-
['bar\\changed.scss', 2],
44-
]);
45-
plugin.apply({ fileTimestamps }, callback);
46-
47-
expect(linter).toBeCalledTimes(1);
48-
expect(callback).not.toBeCalled();
49-
});
50-
51-
it('not linter if files are not changed', () => {
52-
const fileTimestamps = new Map([['not-changed.scss', 1]]);
53-
54-
plugin.prevTimestamps = fileTimestamps;
55-
plugin.apply({ fileTimestamps }, callback);
56-
57-
expect(linter).not.toBeCalled();
58-
expect(callback).toBeCalledTimes(1);
59-
});
6013
});

0 commit comments

Comments
 (0)