|
| 1 | +const { expect } = require('chai'); |
| 2 | +const sinon = require('sinon'); |
1 | 3 | const { Logger } = require('../logger/logger'); |
2 | 4 | const { Timer } = require('./timer'); |
3 | 5 |
|
4 | 6 | describe('Timer', function() { |
| 7 | + let clock; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + clock = sinon.useFakeTimers(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + clock.restore(); |
| 15 | + }); |
| 16 | + |
5 | 17 | it('should log elapsed time', function() { |
6 | 18 | const logger = new Logger('test', false); |
7 | | - const infoStub = this.sandbox.stub(logger, 'info'); |
| 19 | + const infoStub = sinon.stub(logger, 'info'); |
8 | 20 | const timer = new Timer(logger); |
9 | 21 |
|
10 | | - this.clock.tick(100); |
| 22 | + clock.tick(100); |
11 | 23 | timer.info('time', { customer_id: 10 }); |
12 | 24 |
|
13 | 25 | expect(infoStub).to.have.been. calledWith('time', { customer_id: 10, duration: 100 }); |
14 | 26 | }); |
15 | 27 |
|
16 | 28 | it('should log elapsed time with error', function() { |
17 | 29 | const logger = new Logger('test', false); |
18 | | - const errorStub = this.sandbox.stub(logger, 'fromError'); |
| 30 | + const errorStub = sinon.stub(logger, 'fromError'); |
19 | 31 | const timer = new Timer(logger); |
20 | 32 | const error = new Error('intended'); |
21 | 33 |
|
22 | | - this.clock.tick(100); |
| 34 | + clock.tick(100); |
23 | 35 | timer.fromError('time', error, { customer_id: 10 }); |
24 | 36 |
|
25 | 37 | expect(errorStub).to.have.been. calledWith('time', error, { customer_id: 10, duration: 100 }); |
26 | 38 | }); |
27 | 39 |
|
28 | 40 | it('should log elapsed time with error', function() { |
29 | 41 | const logger = new Logger('test', false); |
30 | | - const errorStub = this.sandbox.stub(logger, 'warnFromError'); |
| 42 | + const errorStub = sinon.stub(logger, 'warnFromError'); |
31 | 43 | const timer = new Timer(logger); |
32 | 44 | const error = new Error('intended'); |
33 | 45 |
|
34 | | - this.clock.tick(100); |
| 46 | + clock.tick(100); |
35 | 47 | timer.warnFromError('time', error, { customer_id: 10 }); |
36 | 48 |
|
37 | | - expect(errorStub).to.have.been. calledWith('time', error, { customer_id: 10, duration: 100 }); |
| 49 | + expect(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, duration: 100 }); |
38 | 50 | }); |
39 | 51 | }); |
0 commit comments