Skip to content

Commit

Permalink
tests for chai.Timer before assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalparadox committed Apr 26, 2012
1 parent a730e7e commit 3fe6d51
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions test/timers.js
Expand Up @@ -9,11 +9,11 @@ var should = chai.should();
describe('Chai Timers', function () {

it('should attach to chai', function () {
chai.expect(chai).to.respondTo('timer');
chai.expect(chai).to.respondTo('Timer');
});

it('should know an object is a timer', function () {
var timer = chai.timer();
var timer = new chai.Timer();
timer.should.be.a.timer;
'hello world'.should.not.be.a.timer;

Expand All @@ -26,22 +26,56 @@ describe('Chai Timers', function () {
});

it('should correctly store start and end times', function (done) {
var timer = chai.timer();
should.equal(timer.__flags.start, null);
should.equal(timer.__flags.stop, null);
var timer = new chai.Timer();
should.equal(timer.started, null);
should.equal(timer.stopped, null);

timer.start();
timer.__flags.start.should.be.a('date');
should.equal(timer.__flags.stop, null);
timer.started.should.be.a('date');
should.equal(timer.stopped, null);

setTimeout(function () {
timer.stop();
timer.__flags.start.should.be.a('date');
timer.__flags.stop.should.be.a('date');
timer.started.should.be.a('date');
timer.stopped.should.be.a('date');
timer.created.should.be.a('date');

timer.should.have.property('elapsed')
.to.be.a('number').above('9');
done();
}, 10);
});

it('should be able to assert `before`', function (done) {
var timer1 = new chai.Timer('timer1');
timer1.start();

setTimeout(function () {
var timer2 = new chai.Timer('timer2');
timer1.stop();
timer2.start();

timer1.should.have.been.created.before(timer2);
timer2.should.have.not.been.created.before(timer1)

timer1.should.have.started.before(timer2);
timer2.should.have.not.started.before(timer1);

(function () {
timer1.should.have.not.started.before(timer2);
}).should.throw(chai.AssertionError, /to not have started before/);

(function () {
timer2.should.have.started.before(timer1);
}).should.throw(chai.AssertionError, /to have started before/);

setTimeout(function () {
timer2.stop();
timer1.should.have.stopped.before(timer2);
timer2.should.have.not.stopped.before(timer1);
done();
}, 10);
}, 10);
});

});

0 comments on commit 3fe6d51

Please sign in to comment.