Coder Social home page Coder Social logo

fake-time's Introduction

fake-time

Fake time AND timers in jest. This here thing aims to solve three(!) problems

  • Jest advanceTimersTo does not change the time Date.now gives out.
  • Jest timer mocks either run the timers once or runs the all timers to exhaustion both of which result in funny if your timers set up new timers
  • Jest timer mocks do not run promises from the promise jobs queue which causes setting new timers from promisy code really annoying.

A working example

// yarn jest examples/working.ts
import * as fakeTime from '../index';

describe('fake-time', () => {
  beforeEach(fakeTime.fake);
  afterAll(fakeTime.restore);

  it('advances time', async () => {
    const start = Date.now();
    await fakeTime.advance(1000);
    expect(Date.now() - start).toBeGreaterThan(1000);
  });

  it('runs promises and new timers from timers', async () => {
    const interval = 10;
    let hits = 0;
    async function set() {
      hits++;
      await Promise.resolve();
      setTimeout(set, interval);
    }
    await set();
    await fakeTime.advance(interval * 10);
    expect(hits).toBeGreaterThan(10);
  });

  describe('wait', () => {
    const dayInMs = 24 * 60 * 50 * 1000;

    it('advances time while waiting', async () => {
      const start = Date.now();
      await fakeTime.wait(async () => expect(Date.now() - start).toBeGreaterThan(dayInMs), {
        untilMs: dayInMs * 2 * 10,
      });
    });
  });
});

fake-time's People

Contributors

dependabot[bot] avatar mhalmagiu avatar renovate-bot avatar renovate[bot] avatar sakari avatar sjagoe avatar waltsu avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fake-time's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>smartlyio/renovate-config:security-base)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm tslint Unavailable

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • Update dependency @types/node to v20.14.15

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/nodejs.yml
  • actions/checkout v3
  • actions/setup-node v3
npm
package.json
  • @types/jest 29.5.12
  • @types/node 20.14.14
  • jest 29.7.0
  • jsverify 0.8.4
  • prettier 3.3.3
  • ts-jest 29.2.4
  • ts-node 10.9.2
  • tslint 6.1.3
  • tslint-config-prettier 1.18.0
  • tslint-plugin-prettier 2.3.0
  • typescript 5.5.4
  • jest ^29.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.