Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 4468194

Browse files
committed
suppress unhandled rejections from update bundles
if we don’t suppress unhandled rejections, it gets caught by process.on(‘unhandledRejection’) and crashes the app. it’s okay to suppress the rejection because the promise will eventually be handled by the app
1 parent 900347c commit 4468194

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,21 @@ const preprocessor = (options = {}) => {
146146
})
147147
}
148148

149-
// when we're notified of an update via watchify, signal for Cypres to
149+
// when we're notified of an update via watchify, signal for Cypress to
150150
// rerun the spec
151151
bundler.on('update', () => {
152152
log(`update ${filePath}`)
153153
// we overwrite the cached bundle promise, so on subsequent invocations
154154
// it gets the latest bundle
155-
bundles[filePath] = bundle().tap(() => {
155+
const bundlePromise = bundles[filePath] = bundle()
156+
.finally(() => {
156157
log(`- update finished for ${filePath}`)
157158
config.emit('rerun')
158159
})
160+
// we suppress unhandled rejections so they don't bubble up to the
161+
// unhandledRejection handler and crash the app. Cypress will eventually
162+
// take care of the rejection when the file is requested
163+
bundlePromise.suppressUnhandledRejections()
159164
})
160165

161166
const bundlePromise = fs.ensureDirAsync(path.dirname(outputPath)).then(bundle)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"secure": "nsp check",
3131
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
3232
"test": "mocha",
33+
"test-debug": "node --inspect --debug-brk ./node_modules/.bin/_mocha",
3334
"test-watch": "chokidar '*.js' 'test/*.js' -c 'npm test'"
3435
},
3536
"release": {

test/index_spec.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ describe('browserify preprocessor', function () {
211211
})
212212
})
213213

214-
it('closes bundler when shouldWatc is true and `close` is emitted', function () {
214+
it('closes bundler when shouldWatch is true and `close` is emitted', function () {
215215
this.config.shouldWatch = true
216216
return this.run().then(() => {
217217
this.config.on.withArgs('close').yield()
@@ -254,6 +254,42 @@ describe('browserify preprocessor', function () {
254254
expect(err.originalStack).to.equal(this.err.stack)
255255
})
256256
})
257+
258+
it('does not trigger unhandled rejection when bundle errors after update', function (done) {
259+
const handler = sandbox.spy()
260+
process.on('unhandledRejection', handler)
261+
this.createWriteStreamApi.on.withArgs('finish').onFirstCall().yields()
262+
263+
this.config.emit = () => {
264+
setTimeout(() => {
265+
expect(handler).not.to.be.called
266+
process.removeListener('unhandledRejection', handler)
267+
done()
268+
}, 500)
269+
}
270+
271+
this.run().then(() => {
272+
streamApi.on.withArgs('error').yieldsAsync(new Error('bundle error')).returns({ pipe () {} })
273+
this.bundlerApi.on.withArgs('update').yield()
274+
})
275+
})
276+
277+
it('rejects subsequent request after and update bundle errors', function () {
278+
this.createWriteStreamApi.on.withArgs('finish').onFirstCall().yields()
279+
const run = preprocessor(this.options)
280+
return run(this.config)
281+
.then(() => {
282+
streamApi.on.withArgs('error').yieldsAsync(new Error('bundle error')).returns({ pipe () {} })
283+
this.bundlerApi.on.withArgs('update').yield()
284+
return run(this.config)
285+
})
286+
.then(() => {
287+
throw new Error('should not resolve')
288+
})
289+
.catch((err) => {
290+
expect(err.message).to.contain('bundle error')
291+
})
292+
})
257293
})
258294
})
259295
})

0 commit comments

Comments
 (0)