Skip to content

Commit

Permalink
tests: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 30, 2020
1 parent bc2d0e3 commit a322e0f
Show file tree
Hide file tree
Showing 132 changed files with 1,331 additions and 903 deletions.
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -220,7 +220,7 @@ class WebpackCLI {
}

if (typeof args.progress === 'string' && args.progress !== 'profile') {
logger.error(`'${this.options.progress}' is an invalid value for the --progress option. Only 'profile' is allowed.`);
logger.error(`'${args.progress}' is an invalid value for the --progress option. Only 'profile' is allowed.`);
process.exit(2);
}

Expand Down
3 changes: 2 additions & 1 deletion test/analyze/analyze-flag.test.js
Expand Up @@ -22,7 +22,8 @@ describe('--analyze flag', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['-c', './analyze.config.js', '--analyze']);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stdout).toContain('Webpack Bundle Analyzer saved report to');
expect(stdout.match(/Webpack Bundle Analyzer saved report to/g)).toHaveLength(1);
});
Expand Down
28 changes: 24 additions & 4 deletions test/bail/bail.test.js
Expand Up @@ -4,59 +4,79 @@ const { run, runWatch } = require('../utils/test-utils');

describe('bail and watch warning', () => {
it('should not log warning in not watch mode', async () => {
const { stderr, stdout, exitCode } = await run(__dirname, ['-c', 'bail-webpack.config.js']);
const { exitCode, stderr, stdout } = await run(__dirname, ['-c', 'bail-webpack.config.js']);

expect(exitCode).toEqual(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should not log warning in not watch mode without the "bail" option', async () => {
const { stderr, stdout, exitCode } = await run(__dirname, ['-c', 'no-bail-webpack.config.js']);
const { exitCode, stderr, stdout } = await run(__dirname, ['-c', 'no-bail-webpack.config.js']);

expect(exitCode).toEqual(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should not log warning in not watch mode without the "watch" option', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'watch-webpack.config.js']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should not log warning without the "bail" option', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'no-bail-webpack.config.js', '--watch']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should not log warning without the "bail" option', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'no-bail-webpack.config.js', '--watch']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should log warning in watch mode', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'bail-webpack.config.js', '--watch']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should log warning in watch mode', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'bail-and-watch-webpack.config.js']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should log warning in case of multiple compilers', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'multi-webpack.config.js']);

const { stderr, stdout } = await runWatch(__dirname, ['-c', 'multi-webpack.config.js'], true, "Compilation 'second' finished");

expect(stderr).toContain("Compilation 'first' starting...");
expect(stderr).toContain("Compilation 'first' finished");
expect(stderr).toContain("Compiler 'first' is watching files for updates...");
expect(stderr).toContain("Compilation 'second' starting...");
expect(stderr).toContain("Compilation 'second' finished");
expect(stderr).not.toContain("Compiler 'second' is watching files for updates...");
expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});
Expand Down
18 changes: 12 additions & 6 deletions test/build-errors/errors.test.js
Expand Up @@ -5,18 +5,22 @@ const { resolve } = require('path');

describe('errors', () => {
it('should output by default', () => {
const { stdout, exitCode } = run(__dirname);
const { exitCode, stderr, stdout } = run(__dirname);

expect(exitCode).toBe(1);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stdout).toMatch(/ERROR/);
expect(stdout).toMatch(/Error: Can't resolve/);
expect(exitCode).toBe(1);
});

it('should output JSON with the "json" flag', () => {
const { stdout, exitCode } = run(__dirname, ['--json']);
const { exitCode, stderr, stdout } = run(__dirname, ['--json']);

expect(() => JSON.parse(stdout)).not.toThrow();
expect(exitCode).toBe(1);
expect(stderr).not.toContain('Compilation starting...');
expect(stderr).not.toContain('Compilation finished');
expect(() => JSON.parse(stdout)).not.toThrow();

const json = JSON.parse(stdout);

Expand All @@ -27,10 +31,12 @@ describe('errors', () => {
});

it('should store json to a file', (done) => {
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json']);
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);

expect(stdout).toContain('stats are successfully stored as json to stats.json');
expect(exitCode).toBe(1);
expect(stderr).not.toContain('Compilation starting...');
expect(stderr).not.toContain('Compilation finished');
expect(stdout).toContain('stats are successfully stored as json to stats.json');

stat(resolve(__dirname, './stats.json'), (err, stats) => {
expect(err).toBe(null);
Expand Down
19 changes: 13 additions & 6 deletions test/build-warnings/warnings.test.js
Expand Up @@ -5,18 +5,23 @@ const { resolve } = require('path');

describe('warnings', () => {
it('should output by default', () => {
const { stdout, exitCode } = run(__dirname);
const { exitCode, stderr, stdout } = run(__dirname);

expect(exitCode).toBe(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stdout).toMatch(/WARNING/);
expect(stdout).toMatch(/Error: Can't resolve/);
expect(exitCode).toBe(0);
});

it('should output JSON with the "json" flag', () => {
const { stdout, exitCode } = run(__dirname, ['--json']);
const { exitCode, stderr, stdout } = run(__dirname, ['--json']);

expect(() => JSON.parse(stdout)).not.toThrow();
expect(exitCode).toBe(0);
expect(stderr).not.toContain('Compilation starting...');
expect(stderr).not.toContain('Compilation finished');

expect(() => JSON.parse(stdout)).not.toThrow();

const json = JSON.parse(stdout);

Expand All @@ -27,10 +32,12 @@ describe('warnings', () => {
});

it('should store json to a file', (done) => {
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json']);
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);

expect(stdout).toContain('stats are successfully stored as json to stats.json');
expect(exitCode).toBe(0);
expect(stderr).not.toContain('Compilation starting...');
expect(stderr).not.toContain('Compilation finished');
expect(stdout).toContain('stats are successfully stored as json to stats.json');

stat(resolve(__dirname, './stats.json'), (err, stats) => {
expect(err).toBe(null);
Expand Down
12 changes: 12 additions & 0 deletions test/cache/cache.test.js
Expand Up @@ -16,6 +16,9 @@ describe('cache', () => {
it('should work', () => {
let { exitCode, stderr, stdout } = run(__dirname, ['-c', './webpack.config.js', '--cache-name', 'test'], false);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');

if (isWebpack5) {
expect(stderr).toContain('No pack exists at');
expect(stderr).toContain('Stored pack');
Expand All @@ -26,6 +29,9 @@ describe('cache', () => {

({ exitCode, stderr, stdout } = run(__dirname, ['-c', './webpack.config.js', '--cache-name', 'test'], false));

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');

if (isWebpack5) {
expect(exitCode).toEqual(0);
expect(stderr).toContain('restore cache container');
Expand All @@ -40,6 +46,9 @@ describe('cache', () => {
it('should work with autoloading configuration', () => {
let { exitCode, stderr, stdout } = run(__dirname, ['--cache-name', 'test-1'], false);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');

if (isWebpack5) {
expect(stderr).toContain('No pack exists at');
expect(stderr).toContain('Stored pack');
Expand All @@ -50,6 +59,9 @@ describe('cache', () => {

({ exitCode, stderr, stdout } = run(__dirname, ['--cache-name', 'test-1'], false));

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');

if (isWebpack5) {
expect(exitCode).toEqual(0);
expect(stderr).toContain('restore cache container');
Expand Down

0 comments on commit a322e0f

Please sign in to comment.