Skip to content

Commit

Permalink
[@sinonjs/fake-timers] Add definitions (#3727)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalduez committed Feb 11, 2020
1 parent 5175c53 commit d340a3d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
declare module '@sinonjs/fake-timers' {
declare opaque type ImmediateID;

declare type FakeMethod =
| 'setTimeout'
| 'clearTimeout'
| 'setImmediate'
| 'clearImmediate'
| 'setInterval'
| 'clearInterval'
| 'Date'
| 'nextTick'
| 'hrtime'
| 'requestAnimationFrame'
| 'cancelAnimationFrame'
| 'requestIdleCallback'
| 'cancelIdleCallback';

declare type InstallConfig = {|
target?: { ... },
now?: number | Date,
toFake?: FakeMethod[],
loopLimit?: number,
shouldAdvanceTime?: boolean,
advanceTimeDelta?: number,
|};

declare type Timers = {|
setTimeout: typeof setTimeout,
clearTimeout: typeof clearTimeout,
setInterval: typeof setInterval,
clearInterval: typeof clearInterval,
setImmediate: typeof setImmediate,
clearImmediate: typeof clearImmediate,
Date: typeof Date,
|};

declare type Clock = {|
...Timers,
requestAnimationFrame: typeof requestAnimationFrame,
cancelAnimationFrame: typeof cancelAnimationFrame,
requestIdleCallback: typeof requestIdleCallback,
cancelIdleCallback: typeof cancelIdleCallback,
now: number,
timeouts: { ... },
loopLimit: number,
countTimers(): number,
hrtime: typeof process.hrtime,
nextTick: typeof process.nextTick,
performance?: { now: typeof performance.now, ... },
tick(time: number | string): void,
tickAsync(time: number | string): Promise<void>,
next(): void,
nextAsync(): Promise<void>,
reset(): void,
runAll(): void,
runAllAsync(): Promise<void>,
runMicrotasks(): void,
runToFrame(): void,
runToLast(): void,
runToLastAsync(): Promise<void>,
setSystemTime(now?: number | Date): void,
uninstall(): void,
Performance: typeof Performance,
|};

declare type FakeTimers = {
timers: Timers,
createClock(now?: number | Date, loopLimit?: number): Clock,
install(config?: InstallConfig): Clock,
withGlobal(global: { ... }): FakeTimers,
...
};

declare module.exports: FakeTimers;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @flow
import { describe, it } from 'flow-typed-test';
import FakeTimers from '@sinonjs/fake-timers';

describe('@sinonjs/fake-timers', () => {
it('install', () => {
FakeTimers.install({
target: global,
now: 150,
loopLimit: 250,
});

// $ExpectError
FakeTimers.install(true);
});

it('clock', () => {
const clock = FakeTimers.install();

clock.next();
clock.tick(150);
clock.runAll();
const timeoutId = clock.setTimeout(() => {});
const intervalId = clock.setInterval(() => {});
clock.clearTimeout(timeoutId);

// $ExpectError
clock.clearTimeout(intervalId);
clock.clearInterval(intervalId);
clock.runToFrame();
});
});

0 comments on commit d340a3d

Please sign in to comment.