Skip to content

Commit 5ca18dc

Browse files
authored
chore: improve unit tests feedback loop by making (#241)
them asynchronous
1 parent bbf698f commit 5ca18dc

26 files changed

+240
-362
lines changed

test/autofix-stop.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ describe('autofix stop', () => {
2525
removeSync(entry);
2626
});
2727

28-
it('should not change file if there are no fixable errors/warnings', (done) => {
28+
it('should not change file if there are no fixable errors/warnings', async () => {
2929
const compiler = pack('nonfixable-clone', { fix: true });
3030

31-
compiler.run(() => {
32-
expect(changed).toBe(false);
33-
done();
34-
});
31+
await compiler.runAsync();
32+
expect(changed).toBe(false);
3533
});
3634
});

test/autofix.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('autofix stop', () => {
1717

1818
test.each([[{}], [{ threads: false }]])(
1919
'should not throw error if file ok after auto-fixing',
20-
(cfg, done) => {
20+
async (cfg) => {
2121
const compiler = pack('fixable-clone', {
2222
...cfg,
2323
fix: true,
@@ -27,20 +27,17 @@ describe('autofix stop', () => {
2727
},
2828
});
2929

30-
compiler.run((err, stats) => {
31-
expect(err).toBeNull();
32-
expect(stats.hasWarnings()).toBe(false);
33-
expect(stats.hasErrors()).toBe(false);
34-
expect(readFileSync(entry).toString('utf8')).toMatchInlineSnapshot(`
30+
const stats = await compiler.runAsync();
31+
expect(stats.hasWarnings()).toBe(false);
32+
expect(stats.hasErrors()).toBe(false);
33+
expect(readFileSync(entry).toString('utf8')).toMatchInlineSnapshot(`
3534
"function foo() {
3635
return true;
3736
}
3837
3938
foo();
4039
"
4140
`);
42-
done();
43-
});
4441
},
4542
);
4643
});

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', { context: join(__dirname, 'fixtures') });
88

9-
compiler.run((err, stats) => {
10-
expect(err).toBeNull();
11-
expect(stats.hasWarnings()).toBe(false);
12-
expect(stats.hasErrors()).toBe(false);
13-
done();
14-
});
9+
const stats = await compiler.runAsync();
10+
expect(stats.hasWarnings()).toBe(false);
11+
expect(stats.hasErrors()).toBe(false);
1512
});
1613

17-
it('relative', (done) => {
14+
it('relative', async () => {
1815
const compiler = pack('good', { context: '../fixtures/' });
1916

20-
compiler.run((err, stats) => {
21-
expect(err).toBeNull();
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 & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,43 @@
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 });
66

7-
compiler.run((err, stats) => {
8-
expect(err).toBeNull();
9-
expect(stats.hasErrors()).toBe(false);
10-
done();
11-
});
7+
const stats = await compiler.runAsync();
8+
expect(stats.hasErrors()).toBe(false);
129
});
1310

14-
it('should emit errors if emitError is undefined', (done) => {
11+
it('should emit errors if emitError is undefined', async () => {
1512
const compiler = pack('error', {});
1613

17-
compiler.run((err, stats) => {
18-
expect(err).toBeNull();
19-
expect(stats.hasErrors()).toBe(true);
20-
done();
21-
});
14+
const stats = await compiler.runAsync();
15+
expect(stats.hasErrors()).toBe(true);
2216
});
2317

24-
it('should emit errors if emitError is true', (done) => {
18+
it('should emit errors if emitError is true', async () => {
2519
const compiler = pack('error', { emitError: true });
2620

27-
compiler.run((err, stats) => {
28-
expect(err).toBeNull();
29-
expect(stats.hasErrors()).toBe(true);
30-
done();
31-
});
21+
const stats = await compiler.runAsync();
22+
expect(stats.hasErrors()).toBe(true);
3223
});
3324

34-
it('should emit errors, but not warnings if emitError is true and emitWarning is false', (done) => {
25+
it('should emit errors, but not warnings if emitError is true and emitWarning is false', async () => {
3526
const compiler = pack('full-of-problems', {
3627
emitError: true,
3728
emitWarning: false,
3829
});
3930

40-
compiler.run((err, stats) => {
41-
expect(err).toBeNull();
42-
expect(stats.hasWarnings()).toBe(false);
43-
expect(stats.hasErrors()).toBe(true);
44-
done();
45-
});
31+
const stats = await compiler.runAsync();
32+
expect(stats.hasWarnings()).toBe(false);
33+
expect(stats.hasErrors()).toBe(true);
4634
});
4735

48-
it('should emit errors and warnings if emitError is true and emitWarning is undefined', (done) => {
36+
it('should emit errors and warnings if emitError is true and emitWarning is undefined', async () => {
4937
const compiler = pack('full-of-problems', { emitError: true });
5038

51-
compiler.run((err, stats) => {
52-
expect(err).toBeNull();
53-
expect(stats.hasWarnings()).toBe(true);
54-
expect(stats.hasErrors()).toBe(true);
55-
done();
56-
});
39+
const stats = await compiler.runAsync();
40+
expect(stats.hasWarnings()).toBe(true);
41+
expect(stats.hasErrors()).toBe(true);
5742
});
5843
});

test/emit-warning.test.js

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,43 @@
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('warn', { emitWarning: false });
66

7-
compiler.run((err, stats) => {
8-
expect(err).toBeNull();
9-
expect(stats.hasWarnings()).toBe(false);
10-
done();
11-
});
7+
const stats = await compiler.runAsync();
8+
expect(stats.hasWarnings()).toBe(false);
129
});
1310

14-
it('should emit warnings if emitWarning is undefined', (done) => {
11+
it('should emit warnings if emitWarning is undefined', async () => {
1512
const compiler = pack('warn', {});
1613

17-
compiler.run((err, stats) => {
18-
expect(err).toBeNull();
19-
expect(stats.hasWarnings()).toBe(true);
20-
done();
21-
});
14+
const stats = await compiler.runAsync();
15+
expect(stats.hasWarnings()).toBe(true);
2216
});
2317

24-
it('should emit warnings if emitWarning is true', (done) => {
18+
it('should emit warnings if emitWarning is true', async () => {
2519
const compiler = pack('warn', { emitWarning: true });
2620

27-
compiler.run((err, stats) => {
28-
expect(err).toBeNull();
29-
expect(stats.hasWarnings()).toBe(true);
30-
done();
31-
});
21+
const stats = await compiler.runAsync();
22+
expect(stats.hasWarnings()).toBe(true);
3223
});
3324

34-
it('should emit warnings, but not warnings if emitWarning is true and emitError is false', (done) => {
25+
it('should emit warnings, but not warnings if emitWarning is true and emitError is false', async () => {
3526
const compiler = pack('full-of-problems', {
3627
emitWarning: true,
3728
emitError: false,
3829
});
3930

40-
compiler.run((err, stats) => {
41-
expect(err).toBeNull();
42-
expect(stats.hasWarnings()).toBe(true);
43-
expect(stats.hasErrors()).toBe(false);
44-
done();
45-
});
31+
const stats = await compiler.runAsync();
32+
expect(stats.hasWarnings()).toBe(true);
33+
expect(stats.hasErrors()).toBe(false);
4634
});
4735

48-
it('should emit warnings and errors if emitWarning is true and emitError is undefined', (done) => {
36+
it('should emit warnings and errors if emitWarning is true and emitError is undefined', async () => {
4937
const compiler = pack('full-of-problems', { emitWarning: true });
5038

51-
compiler.run((err, stats) => {
52-
expect(err).toBeNull();
53-
expect(stats.hasWarnings()).toBe(true);
54-
expect(stats.hasErrors()).toBe(true);
55-
done();
56-
});
39+
const stats = await compiler.runAsync();
40+
expect(stats.hasWarnings()).toBe(true);
41+
expect(stats.hasErrors()).toBe(true);
5742
});
5843
});

test/error.test.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ 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');
1010

11-
compiler.run((err, stats) => {
12-
expect(err).toBeNull();
13-
expect(stats.hasWarnings()).toBe(false);
14-
expect(stats.hasErrors()).toBe(true);
15-
done();
16-
});
11+
const stats = await compiler.runAsync();
12+
expect(stats.hasWarnings()).toBe(false);
13+
expect(stats.hasErrors()).toBe(true);
1714
});
1815

19-
it('should propagate eslint exceptions as errors', (done) => {
16+
it('should propagate eslint exceptions as errors', async () => {
2017
jest.mock('eslint', () => {
2118
return {
2219
ESLint: function ESLint() {
@@ -27,11 +24,8 @@ describe('error', () => {
2724

2825
const compiler = pack('good', { threads: false });
2926

30-
compiler.run((err, stats) => {
31-
expect(err).toBeNull();
32-
expect(stats.hasWarnings()).toBe(false);
33-
expect(stats.hasErrors()).toBe(true);
34-
done();
35-
});
27+
const stats = await compiler.runAsync();
28+
expect(stats.hasWarnings()).toBe(false);
29+
expect(stats.hasErrors()).toBe(true);
3630
});
3731
});

test/eslint-lint.test.js

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,33 @@ describe('eslint lint', () => {
1717
mockLintFiles.mockClear();
1818
});
1919

20-
it('should lint one file', (done) => {
20+
it('should lint one file', async () => {
2121
const compiler = pack('lint-one', { threads: false });
2222

23-
compiler.run((err) => {
24-
const files = [expect.stringMatching('lint-one-entry.js')];
25-
expect(mockLintFiles).toHaveBeenCalledWith(files);
26-
expect(err).toBeNull();
27-
done();
28-
});
23+
await compiler.runAsync();
24+
expect(mockLintFiles).toHaveBeenCalledTimes(1);
2925
});
3026

31-
it('should lint two files', (done) => {
27+
it('should lint two files', async () => {
3228
const compiler = pack('lint-two', { threads: false });
3329

34-
compiler.run((err) => {
35-
const files = [
36-
expect.stringMatching('lint-two-entry.js'),
37-
expect.stringMatching('lint.js'),
38-
];
39-
expect(mockLintFiles).toHaveBeenCalledWith(files);
40-
expect(err).toBeNull();
41-
done();
42-
});
30+
await compiler.runAsync();
31+
const files = [
32+
expect.stringMatching('lint-two-entry.js'),
33+
expect.stringMatching('lint.js'),
34+
];
35+
expect(mockLintFiles).toHaveBeenCalledWith(files);
4336
});
4437

45-
it('should lint more files', (done) => {
38+
it('should lint more files', async () => {
4639
const compiler = pack('lint-more', { threads: false });
4740

48-
compiler.run((err) => {
49-
const files = [
50-
expect.stringMatching('lint-more-entry.js'),
51-
expect.stringMatching('lint-more.js'),
52-
expect.stringMatching('lint.js'),
53-
];
54-
expect(mockLintFiles).toHaveBeenCalledWith(files);
55-
expect(err).toBeNull();
56-
done();
57-
});
41+
await compiler.runAsync();
42+
const files = [
43+
expect.stringMatching('lint-more-entry.js'),
44+
expect.stringMatching('lint-more.js'),
45+
expect.stringMatching('lint.js'),
46+
];
47+
expect(mockLintFiles).toHaveBeenCalledWith(files);
5848
});
5949
});

test/eslint-path.test.js

Lines changed: 5 additions & 8 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('eslint path', () => {
6-
it('should use another instance of eslint via eslintPath config', (done) => {
6+
it('should use another instance of eslint via eslintPath config', async () => {
77
const eslintPath = join(__dirname, 'mock/eslint');
88
const compiler = pack('good', { eslintPath });
99

10-
compiler.run((err, stats) => {
11-
expect(err).toBeNull();
12-
expect(stats.hasWarnings()).toBe(false);
13-
expect(stats.hasErrors()).toBe(true);
14-
expect(stats.compilation.errors[0].message).toContain('Fake error');
15-
done();
16-
});
10+
const stats = await compiler.runAsync();
11+
expect(stats.hasWarnings()).toBe(false);
12+
expect(stats.hasErrors()).toBe(true);
13+
expect(stats.compilation.errors[0].message).toContain('Fake error');
1714
});
1815
});

test/eslintignore.test.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ import ESLintError from '../src/ESLintError';
33
import pack from './utils/pack';
44

55
describe('eslintignore', () => {
6-
it('should ignores files present in .eslintignore', (done) => {
6+
it('should ignores files present in .eslintignore', async () => {
77
const compiler = pack('ignore', { ignore: true });
88

9-
compiler.run((err, stats) => {
10-
expect(err).toBeNull();
11-
expect(stats.hasWarnings()).toBe(false);
12-
expect(
13-
stats.compilation.errors.filter((x) => x instanceof ESLintError),
14-
).toEqual([]);
15-
16-
done();
17-
});
9+
const stats = await compiler.runAsync();
10+
expect(stats.hasWarnings()).toBe(false);
11+
expect(
12+
stats.compilation.errors.filter((x) => x instanceof ESLintError),
13+
).toEqual([]);
1814
});
1915
});

0 commit comments

Comments
 (0)