Skip to content

Commit fdb0ab2

Browse files
chore: improve unit tests feedback loop by making them asynchronous (#344)
1 parent e0d70bb commit fdb0ab2

21 files changed

+219
-357
lines changed

test/context.test.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@ import { join } from 'path';
33
import pack from './utils/pack';
44

55
describe('context', () => {
6-
it('absolute', (done) => {
6+
it('absolute', async () => {
77
const compiler = pack('good', {
88
context: join(__dirname, 'fixtures/good'),
99
});
10-
11-
compiler.run((err, stats) => {
12-
expect(stats.hasWarnings()).toBe(false);
13-
expect(stats.hasErrors()).toBe(false);
14-
done();
15-
});
10+
const stats = await compiler.runAsync();
11+
expect(stats.hasWarnings()).toBe(false);
12+
expect(stats.hasErrors()).toBe(false);
1613
});
1714

18-
it('relative', (done) => {
15+
it('relative', async () => {
1916
const compiler = pack('good', { context: '../good/' });
20-
21-
compiler.run((err, stats) => {
22-
expect(stats.hasWarnings()).toBe(false);
23-
expect(stats.hasErrors()).toBe(false);
24-
done();
25-
});
17+
const stats = await compiler.runAsync();
18+
expect(stats.hasWarnings()).toBe(false);
19+
expect(stats.hasErrors()).toBe(false);
2620
});
2721
});

test/emit-error.test.js

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,39 @@
11
import pack from './utils/pack';
22

33
describe('emit error', () => {
4-
it('should not emit errors if emitError is false', (done) => {
4+
it('should not emit errors if emitError is false', async () => {
55
const compiler = pack('error', { emitError: false });
6-
7-
compiler.run((err, stats) => {
8-
expect(err).toBeNull();
9-
expect(stats.hasErrors()).toBe(false);
10-
done();
11-
});
6+
const stats = await compiler.runAsync();
7+
expect(stats.hasErrors()).toBe(false);
128
});
139

14-
it('should emit errors if emitError is undefined', (done) => {
10+
it('should emit errors if emitError is undefined', async () => {
1511
const compiler = pack('error');
16-
17-
compiler.run((err, stats) => {
18-
expect(err).toBeNull();
19-
expect(stats.hasErrors()).toBe(true);
20-
done();
21-
});
12+
const stats = await compiler.runAsync();
13+
expect(stats.hasErrors()).toBe(true);
2214
});
2315

24-
it('should emit errors if emitError is true', (done) => {
16+
it('should emit errors if emitError is true', async () => {
2517
const compiler = pack('error', { emitError: true });
26-
27-
compiler.run((err, stats) => {
28-
expect(err).toBeNull();
29-
expect(stats.hasErrors()).toBe(true);
30-
done();
31-
});
18+
const stats = await compiler.runAsync();
19+
expect(stats.hasErrors()).toBe(true);
3220
});
3321

34-
it('should emit errors, but not warnings if emitError is true and emitWarning is false', (done) => {
22+
it('should emit errors, but not warnings if emitError is true and emitWarning is false', async () => {
3523
const compiler = pack('full-of-problems', {
3624
emitError: true,
3725
emitWarning: false,
3826
});
3927

40-
compiler.run((err, stats) => {
41-
expect(err).toBeNull();
42-
expect(stats.hasWarnings()).toBe(false);
43-
expect(stats.hasErrors()).toBe(true);
44-
done();
45-
});
28+
const stats = await compiler.runAsync();
29+
expect(stats.hasWarnings()).toBe(false);
30+
expect(stats.hasErrors()).toBe(true);
4631
});
4732

48-
it('should emit errors and warnings if emitError is true and emitWarning is undefined', (done) => {
33+
it('should emit errors and warnings if emitError is true and emitWarning is undefined', async () => {
4934
const compiler = pack('full-of-problems', { emitError: true });
50-
51-
compiler.run((err, stats) => {
52-
expect(err).toBeNull();
53-
expect(stats.hasWarnings()).toBe(true);
54-
expect(stats.hasErrors()).toBe(true);
55-
done();
56-
});
35+
const stats = await compiler.runAsync();
36+
expect(stats.hasWarnings()).toBe(true);
37+
expect(stats.hasErrors()).toBe(true);
5738
});
5839
});

test/emit-warning.test.js

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,38 @@
11
import pack from './utils/pack';
22

33
describe('emit warning', () => {
4-
it('should not emit warnings if emitWarning is false', (done) => {
4+
it('should not emit warnings if emitWarning is false', async () => {
55
const compiler = pack('warning', { emitWarning: false });
6-
7-
compiler.run((err, stats) => {
8-
expect(err).toBeNull();
9-
expect(stats.hasWarnings()).toBe(false);
10-
done();
11-
});
6+
const stats = await compiler.runAsync();
7+
expect(stats.hasWarnings()).toBe(false);
128
});
139

14-
it('should emit warnings if emitWarning is undefined', (done) => {
10+
it('should emit warnings if emitWarning is undefined', async () => {
1511
const compiler = pack('warning');
16-
17-
compiler.run((err, stats) => {
18-
expect(err).toBeNull();
19-
expect(stats.hasWarnings()).toBe(true);
20-
done();
21-
});
12+
const stats = await compiler.runAsync();
13+
expect(stats.hasWarnings()).toBe(true);
2214
});
2315

24-
it('should emit warnings if emitWarning is true', (done) => {
16+
it('should emit warnings if emitWarning is true', async () => {
2517
const compiler = pack('warning', { emitWarning: true });
26-
27-
compiler.run((err, stats) => {
28-
expect(err).toBeNull();
29-
expect(stats.hasWarnings()).toBe(true);
30-
done();
31-
});
18+
const stats = await compiler.runAsync();
19+
expect(stats.hasWarnings()).toBe(true);
3220
});
3321

34-
it('should emit warnings, but not warnings if emitWarning is true and emitError is false', (done) => {
22+
it('should emit warnings, but not warnings if emitWarning is true and emitError is false', async () => {
3523
const compiler = pack('full-of-problems', {
3624
emitWarning: true,
3725
emitError: false,
3826
});
39-
40-
compiler.run((err, stats) => {
41-
expect(err).toBeNull();
42-
expect(stats.hasWarnings()).toBe(true);
43-
expect(stats.hasErrors()).toBe(false);
44-
done();
45-
});
27+
const stats = await compiler.runAsync();
28+
expect(stats.hasWarnings()).toBe(true);
29+
expect(stats.hasErrors()).toBe(false);
4630
});
4731

48-
it('should emit warnings and errors if emitWarning is true and emitError is undefined', (done) => {
32+
it('should emit warnings and errors if emitWarning is true and emitError is undefined', async () => {
4933
const compiler = pack('full-of-problems', { emitWarning: true });
50-
51-
compiler.run((err, stats) => {
52-
expect(err).toBeNull();
53-
expect(stats.hasWarnings()).toBe(true);
54-
expect(stats.hasErrors()).toBe(true);
55-
done();
56-
});
34+
const stats = await compiler.runAsync();
35+
expect(stats.hasWarnings()).toBe(true);
36+
expect(stats.hasErrors()).toBe(true);
5737
});
5838
});

test/empty.test.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import { join } from 'path';
2-
3-
import webpack from 'webpack';
4-
51
import StylelintWebpackPlugin from '../src';
62

7-
describe('empty', () => {
8-
it('no error when no files matching', (done) => {
9-
const compiler = webpack({
10-
context: join(__dirname, 'fixtures', 'empty'),
11-
mode: 'development',
12-
entry: './index',
13-
plugins: [new StylelintWebpackPlugin()],
14-
});
3+
import pack from './utils/pack';
154

16-
compiler.run((err, stats) => {
17-
expect(stats.hasWarnings()).toBe(false);
18-
expect(stats.hasErrors()).toBe(false);
19-
done();
20-
});
5+
describe('empty', () => {
6+
it('no error when no files matching', async () => {
7+
const compiler = pack(
8+
'empty',
9+
{},
10+
{
11+
plugins: [new StylelintWebpackPlugin()],
12+
},
13+
);
14+
const stats = await compiler.runAsync();
15+
expect(stats.hasWarnings()).toBe(false);
16+
expect(stats.hasErrors()).toBe(false);
2117
});
2218
});

test/error.test.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,21 @@ describe('error', () => {
55
jest.restoreAllMocks();
66
});
77

8-
it('should return error if file is bad', (done) => {
8+
it('should return error if file is bad', async () => {
99
const compiler = pack('error');
10-
11-
compiler.run((err, stats) => {
12-
expect(err).toBeNull();
13-
expect(stats.hasWarnings()).toBe(false);
14-
expect(stats.hasErrors()).toBe(true);
15-
done();
16-
});
10+
const stats = await compiler.runAsync();
11+
expect(stats.hasWarnings()).toBe(false);
12+
expect(stats.hasErrors()).toBe(true);
1713
});
1814

19-
it('should propagate stylelint exceptions as errors', (done) => {
15+
it('should propagate stylelint exceptions as errors', async () => {
2016
jest.mock('stylelint', () => {
2117
throw new Error('Oh no!');
2218
});
2319

2420
const compiler = pack('good');
25-
26-
compiler.run((err, stats) => {
27-
expect(err).toBeNull();
28-
expect(stats.hasWarnings()).toBe(false);
29-
expect(stats.hasErrors()).toBe(true);
30-
done();
31-
});
21+
const stats = await compiler.runAsync();
22+
expect(stats.hasWarnings()).toBe(false);
23+
expect(stats.hasErrors()).toBe(true);
3224
});
3325
});

test/exclude.test.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
11
import pack from './utils/pack';
22

33
describe('exclude', () => {
4-
it('should exclude with globs', (done) => {
4+
it('should exclude with globs', async () => {
55
const compiler = pack('exclude', { exclude: ['*test*'] });
6-
7-
compiler.run((err, stats) => {
8-
expect(err).toBeNull();
9-
expect(stats.hasWarnings()).toBe(false);
10-
expect(stats.hasErrors()).toBe(false);
11-
done();
12-
});
6+
const stats = await compiler.runAsync();
7+
expect(stats.hasWarnings()).toBe(false);
8+
expect(stats.hasErrors()).toBe(false);
139
});
1410

15-
it('should exclude files', (done) => {
11+
it('should exclude files', async () => {
1612
const compiler = pack('exclude', { exclude: ['test.scss'] });
17-
18-
compiler.run((err, stats) => {
19-
expect(err).toBeNull();
20-
expect(stats.hasWarnings()).toBe(false);
21-
expect(stats.hasErrors()).toBe(false);
22-
done();
23-
});
13+
const stats = await compiler.runAsync();
14+
expect(stats.hasWarnings()).toBe(false);
15+
expect(stats.hasErrors()).toBe(false);
2416
});
2517

26-
it('should exclude folders', (done) => {
18+
it('should exclude folders', async () => {
2719
const compiler = pack('exclude-folder', { exclude: ['folder'] });
28-
29-
compiler.run((err, stats) => {
30-
expect(err).toBeNull();
31-
expect(stats.hasWarnings()).toBe(false);
32-
expect(stats.hasErrors()).toBe(false);
33-
done();
34-
});
20+
const stats = await compiler.runAsync();
21+
expect(stats.hasWarnings()).toBe(false);
22+
expect(stats.hasErrors()).toBe(false);
3523
});
3624
});

test/fail-on-config.test.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ import { join } from 'path';
33
import pack from './utils/pack';
44

55
describe('fail on config', () => {
6-
it('fails when .stylelintrc is not a proper format', (done) => {
6+
it('fails when .stylelintrc is not a proper format', async () => {
77
const configFile = join(__dirname, '.badstylelintrc');
88
const compiler = pack('error', { configFile });
9-
10-
compiler.run((err, stats) => {
11-
const { errors } = stats.compilation;
12-
expect(stats.hasWarnings()).toBe(false);
13-
expect(stats.hasErrors()).toBe(true);
14-
expect(errors).toHaveLength(1);
15-
done();
16-
});
9+
const stats = await compiler.runAsync();
10+
const { errors } = stats.compilation;
11+
expect(stats.hasWarnings()).toBe(false);
12+
expect(stats.hasErrors()).toBe(true);
13+
expect(errors).toHaveLength(1);
1714
});
1815
});

test/fail-on-error.test.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
import pack from './utils/pack';
22

33
describe('fail on error', () => {
4-
it('should emits errors', (done) => {
4+
it('should emits errors', async () => {
55
const compiler = pack('error', { failOnError: true });
6-
7-
compiler.run((err, stats) => {
8-
expect(err).toBeNull();
9-
expect(stats.hasErrors()).toBe(true);
10-
done();
11-
});
6+
const stats = await compiler.runAsync();
7+
expect(stats.hasErrors()).toBe(true);
128
});
139

14-
it('should emit warnings when disabled', (done) => {
10+
it('should emit warnings when disabled', async () => {
1511
const compiler = pack('error', { failOnError: false });
16-
17-
compiler.run((err, stats) => {
18-
expect(err).toBeNull();
19-
expect(stats.hasErrors()).toBe(false);
20-
expect(stats.hasWarnings()).toBe(true);
21-
done();
22-
});
12+
const stats = await compiler.runAsync();
13+
expect(stats.hasErrors()).toBe(false);
14+
expect(stats.hasWarnings()).toBe(true);
2315
});
2416

25-
it('should correctly indentifies a success', (done) => {
17+
it('should correctly indentifies a success', async () => {
2618
const compiler = pack('good', { failOnError: true });
27-
28-
compiler.run((err) => {
29-
expect(err).toBeNull();
30-
done();
31-
});
19+
const stats = await compiler.runAsync();
20+
expect(stats.hasErrors()).toBe(false);
3221
});
3322
});

0 commit comments

Comments
 (0)