Skip to content

Commit

Permalink
tests: configtest
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jan 4, 2021
1 parent ab856e2 commit 361e806
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/configtest/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@webpack-cli/configtest",
"version": "1.0.0",
"description": "Test your webpack configuration",
"description": "Test your webpack configuration against validation errors.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"license": "MIT",
Expand Down
3 changes: 1 addition & 2 deletions packages/configtest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ class ConfigTestCommand {
process.exit(2);
}

const { options, absolutePath } = await cli.resolveConfig({ config: [configPath] });
const { options } = await cli.resolveConfig({ config: [configPath] });

//eslint-disable-next-line @typescript-eslint/no-explicit-any
const validationErrors: any = validate(options);

if (validationErrors) {
logger.error("Your configuration validation wasn't successful");
logger.error(validationErrors);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/webpack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"@webpack-cli/migrate": {
"optional": true
},
"@webpack-cli/configtest": {
"optional": true
},
"webpack-bundle-analyzer": {
"optional": true
},
Expand Down
39 changes: 39 additions & 0 deletions test/configtest/configtest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const { run } = require('../utils/test-utils');

describe('basic info usage', () => {
it('should validate webpack config successfully', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './webpack.config.js'], false);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
});

it('should throw validation error', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './error.config.js'], false);

expect(exitCode).toBe(2);
expect(stderr).toContain('ValidationError: Invalid configuration object.');
expect(stderr).toContain('configuration.mode should be one of these:');
expect(stdout).toBeFalsy();
});

it(`should validate the config with alias 't'`, () => {
const { exitCode, stderr, stdout } = run(__dirname, ['t', './error.config.js'], false);

expect(exitCode).toBe(2);
expect(stderr).toContain('ValidationError: Invalid configuration object.');
expect(stderr).toContain('configuration.mode should be one of these:');
expect(stdout).toBeFalsy();
});

it('should throw error when multiple configurations are provided simultaneously', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './error.config.js', './webpack.config.js'], false);

expect(exitCode).toBe(2);
expect(stderr).toContain('Only one configuration can be validated at a time');
expect(stdout).toBeFalsy();
});
});
5 changes: 5 additions & 0 deletions test/configtest/error.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
mode: 'dev', // error
target: 'node',
stats: 'normal',
};
1 change: 1 addition & 0 deletions test/configtest/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('configtest command');
5 changes: 5 additions & 0 deletions test/configtest/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
mode: 'development',
target: 'node',
stats: 'verbose',
};
1 change: 1 addition & 0 deletions test/help/help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('help', () => {
expect(stdout.match(/loader\|l/g)).toHaveLength(1);
expect(stdout.match(/migrate\|m/g)).toHaveLength(1);
expect(stdout.match(/plugin\|p/g)).toHaveLength(1);
expect(stdout.match(/configtest\|t/g)).toHaveLength(1);
expect(stdout).toContain("To see list of all supported commands and options run 'webpack --help=verbose'.");
expect(stdout).toContain('CLI documentation: https://webpack.js.org/api/cli/.');
// TODO buggy on windows
Expand Down

0 comments on commit 361e806

Please sign in to comment.