UNPKG

8.14 kBtext/coffeescriptView Raw
1ipso = require '../lib/ipso'
2should = require 'should'
3{deferred, util} = require 'also'
4Loader = require '../lib/loader'
5does = require 'does'
6
7
8
9describe 'ipso', ->
10
11 before ->
12
13 resolver = => @resolve
14
15 @fakeit = (title, fn) =>
16 @test = timer: _onTimeout: ->
17 fn.call @, resolver()
18
19
20 @resolvingPromise = deferred (action) ->
21 action.resolve 'RESULT'
22
23 beforeEach -> @RESULT = undefined
24
25
26 it 'works normally', ->
27
28 #console.log 'in works normally'.bold
29 true.should.equal true
30
31 # it 'still works normally', ipso ->
32
33 # console.log 'in still works normally'.bold
34 # true.should.equal false
35
36
37 context 'it warns on injecting done into context', ipso (done) ->
38
39
40 context 'for mocha tests', ->
41
42
43 it 'goes in front of a test function like this:', ipso ->
44
45 # console.log @resolvingPromise
46
47
48 context 'injection for synchronous tests', ->
49
50 it 'returns a function with done as the only argument if no "done"', (done) ->
51
52 #
53 # injection is asynchronous even when mocha test is not
54 #
55
56 fn = ipso (zlib, net) ->
57 args = util.argsOf fn
58 args.should.eql ['done']
59 done()
60
61 context 'injection for asynchronous tests', ->
62
63 it 'returns a function with done as the only argument if "done"', (done) ->
64
65 fn = ipso (done, zlib, net) ->
66 args = util.argsOf fn
67 args.should.eql ['done']
68 done()
69
70
71 context 'it can inject into before', ->
72
73 before ipso (http) -> @http = http
74
75 it 'did inject http', ipso (done) ->
76
77 @http = require 'http'
78 done()
79
80
81 it 'calls each test "runtime" into does as test starts up', ipso (done) ->
82
83 spec = does._test().runtime.current.spec
84
85 spec.title.should.equal 'calls each test "runtime" into does as test starts up'
86 done()
87
88
89 it 'still fails as it should', ->
90
91 @resolve = (error) ->
92 #console.log error
93 error.name.should.equal 'AssertionError'
94 done()
95
96 @fakeit 'fails this', ipso (done) ->
97 true.should.equal 'this is expected to fail'
98
99
100
101 it 'preserves the mocha context', ipso (done) ->
102
103 should.exist @resolvingPromise
104 done()
105
106
107 it 'initializes loader with starting cwd', ipso (done) ->
108
109 Loader._test().dir.should.equal process.cwd()
110 done()
111
112
113 it 'can assign module list into loader', (done) ->
114
115 ipso.modules
116 tag1: require: '../lib/require/argument'
117 tag2: require: 'module-name'
118
119 Loader._test().modules.should.eql
120 tag1: require: '../lib/require/argument'
121 tag2: require: 'module-name'
122
123 done()
124
125
126 it 'throws error if modules is loaded into tag', (done) ->
127
128 try ipso.modules
129 tag2: require 'http'
130
131 catch error
132 error.message.should.equal 'ipso.module expects { tagName: { require: "path/or/name" } }'
133 done()
134
135 it 'throws error if require is not a subkey', (done) ->
136
137 try ipso.modules
138 tag2: erquire:'http'
139
140 catch error
141 error.message.should.equal 'ipso.module expects { tagName: { require: "path/or/name" } }'
142 done()
143
144 it 'ipso.modules returns ipso', (done) ->
145
146 ipso.modules
147 tag: require: 'moo'
148
149 .should.equal ipso
150 done()
151
152
153 it 'passes from within the promise resolution / fullfillment handler', ipso (done) ->
154
155 @resolvingPromise().then (result) ->
156
157 result.should.equal 'RESULT'
158 done()
159
160
161 it 'fails from within the promise resolution / fullfillment handler', ->
162
163 @resolve = (error) ->
164 error.name.should.equal 'AssertionError'
165 done()
166
167 @fakeit 'fails this', ipso (done) =>
168
169
170 it 'injects mode nodules', ipso (done, mocha) ->
171
172 mocha.should.equal require 'mocha'
173 done()
174
175
176 describe 'it can inject into describe', ipso (vm) ->
177
178 context 'it can inject into context', ipso (net) ->
179
180 it 'confirms', ->
181
182 vm.should.equal require 'vm'
183 net.should.equal require 'net'
184
185
186 it 'can inject into synchronous test', ipso (zlib, net) ->
187
188 net.should.equal require 'net'
189 zlib.should.equal require 'zlib'
190
191
192 it 'fails when injecting undefined node module', (done) ->
193
194 @resolve = (error) ->
195 error.should.match /Cannot find module/
196 done()
197
198 @fakeit 'fails this', ipso (facto, i) ->
199
200
201 it 'succeeds when injecting undefined LocalModule', ipso (LocalModule) ->
202
203 LocalModule.$ipso.PENDING.should.equal true
204
205
206
207 it 'injects modules as spectatable', ipso (facto, should) ->
208
209 should.does.should.be.an.instanceof Function
210 facto()
211
212
213 it 'defines tag() for hook prepping spectatable objects the do not reset stubs at injection', (done) ->
214
215 ipso.tag.should.be.an.instanceof Function
216 done()
217
218 context 'tag()', ->
219
220 it 'registers spectated object as tagged', (done) ->
221
222 object = this: 1
223
224 ipso.tag( tagName: object ).then ->
225
226 expx = does._test().spectacles
227 lastone = expx[uuid] for uuid of expx
228 lastone.name.should.equal 'tagName'
229 lastone.tagged.should.equal true
230 object.does.should.be.an.instanceof Function
231 lastone.object.should.equal object
232 done()
233
234
235 it 'can tag more than one at a time', ipso (facto) ->
236
237 ipso.tag
238
239 satelite: class Satelite
240 planet: class Planet
241 star: class Star
242 π: class π
243
244 .then ->
245
246 tagged = does._test().tagged
247
248 should.exist tagged['satelite']
249 should.exist tagged['planet']
250 should.exist tagged['star']
251 should.exist tagged['π']
252 facto()
253
254 context 'mock', ->
255
256 it 'returns a mock object', ->
257
258 mock = ipso.mock 'thing'
259 mock.is.should.be.an.instanceof Function
260 mock.does.should.be.an.instanceof Function
261
262 it 'can assert as self', ->
263
264 mock1 = ipso.mock 'something'
265 mock2 = ipso.mock 'something else'
266
267 try mock1.is mock2
268 catch error
269 error.should.be.an.instanceof require('assert').AssertionError
270 error.should.match /expected/
271
272 try mock1.is 'something else'
273 catch error
274 error.actual.should.equal 'something'
275 error.expected.should.equal 'something else'
276
277
278 # it 'throws expected functions not called instead of timeout even if timeout is reset', ipso (facto, Something) ->
279
280 # @timeout 10
281 # Something.does
282 # something: -> 'okgood'
283 # somethingElse: -> facto()
284
285 # Something.something()
286 # # Something.somethingElse()
287
288 # # 1 | {
289 # # 2 | "Something": {
290 # # 3 | "functions": {
291 # # 4 | "Object.something()": "was called",
292 # # 5 | "Object.somethingElse()": "was NOT called"
293 # # 6 | }
294 # # 7 | }
295 # # 8 | }
296
297 # #
298 # # not bothering to try and make a 'passing test' capable
299 # # of testing that this test fails by timeout
300 # #
301
302
303
304