-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
spec.js
72 lines (60 loc) · 2.36 KB
/
spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const debug = require('debug')('cypress:server:controllers:spec')
const Promise = require('bluebird')
const errors = require('../errors')
const preprocessor = require('../plugins/preprocessor')
const ignoreECONNABORTED = () => {
// https://github.com/cypress-io/cypress/issues/1877
// now that we are properly catching errors from
// res.sendFile, sendFile will reject if the browser aborts
// its internal requests (as it shuts down) with
// ECONNABORTED. This happens because if a previous spec
// file is unable to be transpiled correctly, we immediately
// shut down the run, which closes the browser, triggering
// the browser to abort the request which would end up here
// and display two different errors.
}
const ignoreEPIPE = () => {
// 'write EPIPE' errors can occur if a spec is served and then rerun
// quickly because it's trying to send the spec file to a socket that
// has already been closed. this can be ignored because it means
// another version of the file has already been sent and will
// be loaded by the browser instead
}
module.exports = {
handle (spec, req, res, config, next, onError) {
debug('request for %o', { spec })
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
})
res.type('js')
return preprocessor
.getFile(spec, config)
.then((filePath) => {
debug('sending spec %o', { filePath })
const sendFile = Promise.promisify(res.sendFile.bind(res))
return sendFile(filePath)
})
.catch({ code: 'ECONNABORTED' }, ignoreECONNABORTED)
.catch({ code: 'EPIPE' }, ignoreEPIPE)
.catch((err) => {
debug(`preprocessor error for spec '%s': %s`, spec, err.stack)
if (!config.isTextTerminal) {
return res.send(preprocessor.clientSideError(err))
}
// bluebird made a change in 3.4.7 where they handle
// SyntaxErrors differently here
// https://github.com/petkaantonov/bluebird/pull/1295
//
// their new behavior messes up how we show these errors
// so we must backup the original stack and replace it here
if (err.originalStack) {
err.stack = err.originalStack
}
const filePath = err.filePath != null ? err.filePath : spec
err = errors.get('BUNDLE_ERROR', filePath, preprocessor.errorMessage(err))
onError(err)
})
},
}