-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathproxyquire.js
178 lines (148 loc) · 5.78 KB
/
proxyquire.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* jshint asi:true */
'use strict'
var assert = require('assert')
var proxyquire = require('..')
var stats = require('./samples/stats')
describe('Given foo requires the bar and path modules and bar.bar() returns "bar"', function () {
var file = '/folder/test.ext'
var foo
var foober
var barber = { bar: function () { return 'barber' } }
describe('When I resolve foo with no overrides to bar as foo and resolve foo with barber stub as foober.', function () {
before(function () {
stats.reset()
foo = proxyquire('./samples/foo', { './bar': { /* no overrides */ } })
foober = proxyquire('./samples/foo', { './bar': barber })
})
it('foo is required 2 times', function () {
assert.strictEqual(stats.fooRequires(), 2)
})
describe('foo\'s bar is unchanged', function () {
it('foo.bigBar() == "BAR"', function () {
assert.strictEqual(foo.bigBar(), 'BAR')
})
})
describe('only stubbed modules have overrides in foober', function () {
it('foober.bigBar() == "BARBER"', function () {
assert.strictEqual(foober.bigBar(), 'BARBER')
})
it('foober.bigExt("/folder/test.ext") == ".EXT"', function () {
assert.strictEqual(foober.bigExt(file), '.EXT')
})
it('foober.bigBas("/folder/test.ext") == "TEST.EXT"', function () {
assert.strictEqual(foober.bigBas(file), 'TEST.EXT')
})
})
describe('when I override keys of stubs after resolve', function () {
before(function () {
barber.bar = function () { return 'friseur' }
barber.rab = function () { return 'rabarber' }
})
it('overrides behavior when module is required inside function call', function () {
assert.strictEqual(foober.bigBar(), 'FRISEUR')
})
it('overrides behavior when module is required on top of file', function () {
assert.strictEqual(foober.bigRab(), 'RABARBER')
})
describe('and then delete overrides of stubs after resolve', function () {
beforeEach(function () {
delete barber.bar
delete barber.rab
})
it('reverts to original behavior when module is required inside function call', function () {
assert.strictEqual(foober.bigBar(), 'BAR')
})
it('doesn\'t properly revert to original behavior when module is required on top of file ', function () {
assert.throws(foober.bigRab)
})
})
})
})
describe('When foo.bigExt() returns capitalized path.extname and foo.bigBas() returns capitalized path.basename', function () {
describe('and path.extname(file) is stubbed to return "override " + file', function () {
describe('and callThru was not changed globally or for path module', function () {
before(function () {
foo = proxyquire('./samples/foo', {
path: {
extname: function (file) { return 'override ' + file }
}
})
})
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () {
assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT')
})
it('foo.bigBas(file) == "TEST.EXT"', function () {
assert.strictEqual(foo.bigBas(file), 'TEST.EXT')
})
})
describe('and callThru is turned off for path module', function () {
before(function () {
foo = proxyquire('./samples/foo', {
path: {
extname: function (file) { return 'override ' + file }, '@noCallThru': true
}
})
})
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () {
assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT')
})
it('foo.bigBas(file) throws', function () {
assert.throws(foo.bigBas)
})
})
describe('and callThru was turned off globally', function () {
var $proxyquire
before(function () {
$proxyquire = proxyquire.noCallThru()
})
describe('and not changed for path module', function () {
before(function () {
foo = $proxyquire('./samples/foo', {
path: {
extname: function (file) { return 'override ' + file }
}
})
})
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () {
assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT')
})
it('foo.bigBas(file) throws', function () {
assert.throws(foo.bigBas)
})
})
describe('and turned back on for path module', function () {
before(function () {
foo = $proxyquire('./samples/foo', {
path: {
extname: function (file) { return 'override ' + file }, '@noCallThru': false
}
})
})
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () {
assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT')
})
it('foo.bigBas(file) == "TEST.EXT"', function () {
assert.strictEqual(foo.bigBas(file), 'TEST.EXT')
})
})
describe('and turned back on globally', function () {
before(function () {
foo = $proxyquire
.callThru()
.load('./samples/foo', {
path: {
extname: function (file) { return 'override ' + file }
}
})
})
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () {
assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT')
})
it('foo.bigBas(file) == "TEST.EXT"', function () {
assert.strictEqual(foo.bigBas(file), 'TEST.EXT')
})
})
})
})
})
})