Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk committed Nov 6, 2020
1 parent 6d8140d commit 153d5aa
Show file tree
Hide file tree
Showing 46 changed files with 295 additions and 305 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -77,7 +77,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"execa": "^4.1.0",
"get-port": "^5.1.1",
"get-port": "5.0.0",
"git-cz": "^4.7.1",
"husky": "^4.3.0",
"jest": "^26.6.1",
Expand Down
12 changes: 6 additions & 6 deletions packages/webpack-cli/__tests__/info.test.js
Expand Up @@ -3,23 +3,23 @@ const path = require('path');

describe('Info', () => {
it('should run with cli', () => {
const { stderr } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['info'], {
const { stdout } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['info'], {
cwd: path.resolve(__dirname),
reject: false,
});
expect(stderr).toContain('System');
expect(stderr).toContain('Binaries');
expect(stderr).toContain('OS');
expect(stdout).toContain('System');
expect(stdout).toContain('Binaries');
expect(stdout).toContain('OS');
});

it('should work with flags', () => {
const { stderr } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['info', '--output=json'], {
const { stdout } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['info', '--output=json'], {
cwd: path.resolve(__dirname),
reject: false,
});

const testJSON = () => {
const output = JSON.parse(stderr);
const output = JSON.parse(stdout);
expect(output['System']).toBeTruthy();
expect(output['System']['OS']).toBeTruthy();
};
Expand Down
41 changes: 18 additions & 23 deletions packages/webpack-cli/lib/plugins/WebpackCLIPlugin.js
Expand Up @@ -16,33 +16,16 @@ class WebpackCLIPlugin {
this.appendProgressPlugin(compiler, progress);
}

const isWebpack5 = !!compiler.webpack;
const isWebpack5 = Boolean(compiler.webpack);

const logger = compiler.getInfrastructureLogger(PluginName);

const resolveName = (obj) => (obj.name ? `${obj.name} ` : '');

const done = (stats) => {
const printStats = (childCompiler, childStats) => {
const statsOptions = {
...getStatsOptions(childCompiler),
version: false,
timings: false,
};
const name = resolveName(childCompiler);

const statsString = name + 'output:\n' + childStats.toString(statsOptions);

if (statsString.length) {
if (childStats.hasErrors()) {
logger.error(statsString);
} else if (childStats.hasWarnings()) {
logger.warn(statsString);
} else {
logger.info(statsString);
}
}

const status = childStats.toString({
preset: 'none',
version: true,
Expand All @@ -55,7 +38,7 @@ class WebpackCLIPlugin {
modules: false,
});

const str = (() => {
const normalizedStatus = (() => {
if (isWebpack5) {
return status;
} else {
Expand All @@ -69,13 +52,21 @@ class WebpackCLIPlugin {
})();

if (childStats.hasErrors()) {
logger.error(str);
logger.error(normalizedStatus);
} else if (childStats.hasWarnings()) {
logger.warn(str);
logger.warn(normalizedStatus);
} else {
logger.info(str);
logger.info(normalizedStatus);
}

const statsOptions = {
...getStatsOptions(childCompiler),
version: false,
timings: false,
};
const statsString = childStats.toString(statsOptions);
process.stdout.write(statsString + '\n');

if (this.isWatch) {
logger.info(name + 'watching files for updates...');
}
Expand All @@ -101,7 +92,11 @@ class WebpackCLIPlugin {
}
this.isWatch = true;
});
compiler.hooks.done.tap(PluginName, done);
compiler.hooks.done.tap(PluginName, (stats) => {
process.nextTick(() => {
done(stats);
});
});
}

appendProgressPlugin(compiler, progress) {
Expand Down
26 changes: 6 additions & 20 deletions packages/webpack-cli/lib/utils/logger.js
@@ -1,25 +1,11 @@
const util = require('util');
const { red, cyan, yellow, green } = require('colorette');

const write = (prefix = '[webpack-cli] ', color = (val) => val) => {
return (...args) => {
const str = prefix + color(util.format(...args));
process.stderr.write(str + '\n');
};
};

module.exports = {
error: write(undefined, red),
warn: write(undefined, yellow),
info: write(undefined, cyan),
log: write(),

/**
* @deprecated use info
*/
success: write(undefined, green),
/**
* @deprecated use info
*/
raw: write(''),
error: (val) => console.error(`[webpack-cli] ${red(util.format(val))}`),
warn: (val) => console.warn(`[webpack-cli] ${yellow(val)}`),
info: (val) => console.info(`[webpack-cli] ${cyan(val)}`),
success: (val) => console.log(`[webpack-cli] ${green(val)}`),
log: (val) => console.log(`[webpack-cli] ${val}`),
raw: (val) => console.log(val),
};
5 changes: 1 addition & 4 deletions test/analyze/analyze-flag.test.js
Expand Up @@ -2,10 +2,7 @@

const { runAndGetWatchProc, killByPort } = require('../utils/test-utils');

// FIXME: something is off when running by `yarn jest`, looks like it hangs
// when running as one `yarn jest -- analyze` always pass

describe.skip('--analyze flag', () => {
describe('--analyze flag', () => {
beforeEach((done) => {
killByPort(8888, done);
});
Expand Down
20 changes: 11 additions & 9 deletions test/bail/bail.test.js
Expand Up @@ -2,54 +2,56 @@

const { run, runWatch } = require('../utils/test-utils');

describe.skip('bail and watch warning', () => {
const str = `you are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`;

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

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

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

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

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

expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stderr).not.toContain(str);
});

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

expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stderr).not.toContain(str);
});

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

expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stderr).not.toContain(str);
});

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

expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stderr).toContain(str);
});

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

expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stderr).toContain(str);
});

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

expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stderr).toContain(str);
});
});
6 changes: 3 additions & 3 deletions test/build-errors/errors.test.js
Expand Up @@ -5,10 +5,10 @@ const { resolve } = require('path');

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

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

Expand Down
6 changes: 3 additions & 3 deletions test/build-warnings/warnings.test.js
Expand Up @@ -5,10 +5,10 @@ const { resolve } = require('path');

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

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

Expand Down
63 changes: 32 additions & 31 deletions test/colors/colors.test.js
@@ -1,80 +1,81 @@
'use strict';
const { run, isWebpack5 } = require('../utils/test-utils');
const { run } = require('../utils/test-utils');
const { resolve } = require('path');
const { options: coloretteOptions } = require('colorette');
const { options: coloretteOptions, green } = require('colorette');

describe.skip('colorts', () => {
it('should output by default', () => {
const { stderr, exitCode } = run(__dirname);
it.only('should output by default', () => {
const { stdout, exitCode } = run(__dirname, ['--color']);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
expect(stderr).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
// const output = 'main.js';
console.log(coloretteOptions.enabled, [stdout], green('main.js'));
expect(stdout).toContain(green('main.js'));
expect(exitCode).toBe(0);
});

it('should work with the "stats" option from flags', () => {
const { stderr, exitCode } = run(__dirname, ['--stats=verbose']);
const { stdout, exitCode } = run(__dirname, ['--stats=verbose']);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
expect(stderr).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
const output = 'main.js';
expect(stdout).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
expect(exitCode).toBe(0);
});

it('should work with the "stats" option from flags and from configuration', () => {
const { stderr, exitCode } = run(__dirname, ['--stats=verbose', `--config=${resolve(__dirname, './no-stats.webpack.config.js')}`]);
const { stdout, exitCode } = run(__dirname, ['--stats=verbose', `--config=${resolve(__dirname, './no-stats.webpack.config.js')}`]);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
expect(stderr).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
const output = 'main.js';
expect(stdout).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
expect(exitCode).toBe(0);
});

it('should work with the "stats" option from flags and from configuration #2', () => {
const { stderr, exitCode } = run(__dirname, ['--stats=verbose', '--config=stats-string.webpack.config.js']);
const { stdout, exitCode } = run(__dirname, ['--stats=verbose', '--config=stats-string.webpack.config.js']);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
expect(stderr).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
const output = 'main.js';
expect(stdout).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
expect(exitCode).toBe(0);
});

it('should work with the "stats" option from the configuration', () => {
const { stderr, exitCode } = run(__dirname, ['--config=stats-string.webpack.config.js']);
const { stdout, exitCode } = run(__dirname, ['--config=stats-string.webpack.config.js']);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
expect(stderr).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
const output = 'main.js';
expect(stdout).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
expect(exitCode).toBe(0);
});

it('should work with the "stats" option from the configuration #1', () => {
const { stderr, exitCode } = run(__dirname, ['--config=stats-boolean.webpack.config.js']);
const { stdout, exitCode } = run(__dirname, ['--config=stats-boolean.webpack.config.js']);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
expect(stderr).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
const output = 'main.js';
expect(stdout).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
expect(exitCode).toBe(0);
});

it('should work with the "stats" option from the configuration #2', () => {
const { stderr, exitCode } = run(__dirname, ['--config=no-stats.webpack.config.js']);
const { stdout, exitCode } = run(__dirname, ['--config=no-stats.webpack.config.js']);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
expect(stderr).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
const output = 'main.js';
expect(stdout).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
expect(exitCode).toBe(0);
});

it('should work with the "stats" option from the configuration #3', () => {
const { stderr, exitCode } = run(__dirname, ['--config=colors-true.webpack.config.js']);
const { stdout, exitCode } = run(__dirname, ['--config=colors-true.webpack.config.js']);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
expect(stderr).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
const output = 'main.js';
expect(stdout).toContain(coloretteOptions.enabled ? `\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m` : output);
expect(exitCode).toBe(0);
});

it('should work with the "stats" option from the configuration #4', () => {
const { stderr, exitCode } = run(__dirname, ['--config=colors-false.webpack.config.js']);
const { stdout, exitCode } = run(__dirname, ['--config=colors-false.webpack.config.js']);

const output = isWebpack5 ? '<i> [webpack-cli] output:' : 'main.js';
const output = 'main.js';

expect(stderr).not.toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`);
expect(stderr).toContain(output);
expect(stdout).not.toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`);
expect(stdout).toContain(output);
expect(exitCode).toBe(0);
});
});
6 changes: 3 additions & 3 deletions test/config/defaults/basic-config/default-js-config.test.js
Expand Up @@ -4,11 +4,11 @@ const { run, isWebpack5 } = require('../../../utils/test-utils');

describe('Zero Config', () => {
it('runs when config is present but not supplied via flag', () => {
const { stderr, exitCode } = run(__dirname, [], false);
const { stdout, stderr, exitCode } = run(__dirname, [], false);
// default entry should be used
expect(stderr).toContain('./src/index.js');
expect(stdout).toContain('./src/index.js');
// should pick up the output path from config
expect(stderr).toContain('test-output');
expect(stdout).toContain('test-output');
if (!isWebpack5) {
expect(stderr).toContain('Hash');
expect(stderr).toContain('Built at');
Expand Down

0 comments on commit 153d5aa

Please sign in to comment.