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

fix: Suppress unhandled rejections #59

Merged
merged 5 commits into from
Nov 13, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.DS_Store
*.log*
_test-output
4 changes: 4 additions & 0 deletions __snapshots__/e2e_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const debug = require('debug')('cypress:webpack')
const createDeferred = require('./deferred')
const stubbableRequire = require('./stubbable-require')

const bundles = {}
let bundles = {}

// we don't automatically load the rules, so that the babel dependencies are
// not required if a user passes in their own configuration
Expand Down Expand Up @@ -148,12 +148,20 @@ const preprocessor = (options = {}) => {
// we overwrite the latest bundle, so that a new call to this function
// returns a promise that resolves when the bundling is finished
latestBundle = createDeferred()
bundles[filePath] = latestBundle.promise.tap(() => {
bundles[filePath] = latestBundle.promise

bundles[filePath].tap(() => {
debug('- compile finished for', filePath)
// when the bundling is finished, emit 'rerun' to let Cypress
// know to rerun the spec
file.emit('rerun')
})
// we suppress unhandled rejections so they don't bubble up to the
// unhandledRejection handler and crash the process. Cypress will
// eventually take care of the rejection when the file is requested.
// note that this does not work if attached to latestBundle.promise
// for some reason. it only works when attached after .tap ¯\_(ツ)_/¯
.suppressUnhandledRejections()
}

// when we should watch, we hook into the 'compile' hook so we know when
Expand Down Expand Up @@ -200,4 +208,9 @@ Object.defineProperty(preprocessor, 'defaultOptions', {
},
})

// for testing purposes
preprocessor.__reset = () => {
bundles = {}
}

module.exports = preprocessor
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
"pretest": "npm run lint",
"secure": "nsp check",
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
"test": "mocha",
"test-watch": "chokidar '*.js' 'test/*.js' -c 'mocha'",
"test": "npm run test-unit && npm run test-e2e",
"test-e2e": "mocha test/e2e/*.js",
"test-unit": "mocha test/unit/*.js",
"test-debug": "node --inspect --debug-brk ./node_modules/.bin/_mocha",
"test-watch": "chokidar '*.js' 'test/unit/*.js' -c 'npm run test-unit'",
"semantic-release": "semantic-release pre && npm publish --access public && semantic-release post"
},
"devDependencies": {
Expand All @@ -46,6 +49,7 @@
"eslint": "4.6.1",
"eslint-plugin-cypress-dev": "1.1.1",
"eslint-plugin-mocha": "4.11.0",
"fs-extra": "8.1.0",
"github-post-release": "1.13.1",
"license-checker": "13.0.3",
"mocha": "3.5.0",
Expand All @@ -56,6 +60,7 @@
"simple-commit-message": "3.3.1",
"sinon": "3.2.1",
"sinon-chai": "2.13.0",
"snap-shot-it": "7.9.0",
"webpack": "^4.18.1"
},
"peerDependencies": {
Expand All @@ -67,8 +72,8 @@
"babel-loader": "^8.0.2"
},
"dependencies": {
"bluebird": "3.5.0",
"debug": "3.1.0"
"bluebird": "3.7.1",
"debug": "4.1.1"
},
"release": {
"verifyConditions": "condition-circle",
Expand Down
59 changes: 59 additions & 0 deletions test/e2e/e2e_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const EventEmitter = require('events').EventEmitter
const expect = require('chai').expect
const fs = require('fs-extra')
const path = require('path')
const snapshot = require('snap-shot-it')

const preprocessor = require('../../index')

describe('webpack preprocessor - e2e', () => {
const outputPath = path.join(__dirname, '..', '_test-output', 'output.js')

let file
let filePath

beforeEach(() => {
const originalFilePath = path.join(__dirname, '..', 'fixtures', 'example_spec.js')

filePath = path.join(__dirname, '..', '_test-output', 'example_spec.js')

preprocessor.__reset()
fs.removeSync(path.join(__dirname, '_test-output'))
fs.outputFileSync(filePath, '')
fs.copyFileSync(originalFilePath, filePath)

file = Object.assign(new EventEmitter(), {
filePath,
outputPath,
})
})

it('correctly preprocesses the file', () => {
return preprocessor()(file).then(() => {
snapshot(fs.readFileSync(outputPath).toString())
})
})

it('allows attaching catch later on syntax error without triggering unhandled rejection', (done) => {
process.on('unhandledRejection', (err) => {
// eslint-disable-next-line no-console
console.error('Unhandled Rejection:', err.stack)
done('Should not have trigger unhandled rejection')
})

file.shouldWatch = true

preprocessor()(file).then(() => {
fs.outputFileSync(filePath, '{')

setTimeout(() => {
preprocessor()(file)
.catch((err) => {
expect(err.stack).to.include('Unexpected token')
file.emit('close')
done()
})
}, 1000)
})
})
})
6 changes: 6 additions & 0 deletions test/fixtures/example_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
it('is a test', () => {
const [a, b] = [1, 2]
expect(a).to.equal(1)
expect(b).to.equal(2)
expect(Math.min(...[3, 4])).to.equal(3)
})
4 changes: 2 additions & 2 deletions test/index_spec.js → test/unit/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ mockery.enable({
})
mockery.registerMock('webpack', webpack)

const preprocessor = require('../index')
const stubbableRequire = require('../stubbable-require')
const preprocessor = require('../../index')
const stubbableRequire = require('../../stubbable-require')

describe('webpack preprocessor', function () {
beforeEach(function () {
Expand Down