Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: watch option snapshot #2679

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions test/utils/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ const normalizeError = (output) => {
.replace(/\s+at .+(}|\)|\d)/gs, "\n at stack");
};

const normalizeTime = (output) => {
return output.replace(/[\d.]+ ms/gm, "<compile-time> ms").replace();
};

const normalizeSize = (output) => {
return output
.replace(/\d+(.\d+)? (bytes|KiB|MiB|GiB)/g, "<size> <size-abbreviation>")
.replace();
};

const normalizeV4Output = (output) => {
return output
.replace(/Hash: .*/gm, "Hash: <hash>")
.replace(/Time: .*/gm, "Time: <compile time>")
.replace(/Built at: .*/gm, "Built at: <built time>");
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put normalizeCompileTime in normalizeV4Output, ideally we should have two function normalizeStatsV4 and normalizeStatsV5, v5 default output are simple so we should not have big problems here, If you need I can implement these functions so you will be able to continue adding snapshots and improve normalizeStats function using regexps

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep help appreciated, also something wrong with v4 snapshot, thanks!


const normalizeStdout = (stdout) => {
if (typeof stdout !== "string") {
return stdout;
Expand All @@ -261,8 +278,12 @@ const normalizeStdout = (stdout) => {
}

let normalizedStdout = stripAnsi(stdout);

normalizedStdout = normalizeCwd(normalizedStdout);
normalizedStdout = normalizeVersions(normalizedStdout);
normalizedStdout = normalizeTime(normalizedStdout);
normalizedStdout = normalizeSize(normalizedStdout);
normalizedStdout = normalizeV4Output(normalizedStdout);
normalizedStdout = normalizeError(normalizedStdout);

return normalizedStdout;
Expand All @@ -278,6 +299,7 @@ const normalizeStderr = (stderr) => {
}

let normalizedStderr = stripAnsi(stderr);

normalizedStderr = normalizeCwd(normalizedStderr);

const networkIPv4 = internalIp.v4.sync();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stderr 1`] = `""`;

exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stdout 1`] = `
"Webpack Bundle Analyzer is started
at stack
Use Ctrl+C to close it"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stderr 1`] = `""`;

exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stdout 1`] = `
"Webpack Bundle Analyzer is started
at stack
Use Ctrl+C to close it"
`;
6 changes: 3 additions & 3 deletions test/watch/analyze/analyze-flag.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use strict";

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

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

expect(stderr).toBeFalsy();
expect(stdout).toContain("Webpack Bundle Analyzer is started at");
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});
});
89 changes: 89 additions & 0 deletions test/watch/bail/__snapshots__/bail.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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."`;

exports[`"bail" option should log warning in case of multiple compilers: stdout 1`] = `
"Hash: <hash>
Version: webpack x.x.x
Child first:
Hash: <hash>
Time: <compile time>
Built at: <built time>
Asset Size Chunks Chunk Names
./dist-first.js 3.77 KiB main [emitted] main
Entrypoint main = ./dist-first.js
[./src/first.js] 50 bytes {main} [built]
Child second:
Hash: <hash>
Time: <compile time>
Built at: <built time>
Asset Size Chunks Chunk Names
./dist-second.js 3.77 KiB main [emitted] main
Entrypoint main = ./dist-second.js
[./src/second.js] 51 bytes {main} [built]"
`;

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."`;

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."`;

exports[`"bail" option should log warning in watch mode: stdout 1`] = `
"Hash: <hash>
Version: webpack x.x.x
Time: <compile time>
Built at: <built time>
Asset Size Chunks Chunk Names
main.js 3.77 KiB main [emitted] main
Entrypoint main = main.js
[./src/first.js] 50 bytes {main} [built]"
`;

exports[`"bail" option should log warning in watch mode: stdout 2`] = `
"Hash: <hash>
Version: webpack x.x.x
Time: <compile time>
Built at: <built time>
Asset Size Chunks Chunk Names
main.js 3.77 KiB main [emitted] main
Entrypoint main = main.js
[./src/first.js] 50 bytes {main} [built]"
`;

exports[`"bail" option should not log warning in not watch mode without the "watch" option: stderr 1`] = `""`;

exports[`"bail" option should not log warning in not watch mode without the "watch" option: stdout 1`] = `
"Hash: <hash>
Version: webpack x.x.x
Time: <compile time>
Built at: <built time>
Asset Size Chunks Chunk Names
main.js 3.77 KiB main [emitted] main
Entrypoint main = main.js
[./src/first.js] 50 bytes {main} [built]"
`;

exports[`"bail" option should not log warning without the "bail" option: stderr 1`] = `""`;

exports[`"bail" option should not log warning without the "bail" option: stderr 2`] = `""`;

exports[`"bail" option should not log warning without the "bail" option: stdout 1`] = `
"Hash: <hash>
Version: webpack x.x.x
Time: <compile time>
Built at: <built time>
Asset Size Chunks Chunk Names
main.js 3.77 KiB main [emitted] main
Entrypoint main = main.js
[./src/first.js] 50 bytes {main} [built]"
`;

exports[`"bail" option should not log warning without the "bail" option: stdout 2`] = `
"Hash: <hash>
Version: webpack x.x.x
Time: <compile time>
Built at: <built time>
Asset Size Chunks Chunk Names
main.js 3.77 KiB main [emitted] main
Entrypoint main = main.js
[./src/first.js] 50 bytes {main} [built]"
`;
55 changes: 55 additions & 0 deletions test/watch/bail/__snapshots__/bail.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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."`;

exports[`"bail" option should log warning in case of multiple compilers: stdout 1`] = `
"first:
asset ./dist-first.js <size> <size-abbreviation> [emitted] (name: main)
./src/first.js <size> <size-abbreviation> [built] [code generated]
first (webpack x.x.x) compiled successfully in <compile-time> ms

second:
asset ./dist-second.js <size> <size-abbreviation> [emitted] (name: main)
./src/second.js <size> <size-abbreviation> [built] [code generated]
second (webpack x.x.x) compiled successfully in <compile-time> ms"
`;

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."`;

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."`;

exports[`"bail" option should log warning in watch mode: stdout 1`] = `
"asset main.js <size> <size-abbreviation> [compared for emit] (name: main)
./src/first.js <size> <size-abbreviation> [built] [code generated]
webpack x.x.x compiled successfully in <compile-time> ms"
`;

exports[`"bail" option should log warning in watch mode: stdout 2`] = `
"asset main.js <size> <size-abbreviation> [compared for emit] (name: main)
./src/first.js <size> <size-abbreviation> [built] [code generated]
webpack x.x.x compiled successfully in <compile-time> ms"
`;

exports[`"bail" option should not log warning in not watch mode without the "watch" option: stderr 1`] = `""`;

exports[`"bail" option should not log warning in not watch mode without the "watch" option: stdout 1`] = `
"asset main.js <size> <size-abbreviation> [emitted] (name: main)
./src/first.js <size> <size-abbreviation> [built] [code generated]
webpack x.x.x compiled successfully in <compile-time> ms"
`;

exports[`"bail" option should not log warning without the "bail" option: stderr 1`] = `""`;

exports[`"bail" option should not log warning without the "bail" option: stderr 2`] = `""`;

exports[`"bail" option should not log warning without the "bail" option: stdout 1`] = `
"asset main.js <size> <size-abbreviation> [compared for emit] (name: main)
./src/first.js <size> <size-abbreviation> [built] [code generated]
webpack x.x.x compiled successfully in <compile-time> ms"
`;

exports[`"bail" option should not log warning without the "bail" option: stdout 2`] = `
"asset main.js <size> <size-abbreviation> [compared for emit] (name: main)
./src/first.js <size> <size-abbreviation> [built] [code generated]
webpack x.x.x compiled successfully in <compile-time> ms"
`;
32 changes: 13 additions & 19 deletions test/watch/bail/bail.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

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

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

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});

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

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});

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

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});

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

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

it("should log warning in watch mode", async () => {
Expand All @@ -51,18 +49,14 @@ describe('"bail" option', () => {
"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(stdout).toBeTruthy();
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});

it("should log warning in case of multiple compilers", async () => {
const { stderr, stdout } = 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(stdout).toBeTruthy();
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});
});
39 changes: 39 additions & 0 deletions test/watch/basic/__snapshots__/basic.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`basic should log supplied config with watch: stderr 1`] = `
" [webpack-cli] Compiler starting...
[webpack-cli] Compiler is using config: '<cwd>/test/watch/basic/log.config.js'
[webpack-cli] Compiler finished
"
`;

exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--no-watch'
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stdout 1`] = `""`;

exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--watch'
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stdout 1`] = `""`;

exports[`basic should work with negative value: stderr 1`] = `""`;

exports[`basic should work with negative value: stdout 1`] = `
"Hash: <hash>
Version: webpack x.x.x
Time: <compile time>
Built at: <built time>
Asset Size Chunks Chunk Names
main.js 960 bytes 0 [emitted] main
Entrypoint main = main.js
[0] ./src/index.js 32 bytes {0} [built]

WARNING in configuration
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.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/"
`;
30 changes: 30 additions & 0 deletions test/watch/basic/__snapshots__/basic.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`basic should log supplied config with watch: stderr 1`] = `
" [webpack-cli] Compiler starting...
[webpack-cli] Compiler is using config: '<cwd>/test/watch/basic/log.config.js'
[webpack-cli] Compiler finished
"
`;

exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--no-watch'
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stdout 1`] = `""`;

exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--watch'
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stdout 1`] = `""`;

exports[`basic should work with negative value: stderr 1`] = `""`;

exports[`basic should work with negative value: stdout 1`] = `
"asset main.js <size> <size-abbreviation> [emitted] (name: main)
./src/index.js <size> <size-abbreviation> [built] [code generated]
webpack x.x.x compiled successfully in <compile-time> ms"
`;
Loading