From b94c067f126f64e356a36df212db6e6f1cdbec40 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 5 Jan 2021 18:13:22 +0530 Subject: [PATCH] feat: configtest --- .eslintignore | 1 + .prettierignore | 1 + packages/configtest/src/index.ts | 18 +++++++++++++----- test/configtest/configtest.test.js | 10 +++++++++- test/configtest/syntax-error.config.js | 5 +++++ 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 test/configtest/syntax-error.config.js diff --git a/.eslintignore b/.eslintignore index ec220a0fdec..97edf222a8a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -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 diff --git a/.prettierignore b/.prettierignore index 7b3b7544c17..8f68c1e421c 100644 --- a/.prettierignore +++ b/.prettierignore @@ -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 diff --git a/packages/configtest/src/index.ts b/packages/configtest/src/index.ts index 4fc6bc5f068..6f93369d526 100644 --- a/packages/configtest/src/index.ts +++ b/packages/configtest/src/index.ts @@ -1,4 +1,4 @@ -import { validate } from 'webpack'; +import webpack from 'webpack'; class ConfigTestCommand { async apply(cli): Promise { @@ -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.'); }, ); } diff --git a/test/configtest/configtest.test.js b/test/configtest/configtest.test.js index 0831bc2a39a..59c6f34bdfa 100644 --- a/test/configtest/configtest.test.js +++ b/test/configtest/configtest.test.js @@ -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', () => { @@ -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); diff --git a/test/configtest/syntax-error.config.js b/test/configtest/syntax-error.config.js new file mode 100644 index 00000000000..96fc2e63b73 --- /dev/null +++ b/test/configtest/syntax-error.config.js @@ -0,0 +1,5 @@ +module.exports = { + name: 'config-error', + mode: 'development', + target: 'node'; //SyntaxError: Unexpected token ';' +};