Skip to content

Commit

Permalink
chore: improve unit tests feedback loop by making (#241)
Browse files Browse the repository at this point in the history
them asynchronous
  • Loading branch information
onigoetz committed Jan 13, 2024
1 parent bbf698f commit 5ca18dc
Show file tree
Hide file tree
Showing 26 changed files with 240 additions and 362 deletions.
8 changes: 3 additions & 5 deletions test/autofix-stop.test.js
Expand Up @@ -25,12 +25,10 @@ describe('autofix stop', () => {
removeSync(entry);
});

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

compiler.run(() => {
expect(changed).toBe(false);
done();
});
await compiler.runAsync();
expect(changed).toBe(false);
});
});
13 changes: 5 additions & 8 deletions test/autofix.test.js
Expand Up @@ -17,7 +17,7 @@ describe('autofix stop', () => {

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
expect(readFileSync(entry).toString('utf8')).toMatchInlineSnapshot(`
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
expect(readFileSync(entry).toString('utf8')).toMatchInlineSnapshot(`
"function foo() {
return true;
}
foo();
"
`);
done();
});
},
);
});
22 changes: 8 additions & 14 deletions test/context.test.js
Expand Up @@ -3,25 +3,19 @@ import { join } from 'path';
import pack from './utils/pack';

describe('context', () => {
it('absolute', (done) => {
it('absolute', async () => {
const compiler = pack('good', { context: join(__dirname, 'fixtures') });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
});

it('relative', (done) => {
it('relative', async () => {
const compiler = pack('good', { context: '../fixtures/' });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
});
});
49 changes: 17 additions & 32 deletions test/emit-error.test.js
@@ -1,58 +1,43 @@
import pack from './utils/pack';

describe('emit error', () => {
it('should not emit errors if emitError is false', (done) => {
it('should not emit errors if emitError is false', async () => {
const compiler = pack('error', { emitError: false });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(false);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasErrors()).toBe(false);
});

it('should emit errors if emitError is undefined', (done) => {
it('should emit errors if emitError is undefined', async () => {
const compiler = pack('error', {});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasErrors()).toBe(true);
});

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasErrors()).toBe(true);
});

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
});

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(true);
});
});
49 changes: 17 additions & 32 deletions test/emit-warning.test.js
@@ -1,58 +1,43 @@
import pack from './utils/pack';

describe('emit warning', () => {
it('should not emit warnings if emitWarning is false', (done) => {
it('should not emit warnings if emitWarning is false', async () => {
const compiler = pack('warn', { emitWarning: false });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
});

it('should emit warnings if emitWarning is undefined', (done) => {
it('should emit warnings if emitWarning is undefined', async () => {
const compiler = pack('warn', {});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(true);
});

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(true);
});

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(false);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(false);
});

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(true);
});
});
22 changes: 8 additions & 14 deletions test/error.test.js
Expand Up @@ -5,18 +5,15 @@ describe('error', () => {
jest.restoreAllMocks();
});

it('should return error if file is bad', (done) => {
it('should return error if file is bad', async () => {
const compiler = pack('error');

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
});

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

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
});
});
46 changes: 18 additions & 28 deletions test/eslint-lint.test.js
Expand Up @@ -17,43 +17,33 @@ describe('eslint lint', () => {
mockLintFiles.mockClear();
});

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

compiler.run((err) => {
const files = [expect.stringMatching('lint-one-entry.js')];
expect(mockLintFiles).toHaveBeenCalledWith(files);
expect(err).toBeNull();
done();
});
await compiler.runAsync();
expect(mockLintFiles).toHaveBeenCalledTimes(1);
});

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

compiler.run((err) => {
const files = [
expect.stringMatching('lint-two-entry.js'),
expect.stringMatching('lint.js'),
];
expect(mockLintFiles).toHaveBeenCalledWith(files);
expect(err).toBeNull();
done();
});
await compiler.runAsync();
const files = [
expect.stringMatching('lint-two-entry.js'),
expect.stringMatching('lint.js'),
];
expect(mockLintFiles).toHaveBeenCalledWith(files);
});

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

compiler.run((err) => {
const files = [
expect.stringMatching('lint-more-entry.js'),
expect.stringMatching('lint-more.js'),
expect.stringMatching('lint.js'),
];
expect(mockLintFiles).toHaveBeenCalledWith(files);
expect(err).toBeNull();
done();
});
await compiler.runAsync();
const files = [
expect.stringMatching('lint-more-entry.js'),
expect.stringMatching('lint-more.js'),
expect.stringMatching('lint.js'),
];
expect(mockLintFiles).toHaveBeenCalledWith(files);
});
});
13 changes: 5 additions & 8 deletions test/eslint-path.test.js
Expand Up @@ -3,16 +3,13 @@ import { join } from 'path';
import pack from './utils/pack';

describe('eslint path', () => {
it('should use another instance of eslint via eslintPath config', (done) => {
it('should use another instance of eslint via eslintPath config', async () => {
const eslintPath = join(__dirname, 'mock/eslint');
const compiler = pack('good', { eslintPath });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(stats.compilation.errors[0].message).toContain('Fake error');
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(stats.compilation.errors[0].message).toContain('Fake error');
});
});
16 changes: 6 additions & 10 deletions test/eslintignore.test.js
Expand Up @@ -3,17 +3,13 @@ import ESLintError from '../src/ESLintError';
import pack from './utils/pack';

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

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(
stats.compilation.errors.filter((x) => x instanceof ESLintError),
).toEqual([]);

done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(
stats.compilation.errors.filter((x) => x instanceof ESLintError),
).toEqual([]);
});
});

0 comments on commit 5ca18dc

Please sign in to comment.