Skip to content

Commit 1d6e229

Browse files
authored
fix: fix compilation error message (cypress-io/cypress-webpack-preprocessor#63)
* append error to error.message instead of stack
1 parent 7b247d9 commit 1d6e229

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

__snapshots__/e2e.spec.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ Oops...we found an error preparing this test file:
2222
2323
The error was:
2424
25+
Error: Webpack Compilation Error
2526
./cypress/tests/e2e/compile-error.js
2627
Module build failed (from ./node_modules/babel-loader/lib/index.js):
27-
SyntaxError: /[cwd]/cypress/tests/e2e/compile-error.js: Unexpected token, expected "," (12:27)
28+
SyntaxError: /[cwd]/cypress/tests/e2e/compile-error.js: Unexpected token, expected "," (14:27)
2829
29-
10 |
30-
11 | describe('foo', ()=>{
31-
> 12 | it('has syntax error' () => {}})
30+
12 |
31+
13 | describe('foo', ()=>{
32+
> 14 | it('has syntax error' () => {}})
3233
| ^
33-
13 | })
34-
14 |
34+
15 | })
35+
16 |
3536
3637
@ multi ./cypress/tests/e2e/compile-error.js main[0]
3738
39+
3840
This occurred while Cypress was compiling and bundling your test code. This is usually caused by:
3941
4042
- A missing file or dependency

cypress/tests/e2e/compile-error.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
/* EXPECT: {
66
expectedResults: {
77
totalFailed: 1
8-
}
8+
},
9+
// https://github.com/cypress-io/cypress-webpack-preprocessor/issues/64
10+
stdoutInclude: 'Webpack Compilation Error'
911
} */
1012

1113
describe('foo', ()=>{

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ const preprocessor = (options = {}) => {
108108

109109
const rejectWithErr = (err) => {
110110
err.filePath = filePath
111-
// backup the original stack before it's potentially modified by bluebird
112-
err.originalStack = err.stack
113111
debug(`errored bundling ${outputPath}`, err)
114112
latestBundle.reject(err)
115113
}
@@ -125,7 +123,14 @@ const preprocessor = (options = {}) => {
125123

126124
if (stats.hasErrors()) {
127125
err = new Error('Webpack Compilation Error')
128-
err.stack = jsonStats.errors.join('\n\n')
126+
127+
const errorsToAppend = jsonStats.errors
128+
// remove stack trace lines since they're useless for debugging
129+
.map((err) => err.replace(/\n\s*at.*/g, '').replace(/From previous event:\n?/g, ''))
130+
// multiple errors separated by newline
131+
.join('\n\n')
132+
133+
err.message += `\n${errorsToAppend}`
129134

130135
return rejectWithErr(err)
131136
}

test/e2e/helpers.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,17 @@ exports.runTest = async (options = {}) => {
6565
expectedResults: {
6666
totalFailed: 0,
6767
},
68-
expectedStdout: null,
68+
stdoutInclude: null,
6969
browser: 'electron',
7070
exit: true,
7171
})
7272

7373
_.merge(opts, parsedSpecOptions)
7474

75+
if (_.isString(opts.stdoutInclude)) {
76+
opts.stdoutInclude = [opts.stdoutInclude]
77+
}
78+
7579
console.log(chalk.cyanBright(`starting test run: ${opts.spec}`))
7680

7781
const stdio = captureStdio(process.stdout)
@@ -106,8 +110,8 @@ exports.runTest = async (options = {}) => {
106110
expect(res).includes(opts.expectedResults)
107111
})
108112
.then(() => {
109-
if (opts.expectedStdout) {
110-
_.forEach(opts.expectedStdout, (v) => {
113+
if (opts.stdoutInclude) {
114+
_.forEach(opts.stdoutInclude, (v) => {
111115
expect(stdout).include(v)
112116
console.log(`${chalk.bold('run matched stdout:')}\n${v}`)
113117
})

test/unit/index.spec.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ describe('webpack preprocessor', function () {
273273
})
274274
})
275275

276-
it('it rejects with joined errors when a stats err', function () {
277-
const errs = ['foo', 'bar', 'baz']
276+
it('it rejects with joined errors when a stats err and strips stacktrace', function () {
277+
const errs = ['foo\nat Object.foo', 'bar', 'baz']
278+
const errsNoStack = ['foo', 'bar', 'baz']
278279

279280
this.statsApi = {
280281
hasErrors () {
@@ -288,15 +289,7 @@ describe('webpack preprocessor', function () {
288289
this.compilerApi.run.yields(null, this.statsApi)
289290

290291
return this.run().catch((err) => {
291-
expect(err.stack).to.equal(errs.join('\n\n'))
292-
})
293-
})
294-
295-
it('backs up stack as originalStack', function () {
296-
this.compilerApi.run.yields(this.err)
297-
298-
return this.run().catch((err) => {
299-
expect(err.originalStack).to.equal(this.err.stack)
292+
expect(err.message).to.equal(`Webpack Compilation Error\n${errsNoStack.join('\n\n')}`)
300293
})
301294
})
302295
})

0 commit comments

Comments
 (0)