Skip to content

Commit

Permalink
tests: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Nov 6, 2020
1 parent 2faaaa1 commit 60cd78d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/webpack-cli/lib/bootstrap.js
Expand Up @@ -4,6 +4,7 @@ const logger = require('./utils/logger');
const { isCommandUsed } = require('./utils/arg-utils');
const argParser = require('./utils/arg-parser');
const leven = require('leven');
const { options: coloretteOptions } = require('colorette');

process.title = 'webpack-cli';

Expand All @@ -23,6 +24,9 @@ const runCLI = async (cliArgs) => {
// If the unknown arg starts with a '-', it will be considered an unknown flag rather than an entry
let entry;

// enable/disable colors
coloretteOptions.enabled = parsedArgs.opts.color;

if (parsedArgs.unknownArgs.length > 0) {
entry = [];

Expand Down
4 changes: 0 additions & 4 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -235,10 +235,6 @@ class WebpackCLI {
}
}

if (!coloretteOptions.enabled && args.color) {
coloretteOptions.enabled = args.color;
}

stats.colors = typeof stats.colors !== 'undefined' ? stats.colors : coloretteOptions.enabled;

return stats;
Expand Down
27 changes: 26 additions & 1 deletion test/json/json.test.js
Expand Up @@ -2,6 +2,9 @@
const { run } = require('../utils/test-utils');
const { stat, readFile } = require('fs');
const { resolve } = require('path');
const { green } = require('colorette');

const successMessage = 'stats are successfully stored as json to stats.json';

describe('json flag', () => {
it('should return valid json', () => {
Expand All @@ -16,7 +19,29 @@ describe('json flag', () => {
it('should store json to a file', (done) => {
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json']);

expect(stdout).toContain('stats are successfully stored as json to stats.json');
expect(stdout).toContain(`${green(successMessage)}`);
expect(exitCode).toBe(0);

stat(resolve(__dirname, './stats.json'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);

readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(JSON.parse(data)['hash']).toBeTruthy();
expect(JSON.parse(data)['version']).toBeTruthy();
expect(JSON.parse(data)['time']).toBeTruthy();
expect(() => JSON.parse(data)).not.toThrow();
done();
});
});
});

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

expect(stdout).not.toContain(`${green(successMessage)}`);
expect(stdout).toContain(successMessage);
expect(exitCode).toBe(0);

stat(resolve(__dirname, './stats.json'), (err, stats) => {
Expand Down
10 changes: 10 additions & 0 deletions test/unknown/unknown.test.js
@@ -1,12 +1,22 @@
const { run } = require('../utils/test-utils');
const { red } = require('colorette');

describe('unknown behaviour', () => {
it('warns the user if an unknown flag is passed in', () => {
const { stderr, exitCode } = run(__dirname, ['--unknown']);
expect(stderr).toBeTruthy();
expect(stderr).toContain(`${red('Unknown argument: --unknown')}`);
expect(exitCode).toBe(2);
});

it('throws error for unknow flag and respect --no-color', () => {
const { stderr, exitCode } = run(__dirname, ['--unknown', '--no-color']);
expect(stderr).toBeTruthy();
expect(stderr).not.toContain(`${red('Unknown argument: --unknown')}`);
expect(stderr).toContain('Unknown argument: --unknown');
expect(exitCode).toBe(2);
});

it('suggests the closest match to an unknown flag', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--entyr', './a.js']);
expect(stderr).toContain('Unknown argument: --entyr');
Expand Down

0 comments on commit 60cd78d

Please sign in to comment.