Skip to content

Commit

Permalink
test: watch option snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Apr 29, 2021
1 parent f13adae commit af0c647
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 36 deletions.
13 changes: 13 additions & 0 deletions test/utils/test-utils.js
Expand Up @@ -247,6 +247,17 @@ const normalizeError = (output) => {
return output.replace(/SyntaxError: .+/, 'SyntaxError: <error-message>').replace(/\s+at .+(}|\)|\d)/gs, '\n at stack');
};

const normalizeCompileTime = (output) => {
return output.replace(/in \d+ ms/gm, 'in <compile time> ms').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>');
};

const normalizeStdout = (stdout) => {
if (typeof stdout !== 'string') {
return stdout;
Expand All @@ -260,6 +271,8 @@ const normalizeStdout = (stdout) => {
normalizedStdout = normalizeCwd(normalizedStdout);
normalizedStdout = normalizeVersions(normalizedStdout);
normalizedStdout = normalizeError(normalizedStdout);
normalizedStdout = normalizeCompileTime(normalizedStdout);
normalizedStdout = normalizeV4Output(normalizedStdout);

return normalizedStdout;
};
Expand Down
@@ -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"
`;
@@ -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
@@ -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
@@ -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
@@ -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 217 bytes [emitted] (name: main)
./src/first.js 50 bytes [built] [code generated]
first (webpack x.x.x) compiled successfully in <compiler time> ms

second:
asset ./dist-second.js 221 bytes [emitted] (name: main)
./src/second.js 51 bytes [built] [code generated]
second (webpack x.x.x) compiled successfully in <compiler 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 217 bytes [compared for emit] (name: main)
./src/first.js 50 bytes [built] [code generated]
webpack x.x.x compiled successfully in <compiler time> ms"
`;

exports[`"bail" option should log warning in watch mode: stdout 2`] = `
"asset main.js 217 bytes [compared for emit] (name: main)
./src/first.js 50 bytes [built] [code generated]
webpack x.x.x compiled successfully in <compiler 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 217 bytes [emitted] (name: main)
./src/first.js 50 bytes [built] [code generated]
webpack x.x.x compiled successfully in <compiler 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 217 bytes [compared for emit] (name: main)
./src/first.js 50 bytes [built] [code generated]
webpack x.x.x compiled successfully in <compiler time> ms"
`;

exports[`"bail" option should not log warning without the "bail" option: stdout 2`] = `
"asset main.js 217 bytes [compared for emit] (name: main)
./src/first.js 50 bytes [built] [code generated]
webpack x.x.x compiled successfully in <compiler time> ms"
`;
26 changes: 13 additions & 13 deletions test/watch/bail/bail.test.js
@@ -1,47 +1,47 @@
'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 () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'no-bail-webpack.config.js', '--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 () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'no-bail-webpack.config.js', '--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 () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'bail-webpack.config.js', '--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 () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', '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
@@ -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/"
`;
36 changes: 36 additions & 0 deletions test/watch/basic/__snapshots__/basic.test.js.snap.webpack5
@@ -0,0 +1,36 @@
// 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 31 bytes [emitted] [minimized] (name: main)
./src/index.js 32 bytes [built] [code generated]

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/

webpack x.x.x compiled with 1 warning in <compiler time> ms"
`;

0 comments on commit af0c647

Please sign in to comment.