Skip to content

Commit

Permalink
refactor: remove --mode flag validation (#1744)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Aug 17, 2020
1 parent a3331f8 commit 51d27c6
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 75 deletions.
8 changes: 0 additions & 8 deletions packages/webpack-cli/__tests__/ZeroConfigGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,4 @@ describe('ZeroConfigGroup', function () {
expect(result.options).toMatchObject({ mode: 'none' });
expect(result.options.mode).toEqual('none');
});

it('should set mode=production by default', () => {
const group = new ZeroConfigGroup([{}]);

const result = group.run();
// ensure no other properties are added
expect(result.options).toMatchObject({ mode: 'production' });
});
});
7 changes: 0 additions & 7 deletions packages/webpack-cli/lib/groups/ZeroConfigGroup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const GroupHelper = require('../utils/GroupHelper');
const logger = require('../utils/logger');

const PRODUCTION = 'production';
const DEVELOPMENT = 'development';
const NONE = 'none';
/**
* ZeroConfigGroup creates a zero configuration based on the environment
*/
Expand All @@ -21,13 +19,8 @@ class ZeroConfigGroup extends GroupHelper {
return process.env.NODE_ENV;
} else {
if (this.args.mode) {
if (this.args.mode !== PRODUCTION && this.args.mode !== DEVELOPMENT && this.args.mode !== NONE) {
logger.warn('You provided an invalid value for "mode" option. Using "production" by default');
return PRODUCTION;
}
return this.args.mode;
}
return PRODUCTION;
}
}

Expand Down
64 changes: 9 additions & 55 deletions test/mode/mode-single-arg/mode-single-arg.test.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,34 @@
'use strict';
const { run } = require('../../utils/test-utils');
const { stat, readFile } = require('fs');
const { resolve } = require('path');

describe('mode flags', () => {
it('should set mode=production by default', (done) => {
it('should set mode=production by default', () => {
const { stderr, stdout } = run(__dirname);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`mode: 'production'`);

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('production test');
done();
});
});

it('should load a development config when --mode=development is passed', (done) => {
it('should load a development config when --mode=development is passed', () => {
const { stderr, stdout } = run(__dirname, ['--mode', 'development']);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`mode: 'development'`);

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should load a production config when --mode=production is passed', (done) => {
it('should load a production config when --mode=production is passed', () => {
const { stderr, stdout } = run(__dirname, ['--mode', 'production']);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`mode: 'production'`);

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('production test');
done();
});
});

it('should load a none config when --mode=none is passed', (done) => {
it('should load a none config when --mode=none is passed', () => {
const { stderr, stdout } = run(__dirname, ['--mode', 'none']);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`mode: 'none'`);

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should show warning and load a production config when --mode=abcd is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--mode', 'abcd']);
expect(stderr).toContain('invalid value for "mode" option. Using "production" by default');
expect(stdout).toContain(`mode: 'production'`);

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('production test');
done();
});
it('should throw error when --mode=abcd is passed', () => {
const { stderr } = run(__dirname, ['--mode', 'abcd']);
expect(stderr).toContain('configuration.mode should be one of these');
expect(stderr).toContain(`"development" | "production" | "none"`);
});
});
5 changes: 0 additions & 5 deletions test/mode/mode-single-arg/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions test/mode/mode-single-arg/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('test');

0 comments on commit 51d27c6

Please sign in to comment.