Skip to content

Commit 04c80df

Browse files
committed
test: watch option snapshot
1 parent 45b040d commit 04c80df

13 files changed

+318
-42
lines changed

test/utils/test-utils.js

+13
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@ const normalizeError = (output) => {
251251
.replace(/\s+at .+(}|\)|\d)/gs, "\n at stack");
252252
};
253253

254+
const normalizeCompileTime = (output) => {
255+
return output.replace(/in \d+ ms/gm, "in <compile time> ms").replace();
256+
};
257+
258+
const normalizeV4Output = (output) => {
259+
return output
260+
.replace(/Hash: .*/gm, "Hash: <hash>")
261+
.replace(/Time: .*/gm, "Time: <compile time>")
262+
.replace(/Built at: .*/gm, "Built at: <built time>");
263+
};
264+
254265
const normalizeStdout = (stdout) => {
255266
if (typeof stdout !== "string") {
256267
return stdout;
@@ -264,6 +275,8 @@ const normalizeStdout = (stdout) => {
264275
normalizedStdout = normalizeCwd(normalizedStdout);
265276
normalizedStdout = normalizeVersions(normalizedStdout);
266277
normalizedStdout = normalizeError(normalizedStdout);
278+
normalizedStdout = normalizeCompileTime(normalizedStdout);
279+
normalizedStdout = normalizeV4Output(normalizedStdout);
267280

268281
return normalizedStdout;
269282
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stderr 1`] = `""`;
4+
5+
exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stdout 1`] = `
6+
"Webpack Bundle Analyzer is started
7+
at stack
8+
Use Ctrl+C to close it"
9+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stderr 1`] = `""`;
4+
5+
exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stdout 1`] = `
6+
"Webpack Bundle Analyzer is started
7+
at stack
8+
Use Ctrl+C to close it"
9+
`;
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"use strict";
22

3-
const { runWatch } = require("../../utils/test-utils");
3+
const { runWatch, normalizeStderr, normalizeStdout } = require("../../utils/test-utils");
44

55
describe('"analyze" option', () => {
66
it("should load webpack-bundle-analyzer plugin with --analyze flag", async () => {
77
const { stderr, stdout } = await runWatch(__dirname, ["--analyze"], {
88
killString: /Webpack Bundle Analyzer is started at/,
99
});
1010

11-
expect(stderr).toBeFalsy();
12-
expect(stdout).toContain("Webpack Bundle Analyzer is started at");
11+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
12+
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
1313
});
1414
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`"bail" option should log warning in case of multiple compilers: stderr 1`] = `"<w> [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`;
4+
5+
exports[`"bail" option should log warning in case of multiple compilers: stdout 1`] = `
6+
"Hash: <hash>
7+
Version: webpack x.x.x
8+
Child first:
9+
Hash: <hash>
10+
Time: <compile time>
11+
Built at: <built time>
12+
Asset Size Chunks Chunk Names
13+
./dist-first.js 3.77 KiB main [emitted] main
14+
Entrypoint main = ./dist-first.js
15+
[./src/first.js] 50 bytes {main} [built]
16+
Child second:
17+
Hash: <hash>
18+
Time: <compile time>
19+
Built at: <built time>
20+
Asset Size Chunks Chunk Names
21+
./dist-second.js 3.77 KiB main [emitted] main
22+
Entrypoint main = ./dist-second.js
23+
[./src/second.js] 51 bytes {main} [built]"
24+
`;
25+
26+
exports[`"bail" option should log warning in watch mode: stderr 1`] = `"<w> [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`;
27+
28+
exports[`"bail" option should log warning in watch mode: stderr 2`] = `"<w> [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`;
29+
30+
exports[`"bail" option should log warning in watch mode: stdout 1`] = `
31+
"Hash: <hash>
32+
Version: webpack x.x.x
33+
Time: <compile time>
34+
Built at: <built time>
35+
Asset Size Chunks Chunk Names
36+
main.js 3.77 KiB main [emitted] main
37+
Entrypoint main = main.js
38+
[./src/first.js] 50 bytes {main} [built]"
39+
`;
40+
41+
exports[`"bail" option should log warning in watch mode: stdout 2`] = `
42+
"Hash: <hash>
43+
Version: webpack x.x.x
44+
Time: <compile time>
45+
Built at: <built time>
46+
Asset Size Chunks Chunk Names
47+
main.js 3.77 KiB main [emitted] main
48+
Entrypoint main = main.js
49+
[./src/first.js] 50 bytes {main} [built]"
50+
`;
51+
52+
exports[`"bail" option should not log warning in not watch mode without the "watch" option: stderr 1`] = `""`;
53+
54+
exports[`"bail" option should not log warning in not watch mode without the "watch" option: stdout 1`] = `
55+
"Hash: <hash>
56+
Version: webpack x.x.x
57+
Time: <compile time>
58+
Built at: <built time>
59+
Asset Size Chunks Chunk Names
60+
main.js 3.77 KiB main [emitted] main
61+
Entrypoint main = main.js
62+
[./src/first.js] 50 bytes {main} [built]"
63+
`;
64+
65+
exports[`"bail" option should not log warning without the "bail" option: stderr 1`] = `""`;
66+
67+
exports[`"bail" option should not log warning without the "bail" option: stderr 2`] = `""`;
68+
69+
exports[`"bail" option should not log warning without the "bail" option: stdout 1`] = `
70+
"Hash: <hash>
71+
Version: webpack x.x.x
72+
Time: <compile time>
73+
Built at: <built time>
74+
Asset Size Chunks Chunk Names
75+
main.js 3.77 KiB main [emitted] main
76+
Entrypoint main = main.js
77+
[./src/first.js] 50 bytes {main} [built]"
78+
`;
79+
80+
exports[`"bail" option should not log warning without the "bail" option: stdout 2`] = `
81+
"Hash: <hash>
82+
Version: webpack x.x.x
83+
Time: <compile time>
84+
Built at: <built time>
85+
Asset Size Chunks Chunk Names
86+
main.js 3.77 KiB main [emitted] main
87+
Entrypoint main = main.js
88+
[./src/first.js] 50 bytes {main} [built]"
89+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`"bail" option should log warning in case of multiple compilers: stderr 1`] = `"<w> [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`;
4+
5+
exports[`"bail" option should log warning in case of multiple compilers: stdout 1`] = `
6+
"first:
7+
asset ./dist-first.js 217 bytes [emitted] (name: main)
8+
./src/first.js 50 bytes [built] [code generated]
9+
first (webpack x.x.x) compiled successfully in <compiler time> ms
10+
11+
second:
12+
asset ./dist-second.js 221 bytes [emitted] (name: main)
13+
./src/second.js 51 bytes [built] [code generated]
14+
second (webpack x.x.x) compiled successfully in <compiler time> ms"
15+
`;
16+
17+
exports[`"bail" option should log warning in watch mode: stderr 1`] = `"<w> [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`;
18+
19+
exports[`"bail" option should log warning in watch mode: stderr 2`] = `"<w> [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`;
20+
21+
exports[`"bail" option should log warning in watch mode: stdout 1`] = `
22+
"asset main.js 217 bytes [compared for emit] (name: main)
23+
./src/first.js 50 bytes [built] [code generated]
24+
webpack x.x.x compiled successfully in <compiler time> ms"
25+
`;
26+
27+
exports[`"bail" option should log warning in watch mode: stdout 2`] = `
28+
"asset main.js 217 bytes [compared for emit] (name: main)
29+
./src/first.js 50 bytes [built] [code generated]
30+
webpack x.x.x compiled successfully in <compiler time> ms"
31+
`;
32+
33+
exports[`"bail" option should not log warning in not watch mode without the "watch" option: stderr 1`] = `""`;
34+
35+
exports[`"bail" option should not log warning in not watch mode without the "watch" option: stdout 1`] = `
36+
"asset main.js 217 bytes [emitted] (name: main)
37+
./src/first.js 50 bytes [built] [code generated]
38+
webpack x.x.x compiled successfully in <compiler time> ms"
39+
`;
40+
41+
exports[`"bail" option should not log warning without the "bail" option: stderr 1`] = `""`;
42+
43+
exports[`"bail" option should not log warning without the "bail" option: stderr 2`] = `""`;
44+
45+
exports[`"bail" option should not log warning without the "bail" option: stdout 1`] = `
46+
"asset main.js 217 bytes [compared for emit] (name: main)
47+
./src/first.js 50 bytes [built] [code generated]
48+
webpack x.x.x compiled successfully in <compiler time> ms"
49+
`;
50+
51+
exports[`"bail" option should not log warning without the "bail" option: stdout 2`] = `
52+
"asset main.js 217 bytes [compared for emit] (name: main)
53+
./src/first.js 50 bytes [built] [code generated]
54+
webpack x.x.x compiled successfully in <compiler time> ms"
55+
`;

test/watch/bail/bail.test.js

+13-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"use strict";
22

3-
const { runWatch } = require("../../utils/test-utils");
3+
const { runWatch, normalizeStderr, normalizeStdout } = require("../../utils/test-utils");
44

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

9-
expect(stderr).toBeFalsy();
10-
expect(stdout).toBeTruthy();
9+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
10+
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
1111
});
1212

1313
it('should not log warning without the "bail" option', async () => {
@@ -17,8 +17,8 @@ describe('"bail" option', () => {
1717
"--watch",
1818
]);
1919

20-
expect(stderr).toBeFalsy();
21-
expect(stdout).toBeTruthy();
20+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
21+
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
2222
});
2323

2424
it('should not log warning without the "bail" option', async () => {
@@ -28,8 +28,8 @@ describe('"bail" option', () => {
2828
"--watch",
2929
]);
3030

31-
expect(stderr).toBeFalsy();
32-
expect(stdout).toBeTruthy();
31+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
32+
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
3333
});
3434

3535
it("should log warning in watch mode", async () => {
@@ -39,10 +39,8 @@ describe('"bail" option', () => {
3939
"--watch",
4040
]);
4141

42-
expect(stderr).toContain(
43-
`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`,
44-
);
45-
expect(stdout).toBeTruthy();
42+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
43+
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
4644
});
4745

4846
it("should log warning in watch mode", async () => {
@@ -51,18 +49,14 @@ describe('"bail" option', () => {
5149
"bail-and-watch-webpack.config.js",
5250
]);
5351

54-
expect(stderr).toContain(
55-
`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`,
56-
);
57-
expect(stdout).toBeTruthy();
52+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
53+
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
5854
});
5955

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

63-
expect(stderr).toContain(
64-
`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`,
65-
);
66-
expect(stdout).toBeTruthy();
59+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
60+
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
6761
});
6862
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`basic should log supplied config with watch: stderr 1`] = `
4+
" [webpack-cli] Compiler starting...
5+
[webpack-cli] Compiler is using config: '<cwd>/test/watch/basic/log.config.js'
6+
[webpack-cli] Compiler finished
7+
"
8+
`;
9+
10+
exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stderr 1`] = `
11+
"[webpack-cli] Error: Unknown option '--no-watch'
12+
[webpack-cli] Run 'webpack --help' to see available commands and options"
13+
`;
14+
15+
exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stdout 1`] = `""`;
16+
17+
exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stderr 1`] = `
18+
"[webpack-cli] Error: Unknown option '--watch'
19+
[webpack-cli] Run 'webpack --help' to see available commands and options"
20+
`;
21+
22+
exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stdout 1`] = `""`;
23+
24+
exports[`basic should work with negative value: stderr 1`] = `""`;
25+
26+
exports[`basic should work with negative value: stdout 1`] = `
27+
"Hash: <hash>
28+
Version: webpack x.x.x
29+
Time: <compile time>
30+
Built at: <built time>
31+
Asset Size Chunks Chunk Names
32+
main.js 960 bytes 0 [emitted] main
33+
Entrypoint main = main.js
34+
[0] ./src/index.js 32 bytes {0} [built]
35+
36+
WARNING in configuration
37+
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
38+
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/"
39+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`basic should log supplied config with watch: stderr 1`] = `
4+
" [webpack-cli] Compiler starting...
5+
[webpack-cli] Compiler is using config: '<cwd>/test/watch/basic/log.config.js'
6+
[webpack-cli] Compiler finished
7+
"
8+
`;
9+
10+
exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stderr 1`] = `
11+
"[webpack-cli] Error: Unknown option '--no-watch'
12+
[webpack-cli] Run 'webpack --help' to see available commands and options"
13+
`;
14+
15+
exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stdout 1`] = `""`;
16+
17+
exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stderr 1`] = `
18+
"[webpack-cli] Error: Unknown option '--watch'
19+
[webpack-cli] Run 'webpack --help' to see available commands and options"
20+
`;
21+
22+
exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stdout 1`] = `""`;
23+
24+
exports[`basic should work with negative value: stderr 1`] = `""`;
25+
26+
exports[`basic should work with negative value: stdout 1`] = `
27+
"asset main.js 31 bytes [emitted] [minimized] (name: main)
28+
./src/index.js 32 bytes [built] [code generated]
29+
30+
WARNING in configuration
31+
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
32+
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
33+
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
34+
35+
webpack x.x.x compiled with 1 warning in <compiler time> ms"
36+
`;

0 commit comments

Comments
 (0)