-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
spec_helper.js
128 lines (97 loc) · 2.9 KB
/
spec_helper.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const root = '@packages/server'
const chai = require('chai')
chai.use(require('chai-subset'))
global.root = root
global.supertest = require('supertest')
global.nock = require('nock')
global.expect = chai.expect
global.mockery = require('mockery')
global.proxyquire = require('proxyquire')
global.sinon = require('sinon')
const _ = require('lodash')
const Promise = require('bluebird')
const path = require('path')
const cache = require(`@packages/server/lib/cache`)
require('chai')
.use(require('@cypress/sinon-chai'))
.use(require('chai-uuid'))
.use(require('chai-as-promised'))
if (process.env.UPDATE) {
throw new Error('You\'re using UPDATE=1 which is the old way of updating snapshots.\n\nThe correct environment variable is SNAPSHOT_UPDATE=1')
}
if (process.env.UPDATE_SNAPSHOT) {
throw new Error('You\'re using UPDATE_SNAPSHOT=1\n\nThe correct environment variable is SNAPSHOT_UPDATE=1')
}
if (process.env.UPDATE_SNAPSHOTS) {
throw new Error('You\'re using UPDATE_SNAPSHOTS=1\n\nThe correct environment variable is SNAPSHOT_UPDATE=1')
}
let hasOnly = false;
// hack for older version of mocha so that
// snap-shot-it can find suite._onlyTests
['it', 'describe', 'context'].forEach((prop) => {
const backup = global[prop].only
global[prop].only = function (...args) {
hasOnly = true
return backup.apply(this, args)
}
})
const originalEnv = process.env
const env = _.clone(process.env)
sinon.usingPromise(Promise)
// backup these originals
const {
restore,
useFakeTimers,
} = sinon
sinon.useFakeTimers = function (...args) {
sinon._clock = useFakeTimers.apply(sinon, args)
}
sinon.restore = function (...args) {
let c
c = sinon._clock
if (c) {
c.restore()
}
return restore.apply(sinon, args)
}
mockery.enable({
warnOnUnregistered: false,
})
// stub out the entire electron object for our stub
// we must use an absolute path here because of the way mockery internally loads this
// module - meaning the first time electron is required it'll use this path string
// so because its required from a separate module we must use an absolute reference to it
mockery.registerSubstitute(
'electron',
path.join(__dirname, './support/helpers/electron_stub'),
)
// stub out electron's original-fs module which is available when running in electron
mockery.registerMock('original-fs', {})
before(function () {
if (hasOnly) {
this.test.parent._onlyTests = [true]
}
})
// appData.ensure()
beforeEach(function () {
this.originalEnv = originalEnv
nock.disableNetConnect()
nock.enableNetConnect(/localhost/)
nock.enableNetConnect(/api.honeycomb.io/)
// always clean up the cache
// before each test
return cache.remove()
})
afterEach(() => {
sinon.restore()
nock.cleanAll()
nock.enableNetConnect()
process.env = _.clone(env)
})
module.exports = {
expect: global.expect,
nock: global.nock,
proxyquire: global.proxyquire,
sinon: global.sinon,
root: global.root,
}