Skip to content

Commit

Permalink
feat: configtest
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jan 5, 2021
1 parent 2de58fd commit b94c067
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -10,3 +10,4 @@ test/typescript/webpack.config.ts
test/config/error-commonjs/syntax-error.js
test/config/error-mjs/syntax-error.mjs
test/config/error-array/webpack.config.js
test/configtest/syntax-error.config.js
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -9,3 +9,4 @@ test/config/error-mjs/syntax-error.mjs
packages/webpack-cli/__tests__/test-assets/.yo-rc.json
test/build-errors/stats.json
packages/**/lib
test/configtest/syntax-error.config.js
18 changes: 13 additions & 5 deletions packages/configtest/src/index.ts
@@ -1,4 +1,4 @@
import { validate } from 'webpack';
import webpack from 'webpack';

class ConfigTestCommand {
async apply(cli): Promise<void> {
Expand All @@ -16,15 +16,23 @@ class ConfigTestCommand {
async (configPath: string) => {
const { options } = await cli.resolveConfig({ config: [configPath] });

const isValidationError = (error) => {
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
const ValidationError: any = webpack.ValidationError || webpack.WebpackOptionsValidationError;

return error instanceof ValidationError;
};

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

if (validationErrors) {
logger.error(validationErrors);
if (error) {
logger.error(isValidationError(error) ? error.message : error);
process.exit(2);
}

logger.success('There are no validation errors in the given webpack configuration.');
logger.success('There are no errors in the given webpack configuration.');
},
);
}
Expand Down
10 changes: 9 additions & 1 deletion test/configtest/configtest.test.js
Expand Up @@ -8,7 +8,7 @@ describe('basic info usage', () => {

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

it('should throw validation error', () => {
Expand All @@ -20,6 +20,14 @@ describe('basic info usage', () => {
expect(stdout).toBeFalsy();
});

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

expect(exitCode).toBe(2);
expect(stderr).toContain(`SyntaxError: Unexpected token ';'`);
expect(stdout).toBeFalsy();
});

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

Expand Down
5 changes: 5 additions & 0 deletions test/configtest/syntax-error.config.js
@@ -0,0 +1,5 @@
module.exports = {
name: 'config-error',
mode: 'development',
target: 'node'; //SyntaxError: Unexpected token ';'
};

0 comments on commit b94c067

Please sign in to comment.