Skip to content

Commit

Permalink
Changed API to multiple exports
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Dec 19, 2024
1 parent 4b4689e commit 158d0cd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 3.0.0
- Changed API to export fake and real clocks instead of using functions

## 2.0.0
- Updated dependencies
- Updated node version
Expand Down
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ groundhog-day is a wrapper around ```Date.now()``` with real and fake implementa

### 1. Use a fake clock in tests
```js
const Server = require('../server')
const request = require('request')
const clock = require('groundhog-day').fake()
const assert = require('assert')
const { ok, equal } = require('assert');
const { fake: clock } = require('groundhog-day');
const request = require('request');
const Server = require('../server');

describe('Server', () => {

let server

before(done => {
server = new Server(clock)
server.start(done)
})
before((done) => {
server = new Server(clock);
server.start(done);
});

after(done => {
server.stop(done)
})
after((done) => {
server.stop(done);
});

it('should set last modified header', done => {
it('should set last modified header', (done) => {
request.get('http://localhost/demo', (err, res, body) => {
assert.ok(err)
assert.equal(res.headers['last-modified'], 'Tue, 2 Feb 2016 11:00:00 GMT') // Groundhog Day
ok(err);
equal(res.headers['last-modified'], 'Tue, 2 Feb 2016 11:00:00 GMT'); // Groundhog Day
})
})
})
Expand All @@ -42,7 +42,7 @@ describe('Server', () => {
### 2. Use a real clock in production
```js
const Server = require('./server')
const clock = require('groundhog-day').real()
const { real: clock } = require('groundhog-day');
new Server(clock).start(err => {
if (err) process.exit(1)
console.log('Listening')
Expand All @@ -54,18 +54,18 @@ You can configure the fixed time returned by the fake clock in any of the follow

By specifying the number of milliseconds
```js
const clock = require('groundhog-day').fake()
clock.fix(1469563181761)
const { fake: clock } = require('groundhog-day');
clock.fix(1469563181761);
```

By specifying a date instance
```js
const clock = require('groundhog-day').fake()
clock.fix(new Date(1469563181761))
const { fake: clock } = require('groundhog-day');
clock.fix(new Date(1469563181761));
```

By specifying a date string
```js
const clock = require('groundhog-day').fake()
const { fake: clock } = require('groundhog-day');
clock.fix(new Date('2016-07-26T19:59:41.761Z'))
```
35 changes: 15 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
let time = new Date('2016-02-02T11:00:00.000Z').getTime();

module.exports = {
real() {
return {
now() {
return Date.now();
},
};
real: {
now() {
return Date.now();
},
},
fake() {

let time = new Date('2016-02-02T11:00:00.000Z').getTime();

return {
now() {
return time;
},
fix(value) {
time = new Date(value).getTime();
if (Number.isNaN(time)) throw new Error(`Invalid Date: ${value}`);
return this;
},
};
fake: {
now() {
return time;
},
fix(value) {
time = new Date(value).getTime();
if (Number.isNaN(time)) throw new Error(`Invalid Date: ${value}`);
return this;
},
},
};
14 changes: 7 additions & 7 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
const assert = require('assert');
const { describe, it } = require('zunit');
const clock = require('..');
const { fake, real } = require('..');

describe('clocks', () => {

it('fake clock should default to groundhog day', () => {
assert.equal(clock.fake().now(), 1454410800000);
assert.equal(fake.now(), 1454410800000);
});

it('fake clock should fixed time', () => {
assert.equal(clock.fake().fix(1469563181761).now(), 1469563181761);
assert.equal(clock.fake().fix(new Date(1469563181761)).now(), 1469563181761);
assert.equal(clock.fake().fix(new Date(1469563181761).toISOString()).now(), 1469563181761);
assert.equal(fake.fix(1469563181761).now(), 1469563181761);
assert.equal(fake.fix(new Date(1469563181761)).now(), 1469563181761);
assert.equal(fake.fix(new Date(1469563181761).toISOString()).now(), 1469563181761);
});

it('fake clock should reject attempts to fix time to an invalid value', () => {
assert.throws(() => {
clock.fake().fix('foo');
fake.fix('foo');
}, /Invalid Date: foo/);
});

it('real clock should use correct time', () => {
assert.ok(Math.abs(clock.real().now() - Date.now()) < 100);
assert.ok(Math.abs(real.now() - Date.now()) < 100);
});
});

0 comments on commit 158d0cd

Please sign in to comment.