diff --git a/bin/test.ts b/bin/test.ts index 92f9d99..aa3c898 100644 --- a/bin/test.ts +++ b/bin/test.ts @@ -11,6 +11,7 @@ import { Runner } from '#src' await Runner.setTsEnv() .addAssertPlugin() + .addExpectTypeOfPlugin() .addPath('tests/unit/**/*Test.ts') .setCliArgs(process.argv.slice(2)) .setGlobalTimeout(10000) diff --git a/package-lock.json b/package-lock.json index e4ea5a9..ce264de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/test", - "version": "5.5.0", + "version": "5.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/test", - "version": "5.5.0", + "version": "5.6.0", "license": "MIT", "dependencies": { "@japa/assert": "^4.1.1", diff --git a/package.json b/package.json index b58d87e..4fd47f1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/test", - "version": "5.5.0", + "version": "5.6.0", "description": "The Athenna test runner. Built on top of Japa.", "license": "MIT", "author": "João Lenon ", diff --git a/src/mocks/Mock.ts b/src/mocks/Mock.ts index 14ea565..0e39a05 100644 --- a/src/mocks/Mock.ts +++ b/src/mocks/Mock.ts @@ -17,6 +17,7 @@ import type { } from '#src' import { createSandbox } from 'sinon' import { MockBuilder } from '#src/mocks/MockBuilder' +import type { FakeTimerInstallOpts } from '#src/types/FakeTimerInstallOpts' export class Mock { /** @@ -32,6 +33,13 @@ export class Mock { return new MockBuilder(Mock.sandbox, object, method) } + /** + * Create a fake timer to mock dates in your application. + */ + public static useFakeTimers(config?: number | Date | FakeTimerInstallOpts) { + return Mock.sandbox.useFakeTimers(config) + } + /** * Create a spy function for a given object. */ diff --git a/src/types/FakeMethod.ts b/src/types/FakeMethod.ts new file mode 100644 index 0000000..4037f08 --- /dev/null +++ b/src/types/FakeMethod.ts @@ -0,0 +1,28 @@ +/** + * @athenna/test + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Names of clock methods that may be faked by install. + */ +export type FakeMethod = + | 'setTimeout' + | 'clearTimeout' + | 'setImmediate' + | 'clearImmediate' + | 'setInterval' + | 'clearInterval' + | 'Date' + | 'nextTick' + | 'hrtime' + | 'requestAnimationFrame' + | 'cancelAnimationFrame' + | 'requestIdleCallback' + | 'cancelIdleCallback' + | 'performance' + | 'queueMicrotask' diff --git a/src/types/FakeTimerInstallOpts.ts b/src/types/FakeTimerInstallOpts.ts new file mode 100644 index 0000000..8279e5b --- /dev/null +++ b/src/types/FakeTimerInstallOpts.ts @@ -0,0 +1,46 @@ +/** + * @athenna/test + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import type { FakeMethod } from '#src/types' + +export interface FakeTimerInstallOpts { + /** + * Installs fake timers with the specified unix epoch (default: 0) + */ + now?: number | Date | undefined + + /** + * An array with names of global methods and APIs to fake. By default, `@sinonjs/fake-timers` does not replace `nextTick()` and `queueMicrotask()`. + * For instance, `FakeTimers.install({ toFake: ['setTimeout', 'nextTick'] })` will fake only `setTimeout()` and `nextTick()` + */ + toFake?: FakeMethod[] | undefined + + /** + * The maximum number of timers that will be run when calling runAll() (default: 1000) + */ + loopLimit?: number | undefined + + /** + * Tells @sinonjs/fake-timers to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by + * 20ms for every 20ms change in the real system time) (default: false) + */ + shouldAdvanceTime?: boolean | undefined + + /** + * Relevant only when using with shouldAdvanceTime: true. increment mocked time by advanceTimeDelta ms every advanceTimeDelta ms change + * in the real system time (default: 20) + */ + advanceTimeDelta?: number | undefined + + /** + * Tells FakeTimers to clear 'native' (i.e. not fake) timers by delegating to their respective handlers. These are not cleared by + * default, leading to potentially unexpected behavior if timers existed prior to installing FakeTimers. (default: false) + */ + shouldClearNativeTimers?: boolean | undefined +} diff --git a/src/types/index.ts b/src/types/index.ts index fabc22a..204feda 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -12,6 +12,7 @@ export type { Config, PluginFn } from '@japa/runner' export * from '#src/types/Spy' export * from '#src/types/Match' export * from '#src/types/SpyMethod' +export * from '#src/types/FakeMethod' export * from '#src/types/SpyInstance' export * from '#src/types/Stub' export * from '#src/types/StubMethod'