https://github.com/bluelovers/node-fake-timer
Fake Timer API for node.js.
https://github.com/bluelovers/node-fake-timer
nodejs queue setinterval settimeout task timer
Last synced: about 1 year ago
JSON representation
Fake Timer API for node.js.
- Host: GitHub
- URL: https://github.com/bluelovers/node-fake-timer
- Owner: bluelovers
- Created: 2017-11-10T14:30:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-11T03:28:35.000Z (over 8 years ago)
- Last Synced: 2025-01-06T19:48:25.338Z (about 1 year ago)
- Topics: nodejs, queue, setinterval, settimeout, task, timer
- Language: TypeScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-fake-timer
Fake Timer API for node.js.
```javascript
import init, { setTimeout, setImmediate } from 'fake-timer';
import * as moment from 'moment';
setTimeout(() => {}, 1500);
let q = setTimeout(() => {}, 500);
setImmediate(() => {});
init.start(-1)
.then((self) =>
{
console.log(self.timer.data);
console.log(self.timer.queue);
console.log(self.cache.done);
console.log(self.timer.hasExpires());
return self.start(-1);
})
.then((self) =>
{
console.log(self.timer.data);
console.log(self.timer.queue);
console.log(self.cache.done);
console.log(self.timer.hasExpires());
return self;
})
.then((self) =>
{
console.log('[real]', moment().diff(self.timer.data.real_init), 'ms');
console.log('[fake]', self.timer.now().diff(self.timer.data.fake_init), 'ms');
})
;
```
```
{ id: 3,
real_init: moment("2017-11-10T22:46:00.523"),
fake_init: moment("2017-11-10T22:46:00.523"),
fake_now: moment("2017-11-10T22:46:00.523"),
fake_old: moment("2017-11-10T22:46:00.523") }
[ { id: 1,
name: 'BJg-2sVXkG',
timing: moment("2017-11-10T22:46:01.023"),
callback: [Function],
params: [],
type: 'setTimeout',
index: 1 },
{ id: 0,
name: 'H1-2jVQJz',
timing: moment("2017-11-10T22:46:02.023"),
callback: [Function],
params: [],
type: 'setTimeout',
index: 2 } ]
[ { id: 2,
name: 'BkW-2oVQkG',
timing: moment("2017-11-10T22:46:00.523"),
callback: [Function],
params: [],
type: 'setImmediate',
index: 0,
active: moment("2017-11-10T22:46:00.527"),
ending: moment("2017-11-10T22:46:00.527") } ]
false
{ id: 3,
real_init: moment("2017-11-10T22:46:00.523"),
fake_init: moment("2017-11-10T22:46:00.523"),
fake_now: moment("2017-11-10T22:46:01.023"),
fake_old: moment("2017-11-10T22:46:00.523") }
[ { id: 0,
name: 'H1-2jVQJz',
timing: moment("2017-11-10T22:46:02.023"),
callback: [Function],
params: [],
type: 'setTimeout',
index: 1 } ]
[ { id: 1,
name: 'BJg-2sVXkG',
timing: moment("2017-11-10T22:46:01.023"),
callback: [Function],
params: [],
type: 'setTimeout',
index: 0,
active: moment("2017-11-10T22:46:00.532"),
ending: moment("2017-11-10T22:46:00.532") } ]
false
[real] 10 ms
[fake] 500 ms
```