Skip to content

Commit

Permalink
chore: improve unit tests feedback loop by making them asynchronous (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Jan 26, 2024
1 parent e0d70bb commit fdb0ab2
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 357 deletions.
22 changes: 8 additions & 14 deletions test/context.test.js
Original file line number Diff line number Diff line change
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/good'),
});

compiler.run((err, stats) => {
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: '../good/' });

compiler.run((err, stats) => {
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);
});
});
53 changes: 17 additions & 36 deletions test/emit-error.test.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,39 @@
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);
});
});
54 changes: 17 additions & 37 deletions test/emit-warning.test.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,38 @@
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('warning', { 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('warning');

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('warning', { 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);
});
});
30 changes: 13 additions & 17 deletions test/empty.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { join } from 'path';

import webpack from 'webpack';

import StylelintWebpackPlugin from '../src';

describe('empty', () => {
it('no error when no files matching', (done) => {
const compiler = webpack({
context: join(__dirname, 'fixtures', 'empty'),
mode: 'development',
entry: './index',
plugins: [new StylelintWebpackPlugin()],
});
import pack from './utils/pack';

compiler.run((err, stats) => {
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
describe('empty', () => {
it('no error when no files matching', async () => {
const compiler = pack(
'empty',
{},
{
plugins: [new StylelintWebpackPlugin()],
},
);
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
});
});
24 changes: 8 additions & 16 deletions test/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,21 @@ 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 stylelint exceptions as errors', (done) => {
it('should propagate stylelint exceptions as errors', async () => {
jest.mock('stylelint', () => {
throw new Error('Oh no!');
});

const compiler = pack('good');

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);
});
});
36 changes: 12 additions & 24 deletions test/exclude.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
import pack from './utils/pack';

describe('exclude', () => {
it('should exclude with globs', (done) => {
it('should exclude with globs', async () => {
const compiler = pack('exclude', { exclude: ['*test*'] });

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('should exclude files', (done) => {
it('should exclude files', async () => {
const compiler = pack('exclude', { exclude: ['test.scss'] });

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('should exclude folders', (done) => {
it('should exclude folders', async () => {
const compiler = pack('exclude-folder', { exclude: ['folder'] });

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);
});
});
15 changes: 6 additions & 9 deletions test/fail-on-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import { join } from 'path';
import pack from './utils/pack';

describe('fail on config', () => {
it('fails when .stylelintrc is not a proper format', (done) => {
it('fails when .stylelintrc is not a proper format', async () => {
const configFile = join(__dirname, '.badstylelintrc');
const compiler = pack('error', { configFile });

compiler.run((err, stats) => {
const { errors } = stats.compilation;
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(errors).toHaveLength(1);
done();
});
const stats = await compiler.runAsync();
const { errors } = stats.compilation;
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(errors).toHaveLength(1);
});
});
31 changes: 10 additions & 21 deletions test/fail-on-error.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import pack from './utils/pack';

describe('fail on error', () => {
it('should emits errors', (done) => {
it('should emits errors', async () => {
const compiler = pack('error', { failOnError: 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 warnings when disabled', (done) => {
it('should emit warnings when disabled', async () => {
const compiler = pack('error', { failOnError: false });

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

it('should correctly indentifies a success', (done) => {
it('should correctly indentifies a success', async () => {
const compiler = pack('good', { failOnError: true });

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

0 comments on commit fdb0ab2

Please sign in to comment.