Skip to content

Commit 1fe927a

Browse files
fix: fail on error and fail on warning
1 parent a653827 commit 1fe927a

File tree

6 files changed

+37
-38
lines changed

6 files changed

+37
-38
lines changed

src/index.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,34 @@ class ESLintWebpackPlugin {
150150
});
151151

152152
// await and interpret results
153-
compilation.hooks.additionalAssets.tapPromise(this.key, processResults);
154-
155-
async function processResults() {
156-
const { errors, warnings, generateReportAsset } = await report();
157-
158-
if (warnings && !options.failOnWarning) {
159-
// @ts-ignore
160-
compilation.warnings.push(warnings);
161-
} else if (warnings) {
162-
// @ts-ignore
163-
compilation.errors.push(warnings);
164-
}
165-
166-
if (errors && !options.failOnError) {
167-
// @ts-ignore
168-
compilation.warnings.push(errors);
169-
} else if (errors) {
170-
// @ts-ignore
171-
compilation.errors.push(errors);
172-
}
173-
174-
if (generateReportAsset) await generateReportAsset(compilation);
175-
}
153+
compilation.hooks.additionalAssets.tapAsync(
154+
this.key,
155+
async function (callback) {
156+
const { errors, warnings, generateReportAsset } = await report();
157+
158+
if (warnings) {
159+
// @ts-ignore
160+
compilation.warnings.push(warnings);
161+
}
162+
163+
if (errors) {
164+
// @ts-ignore
165+
compilation.errors.push(errors);
166+
}
167+
168+
if (generateReportAsset) {
169+
await generateReportAsset(compilation);
170+
}
171+
172+
if (warnings && options.failOnWarning) {
173+
callback(warnings);
174+
} else if (errors && options.failOnError) {
175+
callback(errors);
176+
} else {
177+
callback();
178+
}
179+
},
180+
);
176181
});
177182
}
178183

test/fail-on-error.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ describe('fail on error', () => {
44
it('should emits errors', async () => {
55
const compiler = pack('error', { failOnError: true });
66

7-
const stats = await compiler.runAsync();
8-
expect(stats.hasErrors()).toBe(true);
7+
await expect(compiler.runAsync()).rejects.toThrow('error');
98
});
109

1110
it('should emit warnings when disabled', async () => {
1211
const compiler = pack('error', { failOnError: false });
1312

1413
const stats = await compiler.runAsync();
15-
expect(stats.hasErrors()).toBe(false);
16-
expect(stats.hasWarnings()).toBe(true);
14+
expect(stats.hasErrors()).toBe(true);
1715
});
1816

1917
it('should correctly identifies a success', async () => {
2018
const compiler = pack('good', { failOnError: true });
2119

22-
await compiler.runAsync();
20+
const stats = await compiler.runAsync();
21+
expect(stats.hasErrors()).toBe(false);
2322
});
2423
});

test/fail-on-warning.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ describe('fail on warning', () => {
44
it('should emits errors', async () => {
55
const compiler = pack('warn', { failOnWarning: true });
66

7-
const stats = await compiler.runAsync();
8-
expect(stats.hasErrors()).toBe(true);
7+
await expect(compiler.runAsync()).rejects.toThrow('warning');
98
});
109

1110
it('should correctly identifies a success', async () => {

test/lint-dirty-modules-only.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ describe('lint dirty modules only', () => {
4141
expect(stats.hasErrors()).toBe(true);
4242
const { errors } = stats.compilation;
4343
expect(errors.length).toBe(1);
44-
const [{ message }] = errors;
45-
expect(message).toEqual(expect.stringMatching('no-unused-vars'));
44+
expect(stats.compilation.errors[0].message).toEqual(expect.stringMatching('no-unused-vars'));
4645
done();
4746
}
4847
});

test/multiple-instances.test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ describe('multiple instances', () => {
4848
},
4949
);
5050

51-
const stats = await compiler.runAsync();
52-
expect(stats.hasWarnings()).toBe(false);
53-
expect(stats.hasErrors()).toBe(true);
51+
await expect(compiler.runAsync()).rejects.toThrow();
5452
});
5553

5654
it('should fail on second instance', async () => {
@@ -73,8 +71,6 @@ describe('multiple instances', () => {
7371
},
7472
);
7573

76-
const stats = await compiler.runAsync();
77-
expect(stats.hasWarnings()).toBe(false);
78-
expect(stats.hasErrors()).toBe(true);
74+
await expect(compiler.runAsync()).rejects.toThrow();
7975
});
8076
});

test/utils/conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default (entry, pluginConf = {}, webpackConf = {}) => {
2121
ignore: false,
2222
// TODO: update tests to run both states: test.each([[{threads: false}], [{threads: true}]])('it should...', async ({threads}) => {...})
2323
threads: true,
24+
failOnError: false,
2425
...pluginConf,
2526
}),
2627
],

0 commit comments

Comments
 (0)