Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

fix: fix compilation error message #63

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions __snapshots__/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ 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)
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]


This occurred while Cypress was compiling and bundling your test code. This is usually caused by:

- A missing file or dependency
Expand Down
4 changes: 3 additions & 1 deletion cypress/tests/e2e/compile-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
/* EXPECT: {
expectedResults: {
totalFailed: 1
}
},
// https://github.com/cypress-io/cypress-webpack-preprocessor/issues/64
stdoutInclude: 'Webpack Compilation Error'
} */

describe('foo', ()=>{
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -125,7 +123,14 @@ const preprocessor = (options = {}) => {

if (stats.hasErrors()) {
err = new Error('Webpack Compilation Error')
err.stack = jsonStats.errors.join('\n\n')

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, ''))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this same logic is used in test runner to strip stack trace lines, so we're just moving this logic up
https://github.com/cypress-io/cypress/blob/master/packages/server/lib/plugins/preprocessor.coffee#L16

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ughh, this is so so weird - why have 3 lines of code inside a string template?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint auto-fixed to that when i tried to add '\n'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

// multiple errors separated by newline
.join('\n\n')

err.message += `\n${errorsToAppend}`

return rejectWithErr(err)
}
Expand Down
10 changes: 7 additions & 3 deletions test/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}`)
})
Expand Down
15 changes: 4 additions & 11 deletions test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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')}`)
})
})
})
Expand Down