~caneypuggies/reformedchurcheslocator/couchapp-backbone

« back to all changes in this revision

Viewing changes to _attachments/js/vendor/jasmine/spec/core/SpecRunningSpec.js

  • Committer: Tim Black
  • Date: 2013-09-16 22:50:16 UTC
  • Revision ID: tim@alwaysreformed.com-20130916225016-zk8jiba25z33ew7h
Versioned Bower vendor directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
describe("jasmine spec running", function () {
 
2
  var env;
 
3
  var fakeTimer;
 
4
 
 
5
  beforeEach(function() {
 
6
    env = new jasmine.Env();
 
7
    env.updateInterval = 0;
 
8
 
 
9
    fakeTimer = new jasmine.FakeTimer();
 
10
    env.setTimeout = fakeTimer.setTimeout;
 
11
    env.clearTimeout = fakeTimer.clearTimeout;
 
12
    env.setInterval = fakeTimer.setInterval;
 
13
    env.clearInterval = fakeTimer.clearInterval;
 
14
  });
 
15
 
 
16
  it('should assign spec ids sequentially', function() {
 
17
    var it0, it1, it2, it3, it4;
 
18
    env.describe('test suite', function() {
 
19
      it0 = env.it('spec 0', function() {
 
20
      });
 
21
      it1 = env.it('spec 1', function() {
 
22
      });
 
23
      it2 = env.xit('spec 2', function() {
 
24
      });
 
25
      it3 = env.it('spec 3', function() {
 
26
      });
 
27
    });
 
28
    env.describe('test suite 2', function() {
 
29
      it4 = env.it('spec 4', function() {
 
30
      });
 
31
    });
 
32
 
 
33
    expect(it0.id).toEqual(0);
 
34
    expect(it1.id).toEqual(1);
 
35
    expect(it2.id).toEqual(2);
 
36
    expect(it3.id).toEqual(3);
 
37
    expect(it4.id).toEqual(4);
 
38
  });
 
39
 
 
40
  it("should build up some objects with results we can inspect", function() {
 
41
 
 
42
    var specWithNoBody, specWithExpectation, specWithFailingExpectations, specWithMultipleExpectations;
 
43
 
 
44
    var suite = env.describe('default current suite', function() {
 
45
      specWithNoBody = env.it('new spec');
 
46
 
 
47
      specWithExpectation = env.it('spec with an expectation').runs(function () {
 
48
        var foo = 'bar';
 
49
        this.expect(foo).toEqual('bar');
 
50
      });
 
51
 
 
52
      specWithFailingExpectations = env.it('spec with failing expectation').runs(function () {
 
53
        var foo = 'bar';
 
54
        this.expect(foo).toEqual('baz');
 
55
      });
 
56
 
 
57
      specWithMultipleExpectations = env.it('spec with multiple expectations').runs(function () {
 
58
        var foo = 'bar';
 
59
        var baz = 'quux';
 
60
 
 
61
        this.expect(foo).toEqual('bar');
 
62
        this.expect(baz).toEqual('quux');
 
63
      });
 
64
    });
 
65
 
 
66
    suite.execute();
 
67
 
 
68
    expect(specWithNoBody.description).toEqual('new spec');
 
69
 
 
70
    expect(specWithExpectation.results().getItems().length).toEqual(1); // "Results aren't there after a spec was executed"
 
71
    expect(specWithExpectation.results().getItems()[0].passed()).toEqual(true); // "Results has a result, but it's true"
 
72
    expect(specWithExpectation.results().description).toEqual('spec with an expectation'); // "Spec's results did not get the spec's description"
 
73
 
 
74
    expect(specWithFailingExpectations.results().getItems()[0].passed()).toEqual(false); // "Expectation that failed, passed"
 
75
 
 
76
    expect(specWithMultipleExpectations.results().getItems().length).toEqual(2); // "Spec doesn't support multiple expectations"
 
77
  });
 
78
 
 
79
  it("should work without a runs block", function() {
 
80
    var another_spec;
 
81
    env.describe('default current suite', function() {
 
82
      another_spec = env.it('spec with an expectation', function () {
 
83
        var foo = 'bar';
 
84
        this.expect(foo).toEqual('bar');
 
85
        this.expect(foo).toEqual('baz');
 
86
      });
 
87
    });
 
88
 
 
89
    another_spec.execute();
 
90
    another_spec.done = true;
 
91
 
 
92
    expect(another_spec.results().getItems().length).toEqual(2);
 
93
    expect(another_spec.results().getItems()[0].passed()).toEqual(true); // "In a spec without a run block, expected first expectation result to be true but was false"
 
94
    expect(another_spec.results().getItems()[1].passed()).toEqual(false); // "In a spec without a run block, expected second expectation result to be false but was true";
 
95
    expect(another_spec.results().description).toEqual('spec with an expectation'); // "In a spec without a run block, results did not include the spec's description";
 
96
  });
 
97
 
 
98
  it('should queue waits and runs that it encounters while executing specs', function() {
 
99
    var specWithRunsAndWaits;
 
100
    var foo = 0;
 
101
    env.describe('test async spec', function() {
 
102
      specWithRunsAndWaits = env.it('spec w/ queued statments', function () {
 
103
        this.runs(function () {
 
104
          foo++;
 
105
        });
 
106
        this.waits(500);
 
107
        this.runs(function () {
 
108
          foo++;
 
109
        });
 
110
        this.waits(500);
 
111
        this.runs(function () {
 
112
          foo++;
 
113
        });
 
114
      });
 
115
    });
 
116
 
 
117
    expect(foo).toEqual(0);
 
118
    specWithRunsAndWaits.execute();
 
119
 
 
120
    expect(foo).toEqual(1);
 
121
    fakeTimer.tick(500);
 
122
    expect(foo).toEqual(2);
 
123
    fakeTimer.tick(500);
 
124
    expect(foo).toEqual(3);
 
125
  });
 
126
 
 
127
  it("should run asynchronous tests", function () {
 
128
    var foo = 0;
 
129
 
 
130
    var a_spec;
 
131
    env.describe('test async spec', function() {
 
132
      a_spec = env.it('spec w/ queued statments', function () {
 
133
        this.runs(function () {
 
134
          foo++;
 
135
        });
 
136
        this.runs(function () {
 
137
          this.expect(foo).toEqual(1);
 
138
        });
 
139
      });
 
140
    });
 
141
 
 
142
    a_spec.execute();
 
143
 
 
144
    expect(a_spec.results().getItems().length).toEqual(1); // 'No call to waits(): Spec queue did not run all functions';
 
145
    expect(a_spec.results().getItems()[0].passed()).toEqual(true); // 'No call to waits(): Queued expectation failed';
 
146
 
 
147
    foo = 0;
 
148
    env.describe('test async spec', function() {
 
149
      a_spec = env.it('spec w/ queued statments', function () {
 
150
        this.runs(function () {
 
151
          fakeTimer.setTimeout(function() {
 
152
            foo++;
 
153
          }, 500);
 
154
        });
 
155
        this.waits(1000);
 
156
        this.runs(function() {
 
157
          this.expect(foo).toEqual(1);
 
158
        });
 
159
      });
 
160
    });
 
161
 
 
162
    a_spec.execute();
 
163
 
 
164
    expect(a_spec.results().getItems().length).toEqual(0);
 
165
 
 
166
    fakeTimer.tick(500);
 
167
    expect(a_spec.results().getItems().length).toEqual(0);
 
168
 
 
169
    fakeTimer.tick(500);
 
170
    expect(a_spec.results().getItems().length).toEqual(1); // 'Calling waits(): Spec queue did not run all functions';
 
171
 
 
172
    expect(a_spec.results().getItems()[0].passed()).toEqual(true); // 'Calling waits(): Queued expectation failed';
 
173
 
 
174
    var bar = 0;
 
175
    var another_spec;
 
176
    env.describe('test async spec', function() {
 
177
      another_spec = env.it('spec w/ queued statments', function () {
 
178
        this.runs(function () {
 
179
          fakeTimer.setTimeout(function() {
 
180
            bar++;
 
181
          }, 250);
 
182
 
 
183
        });
 
184
        this.waits(500);
 
185
        this.runs(function () {
 
186
          fakeTimer.setTimeout(function() {
 
187
            bar++;
 
188
          }, 250);
 
189
        });
 
190
        this.waits(500);
 
191
        this.runs(function () {
 
192
          this.expect(bar).toEqual(2);
 
193
        });
 
194
      });
 
195
    });
 
196
 
 
197
 
 
198
    another_spec.execute();
 
199
 
 
200
    fakeTimer.tick(1000);
 
201
 
 
202
    expect(another_spec.results().getItems().length).toEqual(1);
 
203
    expect(another_spec.results().getItems()[0].passed()).toEqual(true);
 
204
 
 
205
    var baz = 0;
 
206
    var yet_another_spec;
 
207
    env.describe('test async spec', function() {
 
208
      yet_another_spec = env.it('spec w/ async fail', function () {
 
209
        this.runs(function () {
 
210
          fakeTimer.setTimeout(function() {
 
211
            baz++;
 
212
          }, 250);
 
213
        });
 
214
        this.waits(100);
 
215
        this.runs(function() {
 
216
          this.expect(baz).toEqual(1);
 
217
        });
 
218
      });
 
219
    });
 
220
 
 
221
 
 
222
    yet_another_spec.execute();
 
223
    //tick twice so that second runs gets eval'd first: mockClock bug?
 
224
    fakeTimer.tick(100);
 
225
    fakeTimer.tick(150);
 
226
 
 
227
 
 
228
    expect(yet_another_spec.results().getItems().length).toEqual(1);
 
229
    expect(yet_another_spec.results().getItems()[0].passed()).toEqual(false);
 
230
  });
 
231
 
 
232
  it("testAsyncSpecsWithMockSuite", function () {
 
233
    var bar = 0;
 
234
    var another_spec;
 
235
    env.describe('test async spec', function() {
 
236
      another_spec = env.it('spec w/ queued statments', function () {
 
237
        this.runs(function () {
 
238
          fakeTimer.setTimeout(function() {
 
239
            bar++;
 
240
          }, 250);
 
241
        });
 
242
        this.waits(500);
 
243
        this.runs(function () {
 
244
          fakeTimer.setTimeout(function() {
 
245
            bar++;
 
246
          }, 250);
 
247
        });
 
248
        this.waits(1500);
 
249
        this.runs(function() {
 
250
          this.expect(bar).toEqual(2);
 
251
        });
 
252
      });
 
253
    });
 
254
 
 
255
    another_spec.execute();
 
256
    fakeTimer.tick(2000);
 
257
    expect(another_spec.results().getItems().length).toEqual(1);
 
258
    expect(another_spec.results().getItems()[0].passed()).toEqual(true);
 
259
  });
 
260
 
 
261
  describe("waitsFor", function() {
 
262
    var latchFunction = function() {
 
263
      return true;
 
264
    };
 
265
    var spec;
 
266
 
 
267
    function makeWaitsForSpec() {
 
268
      var args = jasmine.util.argsToArray(arguments);
 
269
      env.describe('suite', function() {
 
270
        spec = env.it('spec', function() {
 
271
          this.waitsFor.apply(this, args);
 
272
        });
 
273
      });
 
274
      env.execute();
 
275
    }
 
276
 
 
277
    it("should accept args (latchFunction, timeoutMessage, timeout)", function() {
 
278
      makeWaitsForSpec(latchFunction, "message", 123);
 
279
      var block = spec.queue.blocks[1];
 
280
      expect(block.latchFunction).toBe(latchFunction);
 
281
      expect(block.timeout).toEqual(123);
 
282
      expect(block.message).toEqual('message');
 
283
    });
 
284
 
 
285
    it("should accept args (latchFunction, timeout)", function() {
 
286
      makeWaitsForSpec(latchFunction, 123);
 
287
      var block = spec.queue.blocks[1];
 
288
      expect(block.latchFunction).toBe(latchFunction);
 
289
      expect(block.timeout).toEqual(123);
 
290
      expect(block.message).toEqual(null);
 
291
    });
 
292
 
 
293
    it("should accept args (latchFunction, timeoutMessage)", function() {
 
294
      env.defaultTimeoutInterval = 4321;
 
295
      makeWaitsForSpec(latchFunction, "message");
 
296
      var block = spec.queue.blocks[1];
 
297
      expect(block.latchFunction).toBe(latchFunction);
 
298
      expect(block.timeout).toEqual(4321);
 
299
      expect(block.message).toEqual('message');
 
300
    });
 
301
 
 
302
    it("should accept args (latchFunction)", function() {
 
303
      env.defaultTimeoutInterval = 4321;
 
304
      makeWaitsForSpec(latchFunction);
 
305
      var block = spec.queue.blocks[1];
 
306
      expect(block.latchFunction).toBe(latchFunction);
 
307
      expect(block.timeout).toEqual(4321);
 
308
      expect(block.message).toEqual(null);
 
309
    });
 
310
 
 
311
    it("should accept deprecated args order (timeout, latchFunction, timeoutMessage)", function() {
 
312
      makeWaitsForSpec(123, latchFunction, "message");
 
313
      var block = spec.queue.blocks[1];
 
314
      expect(block.latchFunction).toBe(latchFunction);
 
315
      expect(block.timeout).toEqual(123);
 
316
      expect(block.message).toEqual('message');
 
317
    });
 
318
 
 
319
    it("testWaitsFor", function() {
 
320
      var doneWaiting = false;
 
321
      var runsBlockExecuted = false;
 
322
 
 
323
      var spec;
 
324
      env.describe('foo', function() {
 
325
        spec = env.it('has a waits for', function() {
 
326
          this.runs(function() {
 
327
          });
 
328
 
 
329
          this.waitsFor(500, function() {
 
330
            return doneWaiting;
 
331
          });
 
332
 
 
333
          this.runs(function() {
 
334
            runsBlockExecuted = true;
 
335
          });
 
336
        });
 
337
      });
 
338
 
 
339
      spec.execute();
 
340
      expect(runsBlockExecuted).toEqual(false); //, 'should not have executed runs block yet');
 
341
      fakeTimer.tick(100);
 
342
      doneWaiting = true;
 
343
      fakeTimer.tick(100);
 
344
      expect(runsBlockExecuted).toEqual(true); //, 'should have executed runs block');
 
345
    });
 
346
 
 
347
    it("fails with message", function() {
 
348
      var spec;
 
349
      env.describe('foo', function() {
 
350
        spec = env.it('has a waits for', function() {
 
351
          this.runs(function() {
 
352
          });
 
353
 
 
354
          this.waitsFor(500, function() {
 
355
            return false; // force a timeout
 
356
          }, 'my awesome condition');
 
357
 
 
358
          this.runs(function() {
 
359
          });
 
360
        });
 
361
      });
 
362
 
 
363
      spec.execute();
 
364
      fakeTimer.tick(1000);
 
365
      expect(spec.results().getItems()[0].message).toEqual('timeout: timed out after 500 msec waiting for my awesome condition');
 
366
    });
 
367
 
 
368
    it("fails and skips the rest of the spec if timeout is reached and the latch function hasn't returned true", function() {
 
369
      var runsBlockExecuted = false;
 
370
      var subsequentSpecRan = false;
 
371
 
 
372
      var timeoutSpec, subsequentSpec;
 
373
      var suite = env.describe('foo', function() {
 
374
        timeoutSpec = env.it('has a waits for', function() {
 
375
          this.runs(function() {
 
376
          });
 
377
 
 
378
          this.waitsFor(500, function() {
 
379
            return false;
 
380
          });
 
381
 
 
382
          this.runs(function() {
 
383
            runsBlockExecuted = true;
 
384
          });
 
385
        });
 
386
 
 
387
        subsequentSpec = env.it('then carries on to the next test', function() {
 
388
          subsequentSpecRan = true;
 
389
        });
 
390
      });
 
391
 
 
392
      env.execute();
 
393
      expect(runsBlockExecuted).toEqual(false);
 
394
      fakeTimer.tick(100);
 
395
      expect(runsBlockExecuted).toEqual(false);
 
396
      fakeTimer.tick(400);
 
397
      expect(runsBlockExecuted).toEqual(false);
 
398
      expect(timeoutSpec.results().getItems()[0].message).toEqual('timeout: timed out after 500 msec waiting for something to happen');
 
399
      expect(subsequentSpecRan).toEqual(true);
 
400
    });
 
401
 
 
402
    it("runs afterEach after timing out", function() {
 
403
      var afterEach = jasmine.createSpy('afterEach');
 
404
 
 
405
      env.describe('foo', function () {
 
406
        env.afterEach(afterEach);
 
407
 
 
408
        env.it('waitsFor', function () {
 
409
          this.waitsFor(100, function() {
 
410
            return false;
 
411
          });
 
412
        });
 
413
      }).execute();
 
414
 
 
415
      fakeTimer.tick(500);
 
416
      expect(afterEach).toHaveBeenCalled();
 
417
    });
 
418
 
 
419
    it("runs single-spec after functions after timing out", function() {
 
420
      var after = jasmine.createSpy('after');
 
421
 
 
422
      env.describe('foo', function () {
 
423
        env.it('waitsFor', function () {
 
424
          this.after(after);
 
425
          this.waitsFor(100, function() {
 
426
            return false;
 
427
          });
 
428
        });
 
429
      }).execute();
 
430
 
 
431
      fakeTimer.tick(500);
 
432
      expect(after).toHaveBeenCalled();
 
433
    });
 
434
 
 
435
    describe('with consecutive calls', function () {
 
436
      var foo;
 
437
      beforeEach(function () {
 
438
        foo = 0;
 
439
      });
 
440
 
 
441
      it('exits immediately (does not stack) if the latchFunction times out', function () {
 
442
        var reachedFirstWaitsFor = false;
 
443
        var reachedSecondWaitsFor = false;
 
444
        env.describe('suite that waits', function () {
 
445
          env.it('should stack timeouts', function() {
 
446
            this.waitsFor(500, function () {
 
447
              reachedFirstWaitsFor = true;
 
448
              return false;
 
449
            });
 
450
            this.waitsFor(500, function () {
 
451
              reachedSecondWaitsFor = true;
 
452
            });
 
453
            this.runs(function () {
 
454
              foo++;
 
455
            });
 
456
          });
 
457
        });
 
458
 
 
459
        expect(reachedFirstWaitsFor).toEqual(false);
 
460
        env.execute();
 
461
 
 
462
        expect(reachedFirstWaitsFor).toEqual(true);
 
463
        expect(foo).toEqual(0);
 
464
        expect(reachedSecondWaitsFor).toEqual(false);
 
465
        fakeTimer.tick(500);
 
466
        expect(reachedSecondWaitsFor).toEqual(false);
 
467
        expect(foo).toEqual(0);
 
468
        fakeTimer.tick(500);
 
469
        expect(reachedSecondWaitsFor).toEqual(false);
 
470
        expect(foo).toEqual(0);
 
471
      });
 
472
 
 
473
      it('stacks latchFunctions', function () {
 
474
        var firstWaitsResult = false;
 
475
        var secondWaitsResult = false;
 
476
        var waitsSuite = env.describe('suite that waits', function () {
 
477
          env.it('spec with waitsFors', function() {
 
478
            this.waitsFor(600, function () {
 
479
              fakeTimer.setTimeout(function () {
 
480
                firstWaitsResult = true;
 
481
              }, 300);
 
482
              return firstWaitsResult;
 
483
            });
 
484
            this.waitsFor(600, function () {
 
485
              fakeTimer.setTimeout(function () {
 
486
                secondWaitsResult = true;
 
487
              }, 300);
 
488
              return secondWaitsResult;
 
489
            });
 
490
            this.runs(function () {
 
491
              foo++;
 
492
            });
 
493
          });
 
494
        });
 
495
 
 
496
        expect(firstWaitsResult).toEqual(false);
 
497
        expect(secondWaitsResult).toEqual(false);
 
498
        waitsSuite.execute();
 
499
 
 
500
        expect(firstWaitsResult).toEqual(false);
 
501
        expect(secondWaitsResult).toEqual(false);
 
502
        expect(foo).toEqual(0);
 
503
 
 
504
        fakeTimer.tick(300);
 
505
 
 
506
        expect(firstWaitsResult).toEqual(true);
 
507
        expect(secondWaitsResult).toEqual(false);
 
508
        expect(foo).toEqual(0);
 
509
 
 
510
        fakeTimer.tick(300);
 
511
 
 
512
        expect(firstWaitsResult).toEqual(true);
 
513
        expect(secondWaitsResult).toEqual(true);
 
514
        expect(foo).toEqual(1);
 
515
 
 
516
      });
 
517
    });
 
518
  });
 
519
 
 
520
  it("testSpecAfter", function() {
 
521
    var log = "";
 
522
    var spec;
 
523
    var suite = env.describe("has after", function() {
 
524
      spec = env.it('spec with after', function() {
 
525
        this.runs(function() {
 
526
          log += "spec";
 
527
        });
 
528
      });
 
529
    });
 
530
    spec.after(function() {
 
531
      log += "after1";
 
532
    });
 
533
    spec.after(function() {
 
534
      log += "after2";
 
535
    });
 
536
 
 
537
    suite.execute();
 
538
 
 
539
    expect(log).toEqual("specafter2after1");
 
540
  });
 
541
 
 
542
  describe('test suite declaration', function() {
 
543
    var suite;
 
544
    var dummyFunction = function() {
 
545
    };
 
546
 
 
547
    it('should give the suite a description', function() {
 
548
      suite = env.describe('one suite description', dummyFunction);
 
549
      expect(suite.description).toEqual('one suite description');
 
550
    });
 
551
 
 
552
    it('should enqueue functions for multipart tests and support waits, and run any ready runs() blocks', function() {
 
553
      var foo = 0;
 
554
      var bar = 0;
 
555
 
 
556
      suite = env.describe('one suite description', function () {
 
557
        env.it('should be a test with queuedFunctions', function() {
 
558
          this.runs(function() {
 
559
            foo++;
 
560
          });
 
561
          this.waits(100);
 
562
          this.runs(function() {
 
563
            bar++;
 
564
          });
 
565
        });
 
566
      });
 
567
 
 
568
      suite.execute();
 
569
 
 
570
      expect(foo).toEqual(1);
 
571
      expect(bar).toEqual(0);
 
572
 
 
573
      fakeTimer.tick(100);
 
574
      expect(bar).toEqual(1);
 
575
    });
 
576
 
 
577
  });
 
578
 
 
579
  it("testBeforeAndAfterCallbacks", function () {
 
580
    var suiteWithBefore = env.describe('one suite with a before', function () {
 
581
 
 
582
      this.beforeEach(function () {
 
583
        this.foo = 1;
 
584
      });
 
585
 
 
586
      env.it('should be a spec', function () {
 
587
        this.runs(function() {
 
588
          this.foo++;
 
589
          this.expect(this.foo).toEqual(2);
 
590
        });
 
591
      });
 
592
 
 
593
      env.it('should be another spec', function () {
 
594
        this.runs(function() {
 
595
          this.foo++;
 
596
          this.expect(this.foo).toEqual(2);
 
597
        });
 
598
      });
 
599
    });
 
600
 
 
601
    suiteWithBefore.execute();
 
602
 
 
603
    var suite = suiteWithBefore;
 
604
 
 
605
    expect(suite.results().getItems()[0].passed()).toEqual(true); // "testBeforeAndAfterCallbacks: the first spec's foo should have been 2");
 
606
    expect(suite.results().getItems()[1].passed()).toEqual(true); // "testBeforeAndAfterCallbacks: the second spec's this.foo should have been 2");
 
607
 
 
608
 
 
609
    var foo = 1;
 
610
    var suiteWithAfter = env.describe('one suite with an after_each', function () {
 
611
 
 
612
      env.it('should be a spec with an after_each', function () {
 
613
        this.expect(foo).toEqual(1);
 
614
        foo++;
 
615
        this.expect(foo).toEqual(2);
 
616
      });
 
617
 
 
618
      env.it('should be another spec with an after_each', function () {
 
619
        this.expect(foo).toEqual(0);
 
620
        foo++;
 
621
        this.expect(foo).toEqual(1);
 
622
      });
 
623
 
 
624
      this.afterEach(function () {
 
625
        foo = 0;
 
626
      });
 
627
    });
 
628
 
 
629
    suiteWithAfter.execute();
 
630
 
 
631
    suite = suiteWithAfter;
 
632
    expect(suite.afterEach.length).toEqual(1);
 
633
    expect(suite.results().getItems()[0].passed()).toEqual(true);
 
634
    expect(suite.results().getItems()[1].passed()).toEqual(true);
 
635
    expect(foo).toEqual(0);
 
636
 
 
637
  });
 
638
 
 
639
  it('#waits should allow consecutive waits calls', function () {
 
640
    var foo = 0;
 
641
    var waitsSuite = env.describe('suite that waits', function () {
 
642
      env.it('should wait', function() {
 
643
        this.waits(500);
 
644
        this.waits(500);
 
645
        this.runs(function () {
 
646
          foo++;
 
647
        });
 
648
      });
 
649
    });
 
650
 
 
651
    waitsSuite.execute();
 
652
    expect(foo).toEqual(0);
 
653
    fakeTimer.tick(500);
 
654
    expect(foo).toEqual(0);
 
655
    fakeTimer.tick(500);
 
656
 
 
657
    expect(foo).toEqual(1);
 
658
  });
 
659
 
 
660
  it('nested suites', function () {
 
661
 
 
662
    var foo = 0;
 
663
    var bar = 0;
 
664
    var baz = 0;
 
665
    var quux = 0;
 
666
    var nested = env.describe('suite', function () {
 
667
      env.describe('nested', function () {
 
668
        env.it('should run nested suites', function () {
 
669
          foo++;
 
670
        });
 
671
        env.it('should run nested suites', function () {
 
672
          bar++;
 
673
        });
 
674
      });
 
675
 
 
676
      env.describe('nested 2', function () {
 
677
        env.it('should run suites following nested suites', function () {
 
678
          baz++;
 
679
        });
 
680
      });
 
681
 
 
682
      env.it('should run tests following nested suites', function () {
 
683
        quux++;
 
684
      });
 
685
    });
 
686
 
 
687
    expect(foo).toEqual(0);
 
688
    expect(bar).toEqual(0);
 
689
    expect(baz).toEqual(0);
 
690
    expect(quux).toEqual(0);
 
691
    nested.execute();
 
692
 
 
693
    expect(foo).toEqual(1);
 
694
    expect(bar).toEqual(1);
 
695
    expect(baz).toEqual(1);
 
696
    expect(quux).toEqual(1);
 
697
  });
 
698
 
 
699
  it("#beforeEach should be able to eval runs and waits blocks", function () {
 
700
    var foo = 0;
 
701
    var bar = 0;
 
702
    var suiteWithBefore = env.describe('one suite with a before', function () {
 
703
      this.beforeEach(function () {
 
704
        this.runs(function () {
 
705
          foo++;
 
706
        });
 
707
        this.waits(500);
 
708
        this.runs(function () {
 
709
          foo++;
 
710
        });
 
711
        this.waits(500);
 
712
      });
 
713
 
 
714
      env.it('should be a spec', function () {
 
715
        bar = 1;
 
716
        foo++;
 
717
      });
 
718
 
 
719
    });
 
720
 
 
721
    expect(foo).toEqual(0);
 
722
    expect(bar).toEqual(0);
 
723
    suiteWithBefore.execute();
 
724
 
 
725
    expect(bar).toEqual(0);
 
726
    expect(foo).toEqual(1);
 
727
    fakeTimer.tick(500);
 
728
 
 
729
    expect(bar).toEqual(0);
 
730
    expect(foo).toEqual(2);
 
731
    fakeTimer.tick(500);
 
732
    expect(bar).toEqual(1);
 
733
    expect(foo).toEqual(3);
 
734
  });
 
735
 
 
736
  it("#afterEach should be able to eval runs and waits blocks", function () {
 
737
    var foo = 0;
 
738
    var firstSpecHasRun = false;
 
739
    var secondSpecHasRun = false;
 
740
    var suiteWithAfter = env.describe('one suite with a before', function () {
 
741
      this.afterEach(function () {
 
742
        this.waits(500);
 
743
        this.runs(function () {
 
744
          foo++;
 
745
        });
 
746
        this.waits(500);
 
747
      });
 
748
 
 
749
      env.it('should be the first spec', function () {
 
750
        firstSpecHasRun = true;
 
751
      });
 
752
 
 
753
      env.it('should be a spec', function () {
 
754
        secondSpecHasRun = true;
 
755
        foo++;
 
756
      });
 
757
 
 
758
    });
 
759
 
 
760
    expect(firstSpecHasRun).toEqual(false);
 
761
    expect(secondSpecHasRun).toEqual(false);
 
762
    expect(foo).toEqual(0);
 
763
 
 
764
    suiteWithAfter.execute();
 
765
 
 
766
 
 
767
    expect(firstSpecHasRun).toEqual(true);
 
768
    expect(secondSpecHasRun).toEqual(false);
 
769
    expect(foo).toEqual(0);
 
770
 
 
771
    fakeTimer.tick(500);
 
772
 
 
773
    expect(foo).toEqual(1);
 
774
    expect(secondSpecHasRun).toEqual(false);
 
775
    fakeTimer.tick(500);
 
776
 
 
777
    expect(foo).toEqual(2);
 
778
    expect(secondSpecHasRun).toEqual(true);
 
779
 
 
780
  });
 
781
 
 
782
  it("Spec#after should be able to eval runs and waits blocks", function () {
 
783
    var runsBeforeAfter = false;
 
784
    var firstSpecHasRun = false;
 
785
    var secondSpecHasRun = false;
 
786
    var afterHasRun = false;
 
787
    var suiteWithAfter = env.describe('one suite with a before', function () {
 
788
 
 
789
      env.it('should be the first spec', function () {
 
790
        firstSpecHasRun = true;
 
791
        this.after(function () {
 
792
          this.waits(500);
 
793
          this.runs(function () {
 
794
            afterHasRun = true;
 
795
          });
 
796
          this.waits(500);
 
797
        }, true);
 
798
        this.waits(500);
 
799
        this.runs(function () {
 
800
          runsBeforeAfter = true;
 
801
        });
 
802
      });
 
803
 
 
804
      env.it('should be a spec', function () {
 
805
        secondSpecHasRun = true;
 
806
      });
 
807
 
 
808
    });
 
809
 
 
810
    expect(firstSpecHasRun).toEqual(false);
 
811
    expect(runsBeforeAfter).toEqual(false);
 
812
    expect(afterHasRun).toEqual(false);
 
813
    expect(secondSpecHasRun).toEqual(false);
 
814
 
 
815
    suiteWithAfter.execute();
 
816
 
 
817
    expect(firstSpecHasRun).toEqual(true);
 
818
    expect(runsBeforeAfter).toEqual(false);
 
819
    expect(afterHasRun).toEqual(false);
 
820
    expect(secondSpecHasRun).toEqual(false);
 
821
 
 
822
    fakeTimer.tick(500);
 
823
 
 
824
    expect(firstSpecHasRun).toEqual(true);
 
825
    expect(runsBeforeAfter).toEqual(true);
 
826
    expect(afterHasRun).toEqual(false);
 
827
    expect(secondSpecHasRun).toEqual(false);
 
828
 
 
829
    fakeTimer.tick(500);
 
830
 
 
831
    expect(firstSpecHasRun).toEqual(true);
 
832
    expect(runsBeforeAfter).toEqual(true);
 
833
    expect(afterHasRun).toEqual(true);
 
834
    expect(secondSpecHasRun).toEqual(false);
 
835
 
 
836
    fakeTimer.tick(500);
 
837
 
 
838
    expect(firstSpecHasRun).toEqual(true);
 
839
    expect(runsBeforeAfter).toEqual(true);
 
840
    expect(afterHasRun).toEqual(true);
 
841
    expect(secondSpecHasRun).toEqual(true);
 
842
  });
 
843
 
 
844
  it("handles waits", function () {
 
845
    var firstSpecHasRun = false;
 
846
    var secondSpecHasRun = false;
 
847
    var suiteWithAfter = env.describe('one suite with a before', function () {
 
848
 
 
849
      env.it('should be the first spec', function () {
 
850
        this.waits(500);
 
851
        this.runs(function () {
 
852
          firstSpecHasRun = true;
 
853
        });
 
854
      });
 
855
 
 
856
      env.it('should be a spec', function () {
 
857
        secondSpecHasRun = true;
 
858
      });
 
859
 
 
860
    });
 
861
 
 
862
    expect(firstSpecHasRun).toEqual(false);
 
863
    expect(secondSpecHasRun).toEqual(false);
 
864
 
 
865
    suiteWithAfter.execute();
 
866
 
 
867
    expect(firstSpecHasRun).toEqual(false);
 
868
    expect(secondSpecHasRun).toEqual(false);
 
869
 
 
870
    fakeTimer.tick(500);
 
871
 
 
872
    expect(firstSpecHasRun).toEqual(true);
 
873
    expect(secondSpecHasRun).toEqual(true);
 
874
  });
 
875
 
 
876
  it("testBeforeExecutesSafely", function() {
 
877
    var report = "";
 
878
    var suite = env.describe('before fails on first test, passes on second', function() {
 
879
      var counter = 0;
 
880
      this.beforeEach(function() {
 
881
        counter++;
 
882
        if (counter == 1) {
 
883
          throw "before failure";
 
884
        }
 
885
      });
 
886
      env.it("first should not run because before fails", function() {
 
887
        this.runs(function() {
 
888
          report += "first";
 
889
          this.expect(true).toEqual(true);
 
890
        });
 
891
      });
 
892
      env.it("second should run and pass because before passes", function() {
 
893
        this.runs(function() {
 
894
          report += "second";
 
895
          this.expect(true).toEqual(true);
 
896
        });
 
897
      });
 
898
    });
 
899
 
 
900
    suite.execute();
 
901
 
 
902
    expect(report).toEqual("firstsecond");
 
903
    var suiteResults = suite.results();
 
904
    expect(suiteResults.getItems()[0].getItems()[0].passed()).toEqual(false);
 
905
    expect(suiteResults.getItems()[1].getItems()[0].passed()).toEqual(true);
 
906
  });
 
907
 
 
908
  it("testAfterExecutesSafely", function() {
 
909
    var report = "";
 
910
    var suite = env.describe('after fails on first test, then passes', function() {
 
911
      var counter = 0;
 
912
      this.afterEach(function() {
 
913
        counter++;
 
914
        if (counter == 1) {
 
915
          throw "after failure";
 
916
        }
 
917
      });
 
918
      env.it("first should run, expectation passes, but spec fails because after fails", function() {
 
919
        this.runs(function() {
 
920
          report += "first";
 
921
          this.expect(true).toEqual(true);
 
922
        });
 
923
      });
 
924
      env.it("second should run and pass because after passes", function() {
 
925
        this.runs(function() {
 
926
          report += "second";
 
927
          this.expect(true).toEqual(true);
 
928
        });
 
929
      });
 
930
      env.it("third should run and pass because after passes", function() {
 
931
        this.runs(function() {
 
932
          report += "third";
 
933
          this.expect(true).toEqual(true);
 
934
        });
 
935
      });
 
936
    });
 
937
 
 
938
    suite.execute();
 
939
 
 
940
    expect(report).toEqual("firstsecondthird"); // "all tests should run");
 
941
    //After each errors should not go in spec results because it confuses the count.
 
942
    var suiteResults = suite.results();
 
943
    expect(suiteResults.getItems().length).toEqual(3, 'testAfterExecutesSafely should have results for three specs');
 
944
 
 
945
    expect(suiteResults.getItems()[0].getItems()[0].passed()).toEqual(true, "testAfterExecutesSafely 1st spec should pass");
 
946
    expect(suiteResults.getItems()[1].getItems()[0].passed()).toEqual(true, "testAfterExecutesSafely 2nd spec should pass");
 
947
    expect(suiteResults.getItems()[2].getItems()[0].passed()).toEqual(true, "testAfterExecutesSafely 3rd spec should pass");
 
948
 
 
949
    expect(suiteResults.getItems()[0].getItems()[0].passed()).toEqual(true, "testAfterExecutesSafely 1st result for 1st suite spec should pass");
 
950
    expect(suiteResults.getItems()[0].getItems()[1].passed()).toEqual(false, "testAfterExecutesSafely 2nd result for 1st suite spec should fail because afterEach failed");
 
951
    expect(suiteResults.getItems()[1].getItems()[0].passed()).toEqual(true, "testAfterExecutesSafely 2nd suite spec should pass");
 
952
    expect(suiteResults.getItems()[2].getItems()[0].passed()).toEqual(true, "testAfterExecutesSafely 3rd suite spec should pass");
 
953
  });
 
954
 
 
955
  it("should permit nested describes", function() {
 
956
    var actions = [];
 
957
 
 
958
    env.beforeEach(function () {
 
959
      actions.push('runner beforeEach');
 
960
    });
 
961
 
 
962
    env.afterEach(function () {
 
963
      actions.push('runner afterEach');
 
964
    });
 
965
 
 
966
    env.describe('Something', function() {
 
967
      env.beforeEach(function() {
 
968
        actions.push('outer beforeEach');
 
969
      });
 
970
 
 
971
      env.afterEach(function() {
 
972
        actions.push('outer afterEach');
 
973
      });
 
974
 
 
975
      env.it('does it 1', function() {
 
976
        actions.push('outer it 1');
 
977
      });
 
978
 
 
979
      env.describe('Inner 1', function() {
 
980
        env.beforeEach(function() {
 
981
          actions.push('inner 1 beforeEach');
 
982
        });
 
983
 
 
984
        env.afterEach(function() {
 
985
          actions.push('inner 1 afterEach');
 
986
        });
 
987
 
 
988
        env.it('does it 2', function() {
 
989
          actions.push('inner 1 it');
 
990
        });
 
991
      });
 
992
 
 
993
      env.it('does it 3', function() {
 
994
        actions.push('outer it 2');
 
995
      });
 
996
 
 
997
      env.describe('Inner 2', function() {
 
998
        env.beforeEach(function() {
 
999
          actions.push('inner 2 beforeEach');
 
1000
        });
 
1001
 
 
1002
        env.afterEach(function() {
 
1003
          actions.push('inner 2 afterEach');
 
1004
        });
 
1005
 
 
1006
        env.it('does it 2', function() {
 
1007
          actions.push('inner 2 it');
 
1008
        });
 
1009
      });
 
1010
    });
 
1011
 
 
1012
    env.execute();
 
1013
 
 
1014
 
 
1015
    var expected = [
 
1016
      "runner beforeEach",
 
1017
      "outer beforeEach",
 
1018
      "outer it 1",
 
1019
      "outer afterEach",
 
1020
      "runner afterEach",
 
1021
 
 
1022
      "runner beforeEach",
 
1023
      "outer beforeEach",
 
1024
      "inner 1 beforeEach",
 
1025
      "inner 1 it",
 
1026
      "inner 1 afterEach",
 
1027
      "outer afterEach",
 
1028
      "runner afterEach",
 
1029
 
 
1030
      "runner beforeEach",
 
1031
      "outer beforeEach",
 
1032
      "outer it 2",
 
1033
      "outer afterEach",
 
1034
      "runner afterEach",
 
1035
 
 
1036
      "runner beforeEach",
 
1037
      "outer beforeEach",
 
1038
      "inner 2 beforeEach",
 
1039
      "inner 2 it",
 
1040
      "inner 2 afterEach",
 
1041
      "outer afterEach",
 
1042
      "runner afterEach"
 
1043
    ];
 
1044
    expect(actions).toEqual(expected);
 
1045
  });
 
1046
 
 
1047
  it("should run multiple befores and afters in the order they are declared", function() {
 
1048
    var actions = [];
 
1049
 
 
1050
    env.beforeEach(function () {
 
1051
      actions.push('runner beforeEach1');
 
1052
    });
 
1053
 
 
1054
    env.afterEach(function () {
 
1055
      actions.push('runner afterEach1');
 
1056
    });
 
1057
 
 
1058
    env.beforeEach(function () {
 
1059
      actions.push('runner beforeEach2');
 
1060
    });
 
1061
 
 
1062
    env.afterEach(function () {
 
1063
      actions.push('runner afterEach2');
 
1064
    });
 
1065
 
 
1066
    env.describe('Something', function() {
 
1067
      env.beforeEach(function() {
 
1068
        actions.push('beforeEach1');
 
1069
      });
 
1070
 
 
1071
      env.afterEach(function() {
 
1072
        actions.push('afterEach1');
 
1073
      });
 
1074
 
 
1075
      env.beforeEach(function() {
 
1076
        actions.push('beforeEach2');
 
1077
      });
 
1078
 
 
1079
      env.afterEach(function() {
 
1080
        actions.push('afterEach2');
 
1081
      });
 
1082
 
 
1083
      env.it('does it 1', function() {
 
1084
        actions.push('outer it 1');
 
1085
      });
 
1086
    });
 
1087
 
 
1088
    env.execute();
 
1089
 
 
1090
    var expected = [
 
1091
      "runner beforeEach1",
 
1092
      "runner beforeEach2",
 
1093
      "beforeEach1",
 
1094
      "beforeEach2",
 
1095
      "outer it 1",
 
1096
      "afterEach2",
 
1097
      "afterEach1",
 
1098
      "runner afterEach2",
 
1099
      "runner afterEach1"
 
1100
    ];
 
1101
    expect(actions).toEqual(expected);
 
1102
  });
 
1103
 
 
1104
  it("builds up nested names", function() {
 
1105
    var nestedSpec;
 
1106
    env.describe('Test Subject', function() {
 
1107
      env.describe('when under circumstance A', function() {
 
1108
        env.describe('and circumstance B', function() {
 
1109
          nestedSpec = env.it('behaves thusly', function() {
 
1110
          });
 
1111
        });
 
1112
      });
 
1113
    });
 
1114
 
 
1115
    expect(nestedSpec.getFullName()).toEqual('Test Subject when under circumstance A and circumstance B behaves thusly.'); //, "Spec.fullName was incorrect: " + nestedSpec.getFullName());
 
1116
  });
 
1117
 
 
1118
  it("should skip empty suites", function () {
 
1119
    env.describe('NonEmptySuite1', function() {
 
1120
      env.it('should pass', function() {
 
1121
        this.expect(true).toEqual(true);
 
1122
      });
 
1123
      env.describe('NestedEmptySuite', function() {
 
1124
      });
 
1125
      env.it('should pass', function() {
 
1126
        this.expect(true).toEqual(true);
 
1127
      });
 
1128
    });
 
1129
 
 
1130
    env.describe('EmptySuite', function() {
 
1131
    });
 
1132
 
 
1133
    env.describe('NonEmptySuite2', function() {
 
1134
      env.it('should pass', function() {
 
1135
        this.expect(true).toEqual(true);
 
1136
      });
 
1137
    });
 
1138
 
 
1139
    env.execute();
 
1140
 
 
1141
    var runnerResults = env.currentRunner_.results();
 
1142
    expect(runnerResults.totalCount).toEqual(3);
 
1143
    expect(runnerResults.passedCount).toEqual(3);
 
1144
    expect(runnerResults.failedCount).toEqual(0);
 
1145
  });
 
1146
 
 
1147
  it("should bind 'this' to the running spec within the spec body", function() {
 
1148
    var spec;
 
1149
    var suite = env.describe('one suite description', function () {
 
1150
      env.it('should be a test with queuedFunctions', function() {
 
1151
        spec = this.runs(function() {
 
1152
          this.foo = 0;
 
1153
          this.foo++;
 
1154
        });
 
1155
 
 
1156
        this.runs(function() {
 
1157
          var that = this;
 
1158
          fakeTimer.setTimeout(function() {
 
1159
            that.foo++;
 
1160
          }, 250);
 
1161
        });
 
1162
 
 
1163
        this.runs(function() {
 
1164
          this.expect(this.foo).toEqual(2);
 
1165
        });
 
1166
 
 
1167
        this.waits(300);
 
1168
 
 
1169
        this.runs(function() {
 
1170
          this.expect(this.foo).toEqual(2);
 
1171
        });
 
1172
      });
 
1173
 
 
1174
    });
 
1175
 
 
1176
    suite.execute();
 
1177
    fakeTimer.tick(600);
 
1178
    expect(spec.foo).toEqual(2);
 
1179
    var suiteResults = suite.results();
 
1180
    expect(suiteResults.getItems()[0].getItems().length).toEqual(2);
 
1181
    expect(suiteResults.getItems()[0].getItems()[0].passed()).toEqual(false);
 
1182
    expect(suiteResults.getItems()[0].getItems()[1].passed()).toEqual(true);
 
1183
  });
 
1184
 
 
1185
  it("shouldn't run disabled tests", function() {
 
1186
    var xitSpecWasRun = false;
 
1187
    var suite = env.describe('default current suite', function() {
 
1188
      env.xit('disabled spec').runs(function () {
 
1189
        xitSpecWasRun = true;
 
1190
      });
 
1191
 
 
1192
      env.it('enabled spec').runs(function () {
 
1193
        var foo = 'bar';
 
1194
        expect(foo).toEqual('bar');
 
1195
      });
 
1196
    });
 
1197
 
 
1198
    suite.execute();
 
1199
    expect(xitSpecWasRun).toEqual(false);
 
1200
  });
 
1201
 
 
1202
  it('shouldn\'t execute specs in disabled suites', function() {
 
1203
    var spy = jasmine.createSpy();
 
1204
    var disabledSuite = env.xdescribe('a disabled suite', function() {
 
1205
      env.it('enabled spec, but should not be run', function() {
 
1206
        spy();
 
1207
      });
 
1208
    });
 
1209
 
 
1210
    disabledSuite.execute();
 
1211
 
 
1212
    expect(spy).not.toHaveBeenCalled();
 
1213
  });
 
1214
 
 
1215
  it('#explodes should throw an exception when it is called inside a spec', function() {
 
1216
    var exceptionMessage = false;
 
1217
    var anotherSuite = env.describe('Spec', function () {
 
1218
      env.it('plodes', function() {
 
1219
        try {
 
1220
          this.explodes();
 
1221
        }
 
1222
        catch (e) {
 
1223
          exceptionMessage = e;
 
1224
        }
 
1225
        expect(exceptionMessage).toNotEqual(false);
 
1226
      });
 
1227
    });
 
1228
 
 
1229
    anotherSuite.execute();
 
1230
 
 
1231
    expect(exceptionMessage).toEqual('explodes function should not have been called');
 
1232
  });
 
1233
 
 
1234
  it("should recover gracefully when there are errors in describe functions", function() {
 
1235
    var specs = [];
 
1236
    var superSimpleReporter = new jasmine.Reporter();
 
1237
    superSimpleReporter.reportSpecResults = function(spec) {
 
1238
      specs.push("Spec: " + spec.getFullName());
 
1239
      var results = spec.results().getItems();
 
1240
      for (var i = 0; i < results.length; i++) {
 
1241
        var result = results[i];
 
1242
        specs.push("Result: " + result);
 
1243
      }
 
1244
    };
 
1245
 
 
1246
    try {
 
1247
      env.describe("outer1", function() {
 
1248
        env.describe("inner1", function() {
 
1249
          env.it("should thingy", function() {
 
1250
            this.expect(true).toEqual(true);
 
1251
          });
 
1252
 
 
1253
          throw new Error("fake error");
 
1254
        });
 
1255
 
 
1256
        env.describe("inner2", function() {
 
1257
          env.it("should other thingy", function() {
 
1258
            this.expect(true).toEqual(true);
 
1259
          });
 
1260
        });
 
1261
 
 
1262
        throw new Error("fake error");
 
1263
 
 
1264
      });
 
1265
    } catch(e) {
 
1266
    }
 
1267
 
 
1268
    env.describe("outer2", function() {
 
1269
      env.it("should xxx", function() {
 
1270
        this.expect(true).toEqual(true);
 
1271
      });
 
1272
    });
 
1273
 
 
1274
    env.addReporter(superSimpleReporter);
 
1275
    env.execute();
 
1276
 
 
1277
    expect(specs.join('')).toMatch(new RegExp(
 
1278
      'Spec: outer1 inner1 should thingy.' +
 
1279
        'Result: Passed.' +
 
1280
        'Spec: outer1 inner1 encountered a declaration exception.' +
 
1281
        'Result: Error: fake error.*' +
 
1282
        'Spec: outer1 inner2 should other thingy.' +
 
1283
        'Result: Passed.' +
 
1284
        'Spec: outer1 encountered a declaration exception.' +
 
1285
        'Result: Error: fake error.*' +
 
1286
        'Spec: outer2 should xxx.' +
 
1287
        'Result: Passed.'
 
1288
      ));
 
1289
  });
 
1290
 
 
1291
});