From 4cba2feb1e30409adcca5dca858dc80f4349f50a Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Fri, 21 Feb 2020 13:09:59 -0500 Subject: [PATCH 1/3] append error to error.message instead of stack --- __snapshots__/e2e.spec.js | 2 ++ index.js | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/__snapshots__/e2e.spec.js b/__snapshots__/e2e.spec.js index 9f83c23..ad117e8 100644 --- a/__snapshots__/e2e.spec.js +++ b/__snapshots__/e2e.spec.js @@ -22,6 +22,7 @@ Oops...we found an error preparing this test file: The error was: +Error: Webpack Compilation Error ./cypress/tests/e2e/compile-error.js Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: /[cwd]/cypress/tests/e2e/compile-error.js: Unexpected token, expected "," (12:27) @@ -35,6 +36,7 @@ SyntaxError: /[cwd]/cypress/tests/e2e/compile-error.js: Unexpected token, expect @ multi ./cypress/tests/e2e/compile-error.js main[0] + This occurred while Cypress was compiling and bundling your test code. This is usually caused by: - A missing file or dependency diff --git a/index.js b/index.js index 3966903..2bee2d7 100644 --- a/index.js +++ b/index.js @@ -108,8 +108,6 @@ const preprocessor = (options = {}) => { const rejectWithErr = (err) => { err.filePath = filePath - // backup the original stack before it's potentially modified by bluebird - err.originalStack = err.stack debug(`errored bundling ${outputPath}`, err) latestBundle.reject(err) } @@ -125,7 +123,11 @@ const preprocessor = (options = {}) => { if (stats.hasErrors()) { err = new Error('Webpack Compilation Error') - err.stack = jsonStats.errors.join('\n\n') + err.message += `\n${jsonStats.errors + // remove stack trace lines since they're useless for debugging + .map((err) => err.replace(/\n\s*at.*/g, '').replace(/From previous event:\n?/g, '')) + // multiple errors separated by newline + .join('\n\n')}` return rejectWithErr(err) } From 6f16976ab1a565e6e9e90b795a2782abaae05687 Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Fri, 21 Feb 2020 13:24:36 -0500 Subject: [PATCH 2/3] fix bad looking code --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2bee2d7..ddeb002 100644 --- a/index.js +++ b/index.js @@ -123,11 +123,14 @@ const preprocessor = (options = {}) => { if (stats.hasErrors()) { err = new Error('Webpack Compilation Error') - err.message += `\n${jsonStats.errors + + const errorsToAppend = jsonStats.errors // remove stack trace lines since they're useless for debugging .map((err) => err.replace(/\n\s*at.*/g, '').replace(/From previous event:\n?/g, '')) // multiple errors separated by newline - .join('\n\n')}` + .join('\n\n') + + err.message += `\n${errorsToAppend}` return rejectWithErr(err) } From 91a43468579e922df9e501b1fffa500d4724e7ca Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Fri, 21 Feb 2020 13:37:09 -0500 Subject: [PATCH 3/3] add test, link to issue --- __snapshots__/e2e.spec.js | 12 ++++++------ cypress/tests/e2e/compile-error.js | 4 +++- test/e2e/helpers.js | 10 +++++++--- test/unit/index.spec.js | 15 ++++----------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/__snapshots__/e2e.spec.js b/__snapshots__/e2e.spec.js index ad117e8..7c324a0 100644 --- a/__snapshots__/e2e.spec.js +++ b/__snapshots__/e2e.spec.js @@ -25,14 +25,14 @@ The error was: Error: Webpack Compilation Error ./cypress/tests/e2e/compile-error.js Module build failed (from ./node_modules/babel-loader/lib/index.js): -SyntaxError: /[cwd]/cypress/tests/e2e/compile-error.js: Unexpected token, expected "," (12:27) +SyntaxError: /[cwd]/cypress/tests/e2e/compile-error.js: Unexpected token, expected "," (14:27) - 10 | - 11 | describe('foo', ()=>{ -> 12 | it('has syntax error' () => {}}) + 12 | + 13 | describe('foo', ()=>{ +> 14 | it('has syntax error' () => {}}) | ^ - 13 | }) - 14 | + 15 | }) + 16 | @ multi ./cypress/tests/e2e/compile-error.js main[0] diff --git a/cypress/tests/e2e/compile-error.js b/cypress/tests/e2e/compile-error.js index 62452d6..1f983ea 100644 --- a/cypress/tests/e2e/compile-error.js +++ b/cypress/tests/e2e/compile-error.js @@ -5,7 +5,9 @@ /* EXPECT: { expectedResults: { totalFailed: 1 - } + }, + // https://github.com/cypress-io/cypress-webpack-preprocessor/issues/64 + stdoutInclude: 'Webpack Compilation Error' } */ describe('foo', ()=>{ diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index 8a0ce8c..3cc2871 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -65,13 +65,17 @@ exports.runTest = async (options = {}) => { expectedResults: { totalFailed: 0, }, - expectedStdout: null, + stdoutInclude: null, browser: 'electron', exit: true, }) _.merge(opts, parsedSpecOptions) + if (_.isString(opts.stdoutInclude)) { + opts.stdoutInclude = [opts.stdoutInclude] + } + console.log(chalk.cyanBright(`starting test run: ${opts.spec}`)) const stdio = captureStdio(process.stdout) @@ -106,8 +110,8 @@ exports.runTest = async (options = {}) => { expect(res).includes(opts.expectedResults) }) .then(() => { - if (opts.expectedStdout) { - _.forEach(opts.expectedStdout, (v) => { + if (opts.stdoutInclude) { + _.forEach(opts.stdoutInclude, (v) => { expect(stdout).include(v) console.log(`${chalk.bold('run matched stdout:')}\n${v}`) }) diff --git a/test/unit/index.spec.js b/test/unit/index.spec.js index 160cba7..14c16c0 100644 --- a/test/unit/index.spec.js +++ b/test/unit/index.spec.js @@ -273,8 +273,9 @@ describe('webpack preprocessor', function () { }) }) - it('it rejects with joined errors when a stats err', function () { - const errs = ['foo', 'bar', 'baz'] + it('it rejects with joined errors when a stats err and strips stacktrace', function () { + const errs = ['foo\nat Object.foo', 'bar', 'baz'] + const errsNoStack = ['foo', 'bar', 'baz'] this.statsApi = { hasErrors () { @@ -288,15 +289,7 @@ describe('webpack preprocessor', function () { this.compilerApi.run.yields(null, this.statsApi) return this.run().catch((err) => { - expect(err.stack).to.equal(errs.join('\n\n')) - }) - }) - - it('backs up stack as originalStack', function () { - this.compilerApi.run.yields(this.err) - - return this.run().catch((err) => { - expect(err.originalStack).to.equal(this.err.stack) + expect(err.message).to.equal(`Webpack Compilation Error\n${errsNoStack.join('\n\n')}`) }) }) })