* Restores real time temporarily until callback returns and resolves. * * @throws {TimeError} If time is not faked * * @example Usage * ```ts * import { FakeTime } from "@std/testing/time"; * import { assertEquals, assertNotEquals } from "@std/assert" * * const setTimeout
(
// deno-lint-ignore no-explicit-any
callback: (...args: any[]) => Promise<T> | T,
// deno-lint-ignore no-explicit-any
...args: any[]
)
| 430 | * @returns The returned value from the callback |
| 431 | */ |
| 432 | static restoreFor<T>( |
| 433 | // deno-lint-ignore no-explicit-any |
| 434 | callback: (...args: any[]) => Promise<T> | T, |
| 435 | // deno-lint-ignore no-explicit-any |
| 436 | ...args: any[] |
| 437 | ): Promise<T> { |
| 438 | if (!time) { |
| 439 | return Promise.reject( |
| 440 | new TimeError("Cannot restore time: time is not faked"), |
| 441 | ); |
| 442 | } |
| 443 | restoreGlobals(); |
| 444 | try { |
| 445 | const result = callback.apply(null, args); |
| 446 | if (result instanceof Promise) { |
| 447 | return result.finally(() => overrideGlobals()); |
| 448 | } else { |
| 449 | overrideGlobals(); |
| 450 | return Promise.resolve(result); |
| 451 | } |
| 452 | } catch (e) { |
| 453 | overrideGlobals(); |
| 454 | return Promise.reject(e); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * The number of milliseconds elapsed since the epoch (January 1, 1970 00:00:00 UTC) for the fake time. |
no test coverage detected