forked from webpack/webpack-cli
/
mode-with-config.test.js
144 lines (123 loc) · 5.81 KB
/
mode-with-config.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
const { stat, readFile } = require('fs');
const { resolve } = require('path');
// eslint-disable-next-line node/no-unpublished-require
const { run } = require('../../utils/test-utils');
describe('mode flags with config', () => {
it('should run in production mode when --mode=production is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--config', './webpack.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
// Should not generate source maps when not specified
stat(resolve(__dirname, './bin/main.js.map'), (err) => {
expect(err).toBeTruthy();
});
// Correct mode should be propagated to the compiler
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('"production mode"');
done();
});
});
it('should run in development mode when --mode=development is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--mode', 'development', '--config', './webpack.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
// Should not generate source maps when not specified
stat(resolve(__dirname, './bin/main.js.map'), (err) => {
expect(err).toBeTruthy();
});
// Correct mode should be propagated to the compiler
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('development mode');
done();
});
});
it('should run in none mode when --mode=none is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--mode', 'none', '--config', './webpack.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
// Should not generate source maps when not specified
stat(resolve(__dirname, './bin/main.js.map'), (err) => {
expect(err).toBeTruthy();
});
// Correct mode should be propagated to the compiler
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('none mode');
done();
});
});
it('should use mode flag over config', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['--mode', 'production', '-c', 'webpack.config2.js']);
expect(stderr).toBeFalsy();
expect(exitCode).toEqual(0);
expect(stdout).toContain(`mode: 'production'`);
});
it('should use mode from flag over NODE_ENV', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['--mode', 'none', '-c', 'webpack.config2.js'], false, [], {
NODE_ENV: 'production',
});
expect(stderr).toBeFalsy();
expect(exitCode).toEqual(0);
expect(stdout).toContain(`mode: 'none'`);
});
it('should use mode from config over NODE_ENV', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['-c', 'webpack.config2.js']);
expect(stderr).toBeFalsy();
expect(exitCode).toEqual(0);
expect(stdout).toContain(`mode: 'development'`);
});
it('should use mode from config when multiple config are supplied', () => {
const { stdout, stderr } = run(__dirname, ['-c', 'webpack.config3.js', '-c', 'webpack.config2.js']);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`mode: 'development'`);
expect(stdout.match(new RegExp("mode: 'development'", 'g')).length).toEqual(1);
});
it('mode flag should apply to all configs', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['--mode', 'none', '-c', './webpack.config3.js', '-c', './webpack.config2.js']);
expect(stderr).toBeFalsy();
expect(exitCode).toEqual(0);
expect(stdout).toContain(`mode: 'none'`);
expect(stdout.match(new RegExp("mode: 'none'", 'g')).length).toEqual(2);
});
it('only config where mode is absent pick up from NODE_ENV', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['-c', './webpack.config3.js', '-c', './webpack.config2.js'], false, [], {
NODE_ENV: 'production',
});
expect(stderr).toBeFalsy();
expect(exitCode).toEqual(0);
expect(stdout).toContain(`mode: 'production'`);
expect(stdout).toContain(`mode: 'development'`);
expect(stdout.match(new RegExp("mode: 'production'", 'g')).length).toEqual(1);
expect(stdout.match(new RegExp("mode: 'development'", 'g')).length).toEqual(1);
});
});