Skip to content

Commit

Permalink
tests: check for proper exit code (#2025)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Nov 4, 2020
1 parent 0038ff2 commit f4a72c5
Show file tree
Hide file tree
Showing 74 changed files with 587 additions and 220 deletions.
6 changes: 4 additions & 2 deletions test/bail/bail.test.js
Expand Up @@ -4,15 +4,17 @@ const { run, runWatch } = require('../utils/test-utils');

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

expect(exitCode).toEqual(0);
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

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

expect(exitCode).toEqual(0);
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});
Expand Down
5 changes: 3 additions & 2 deletions test/cache/cache.test.js
Expand Up @@ -4,13 +4,14 @@ const { run, isWebpack5 } = require('../utils/test-utils');

describe('cache related tests', () => {
it('should log warning in case of single compiler', () => {
let { stderr, stdout } = run(__dirname, ['-c', './webpack.config.js'], false);
let { stderr, stdout, exitCode } = run(__dirname, ['-c', './webpack.config.js'], false);
// run 2nd compilation
({ stderr, stdout } = run(__dirname, ['-c', './webpack.config.js'], false));
({ stderr, stdout, exitCode } = run(__dirname, ['-c', './webpack.config.js'], false));

if (isWebpack5) {
expect(stderr).toContain('starting to restore cache content');
expect(stdout).toContain('[cached]');
expect(exitCode).toEqual(0);
}
});
});
8 changes: 6 additions & 2 deletions test/config-format/coffee/coffee.test.js
Expand Up @@ -5,16 +5,20 @@ const { resolve } = require('path');

describe('webpack cli', () => {
it('should support coffeescript file as flag', () => {
const { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.coffee'], false);
const { stderr, stdout, exitCode } = run(__dirname, ['-c', 'webpack.config.coffee'], false);

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(existsSync(resolve(__dirname, 'dist/foo.bundle.js'))).toBeTruthy();
});

it('should load coffeescript file by default', () => {
const { stderr, stdout } = run(__dirname, [], false);
const { stderr, stdout, exitCode } = run(__dirname, [], false);

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(existsSync(resolve(__dirname, 'dist/foo.bundle.js'))).toBeTruthy();
});
});
4 changes: 3 additions & 1 deletion test/config-format/commonjs/commonjs.test.js
Expand Up @@ -4,9 +4,11 @@ const { resolve } = require('path');

describe('webpack cli', () => {
it('should support CommonJS file', () => {
const { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.cjs'], false);
const { stderr, stdout, exitCode } = run(__dirname, ['-c', 'webpack.config.cjs'], false);

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(existsSync(resolve(__dirname, 'dist/foo.bundle.js'))).toBeTruthy();
});
});
4 changes: 3 additions & 1 deletion test/config-format/typescript/typescript.test.js
Expand Up @@ -8,9 +8,11 @@ describe('webpack cli', () => {
'should support typescript file',
async () => {
await runInstall(__dirname);
const { stderr, stdout } = run(__dirname, ['-c', './webpack.config.ts']);
const { stderr, stdout, exitCode } = run(__dirname, ['-c', './webpack.config.ts']);

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
stat(resolve(__dirname, 'bin/foo.bundle.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
4 changes: 3 additions & 1 deletion test/config-lookup/dotfolder-array/dotfolder-array.test.js
Expand Up @@ -5,9 +5,11 @@ const { run } = require('../../utils/test-utils');

describe('dotfolder array config lookup', () => {
it('should find a webpack array configuration in a dotfolder', (done) => {
const { stdout, stderr } = run(__dirname, [], false);
const { stdout, stderr, exitCode } = run(__dirname, [], false);

expect(stderr).not.toBeUndefined();
expect(stdout).not.toBeUndefined();
expect(exitCode).toBe(0);

stat(resolve(__dirname, './dist/dist-commonjs.js'), (err, stats) => {
expect(err).toBe(null);
Expand Down
4 changes: 3 additions & 1 deletion test/config-lookup/dotfolder-single/dotfolder-single.test.js
Expand Up @@ -7,9 +7,11 @@ const { run } = require('../../utils/test-utils');

describe('dotfolder single config lookup', () => {
it('should find a webpack configuration in a dotfolder', (done) => {
const { stdout, stderr } = run(__dirname, [], false);
const { stdout, stderr, exitCode } = run(__dirname, [], false);

expect(stderr).not.toBeUndefined();
expect(stdout).not.toBeUndefined();
expect(exitCode).toBe(0);

expect(stdout).not.toContain('Module not found');
stat(resolve(__dirname, './dist/main.js'), (err, stats) => {
Expand Down
3 changes: 2 additions & 1 deletion test/config/defaults/all/multiple-config.test.js
Expand Up @@ -5,8 +5,9 @@ const { run } = require('../../../utils/test-utils');

describe('Default configuration files: ', () => {
it('Uses prod config from dot folder if present', (done) => {
const { stdout, stderr } = run(__dirname, [], false);
const { stdout, stderr, exitCode } = run(__dirname, [], false);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).not.toBe(undefined);
stat(resolve(__dirname, './binary/prod.bundle.js'), (err, stats) => {
expect(err).toBe(null);
Expand Down
Expand Up @@ -5,8 +5,9 @@ const { run } = require('../../../utils/test-utils');

describe('multiple dev config files with webpack.config.js', () => {
it('Uses webpack.config.development.js', (done) => {
const { stdout, stderr } = run(__dirname, [], false);
const { stdout, stderr, exitCode } = run(__dirname, [], false);
expect(stderr).toBeFalsy();
expect(exitCode).toEqual(0);
expect(stdout).not.toBe(undefined);
stat(resolve(__dirname, './binary/dev.folder.js'), (err, stats) => {
expect(err).toBe(null);
Expand Down
3 changes: 2 additions & 1 deletion test/config/defaults/none and dev/dev-none-config.test.js
Expand Up @@ -5,8 +5,9 @@ const { run } = require('../../../utils/test-utils');

describe('multiple config files', () => {
it('Uses dev config when both dev and none are present', (done) => {
const { stdout, stderr } = run(__dirname, [], false);
const { stdout, stderr, exitCode } = run(__dirname, [], false);
expect(stderr).toBeFalsy();
expect(exitCode).toEqual(0);
expect(stdout).not.toBe(undefined);
stat(resolve(__dirname, './binary/dev.bundle.js'), (err, stats) => {
expect(err).toBe(null);
Expand Down
3 changes: 2 additions & 1 deletion test/config/defaults/with-mode/multiple-config.test.js
Expand Up @@ -5,8 +5,9 @@ const { run } = require('../../../utils/test-utils');

describe('multiple config files', () => {
it('Uses dev config when development mode is supplied', (done) => {
const { stdout, stderr } = run(__dirname, ['--mode', 'development'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['--mode', 'development'], false);
expect(stderr).toBeFalsy();
expect(exitCode).toEqual(0);
expect(stdout).not.toBe(undefined);
stat(resolve(__dirname, './binary/dev.bundle.js'), (err, stats) => {
expect(err).toBe(null);
Expand Down
4 changes: 3 additions & 1 deletion test/config/type/array-promises/array-promises.test.js
Expand Up @@ -5,9 +5,11 @@ const { run } = require('../../../utils/test-utils');

describe('array of promises', () => {
it('is able to understand a configuration file as a promise', (done) => {
const { stdout, stderr } = run(__dirname, ['-c', './webpack.config.js'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['-c', './webpack.config.js'], false);

expect(stdout).toBeTruthy();
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);

stat(resolve(__dirname, './binary/a-promise.js'), (err, stats) => {
expect(err).toBe(null);
Expand Down
6 changes: 5 additions & 1 deletion test/config/type/array/array.test.js
Expand Up @@ -5,7 +5,11 @@ const { run } = require('../../../utils/test-utils');

describe('array', () => {
it('is able to understand a configuration file in array format', (done) => {
run(__dirname, ['-c', resolve(__dirname, 'webpack.config.js')], false);
const { stderr, stdout, exitCode } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.js')], false);

expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toBeTruthy();
stat(resolve(__dirname, './dist/dist-commonjs.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
31 changes: 23 additions & 8 deletions test/config/type/function-with-env/function-with-env.test.js
Expand Up @@ -12,21 +12,23 @@ describe('function configuration', () => {
expect(exitCode).toEqual(1);
});
it('is able to understand a configuration file as a function', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'isProd']);
const { stderr, stdout, exitCode } = run(__dirname, ['--env', 'isProd']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/prod.js'))).toBeTruthy();
});
it('is able to understand a configuration file as a function', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'isDev']);
const { stderr, stdout, exitCode } = run(__dirname, ['--env', 'isDev']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/dev.js'))).toBeTruthy();
});
it('Supports passing string in env', () => {
const { stderr, stdout } = run(__dirname, [
const { stderr, stdout, exitCode } = run(__dirname, [
'--env',
'environment=production',
'--env',
Expand All @@ -36,11 +38,12 @@ describe('function configuration', () => {
]);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/Luffy.js'))).toBeTruthy();
});
it('Supports long nested values in env', () => {
const { stderr, stdout } = run(__dirname, [
const { stderr, stdout, exitCode } = run(__dirname, [
'--env',
'file.name.is.this=Atsumu',
'--env',
Expand All @@ -50,11 +53,12 @@ describe('function configuration', () => {
]);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/Atsumu.js'))).toBeTruthy();
});
it('Supports multiple equal in a string', () => {
const { stderr, stdout } = run(__dirname, [
const { stderr, stdout, exitCode } = run(__dirname, [
'--env',
'file=name=is=Eren',
'--env',
Expand All @@ -64,27 +68,38 @@ describe('function configuration', () => {
]);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/name=is=Eren.js'))).toBeTruthy();
});
it('Supports dot at the end', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'name.=Hisoka', '--env', 'environment=dot', '-c', 'webpack.env.config.js']);
const { stderr, stdout, exitCode } = run(__dirname, [
'--env',
'name.=Hisoka',
'--env',
'environment=dot',
'-c',
'webpack.env.config.js',
]);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/Hisoka.js'))).toBeTruthy();
});
it('Supports dot at the end', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'name.', '--env', 'environment=dot', '-c', 'webpack.env.config.js']);
const { stderr, stdout, exitCode } = run(__dirname, ['--env', 'name.', '--env', 'environment=dot', '-c', 'webpack.env.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/true.js'))).toBeTruthy();
});
it('is able to understand multiple env flags', (done) => {
const { stderr, stdout } = run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);
const { stderr, stdout, exitCode } = run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
// check that the verbose env is respected
expect(stdout).toContain('LOG from webpack');
// check if the values from DefinePlugin make it to the compiled code
Expand Down
6 changes: 5 additions & 1 deletion test/config/type/function/function.test.js
Expand Up @@ -5,7 +5,11 @@ const { run } = require('../../../utils/test-utils');

describe('function', () => {
it('is able to understand a configuration file as a function', (done) => {
run(__dirname, ['-c', resolve(__dirname, 'webpack.config.js')], false);
const { stderr, stdout, exitCode } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.js')], false);

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
stat(resolve(__dirname, './binary/functor.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
4 changes: 3 additions & 1 deletion test/config/type/promise-array/promise-array.test.js
Expand Up @@ -5,9 +5,11 @@ const { run } = require('../../../utils/test-utils');

describe('promise array', () => {
it('is able to understand a configuration file as a promise', (done) => {
const { stdout, stderr } = run(__dirname, ['-c', './webpack.config.js'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['-c', './webpack.config.js'], false);

expect(stdout).toBeTruthy();
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);

stat(resolve(__dirname, './binary/a-promise.js'), (err, stats) => {
expect(err).toBe(null);
Expand Down
4 changes: 3 additions & 1 deletion test/config/type/promise-function/promise-function.test.js
Expand Up @@ -5,9 +5,11 @@ const { run } = require('../../../utils/test-utils');

describe('promise function', () => {
it('is able to understand a configuration file as a promise', (done) => {
const { stdout, stderr } = run(__dirname, ['-c', './webpack.config.js'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['-c', './webpack.config.js'], false);

expect(stdout).toBeTruthy();
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
stat(resolve(__dirname, './binary/promise.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
4 changes: 3 additions & 1 deletion test/config/type/promise/promise.test.js
Expand Up @@ -5,9 +5,11 @@ const { run } = require('../../../utils/test-utils');

describe('promise', () => {
it('is able to understand a configuration file as a promise', (done) => {
const { stdout, stderr } = run(__dirname, ['-c', './webpack.config.js'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['-c', './webpack.config.js'], false);

expect(stdout).toBeTruthy();
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
stat(resolve(__dirname, './binary/promise.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
3 changes: 2 additions & 1 deletion test/core-flags/amd-flag.test.js
Expand Up @@ -4,9 +4,10 @@ const { run } = require('../utils/test-utils');

describe('--no-amd flag', () => {
it('should accept --no-amd', () => {
const { stderr, stdout } = run(__dirname, ['--no-amd']);
const { stderr, stdout, exitCode } = run(__dirname, ['--no-amd']);

expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain('amd: false');
});
});
6 changes: 4 additions & 2 deletions test/core-flags/bail-flag.test.js
Expand Up @@ -4,16 +4,18 @@ const { run } = require('../utils/test-utils');

describe('--bail flag', () => {
it('should set bail to true', () => {
const { stderr, stdout } = run(__dirname, ['--bail']);
const { stderr, stdout, exitCode } = run(__dirname, ['--bail']);

expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain('bail: true');
});

it('should set bail to false', () => {
const { stderr, stdout } = run(__dirname, ['--no-bail']);
const { stderr, stdout, exitCode } = run(__dirname, ['--no-bail']);

expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain('bail: false');
});
});

0 comments on commit f4a72c5

Please sign in to comment.