Testing stuff using now() leads to tests that are failing for wrong reason

This issue has been created since 2022-07-01.

Multiple clocks with the same interval will automatically be synchronized.

The fact that timers are synchronized, will make multiple unit tests share state between them.

Here is unit test that will reproduce the issue:

Notice how latter test will fail, even though it shouldn't. If you run either of tests individually, the test will pass.

import { now } from "mobx-utils";
import { computed, observe } from "mobx";

describe("now", () => {
  let actual: boolean;

  beforeEach(() => {
    jest.useFakeTimers();

    jest.setSystemTime(new Date("2015-10-21T07:28:00Z"));

    const someComputed = computed(() => {
      const currentTimestamp = now(1000);

      return currentTimestamp > new Date("2015-10-21T07:28:00Z").getTime();
    });

    observe(someComputed, (changed) => {
      actual = changed.newValue as boolean;
    }, true);
  });

  it("given time passes, works", () => {
    jest.advanceTimersByTime(1000);

    expect(actual).toBe(true);
  });

  it("does not share the state from previous test", () => {
    expect(actual).toBe(false);
  });
})

And to be absolutely sure that synchronization is causing the trouble, you can comment out stuff from implementation of now to see that both unit tests will pass at same time when synchronization is disabled.

    function now(interval) {
      if (interval === void 0) { interval = 1000; }
        if (!mobx._isComputingDerivation()) {
            // See #40
            return Date.now();
        }
        // if (!tickers[interval]) {
            if (typeof interval === "number")
                return createIntervalTicker(interval).current(); // Notice return here
            else
                tickers[interval] = createAnimationFrameTicker();
        // }
        // return tickers[interval].current();
    }

I'd like to be able to disable the synchronization in unit tests.

More Details About Repo
Owner Name mobxjs
Repo Name mobx-utils
Full Name mobxjs/mobx-utils
Language TypeScript
Created Date 2016-08-07
Updated Date 2022-09-14
Star Count 1088
Watcher Count 23
Fork Count 113
Issue Count 27

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Add `$` as delimiters 4 2022-07-01 2022-07-24
Neovim crashes when changing tags 12 2022-07-01 2022-07-24
leap.nvim compatibility 16 2022-07-02 2022-07-24
请问应该如何配置RSS?文档里可以增加相关说明吗? 1 2021-08-25 2022-07-17
Update dialog unreadable 1 2020-04-28 2022-09-27
No isosurface found for interpolated bands. 0 2021-04-12 2022-09-25
Daemonic processes are not allowed to have children 0 2021-06-18 2022-04-01
Restart serve when perla.jsonc changes 0 2022-01-13 2022-09-27
Admin Console - Don't Shift to Top of Screen When Year Selection is Made Using Drop-Down 0 2022-01-24 2022-08-24
Please confirm whether this is a bug in “I2C_RequestMemoryRead” 1 2021-10-26 2022-08-27
Custom datasets 6 2021-12-01 2022-01-09
How to push stream using RTMP? 0 2022-06-07 2022-08-17
HIP-336 Fix is_approval persistence 1 2022-03-26 2022-07-22
Spellbook blocked 1 2021-03-18 2022-01-16
How to recover the raw data of the ImDrawCmd.Texture? 0 2021-07-29 2022-09-06
circle: "Undeclared identifier 'is_same_v'" when #include <array> 3 2021-09-24 2022-09-06
Filename timestamps contain invalid characters 2 2021-04-19 2022-09-27
多边形可编辑 如何添加右键删除可编辑点? 1 2021-06-03 2022-07-12
Different two 10bit yuv gives MSE=0 10 2022-01-26 2022-09-17
Add CONTRIBUTOR guideline 0 2021-02-15 2022-06-18
1.0.3 doesn't seem to be published anywhere 1 2021-01-04 2022-09-12
"VALUE" is not a keyword for Oracle 5 2021-12-15 2022-09-06
custom table schema path of {{ this }} parsed in correctly in post-hook macro 2 2021-10-01 2022-08-22
bitbake fails due to git segfault on Rpi-distro 4 2021-12-15 2022-09-28
Delete workflow when user deletes CI pipeline | Rename 'Global variables' to 'System variables' 0 2022-07-05 2022-08-25
[Bug] Here We Godamn Go Again!! 8 2022-08-07 2022-09-07
drush site:install doesn't check for missing file permissions in sites/default/files 1 2021-06-05 2022-09-23
excalidraw preview not displayed (no exceptions logged in browser console nor in the container log) 3 2022-07-26 2022-09-22
[Thread plaatform] - Long Manual code is not printing in the device configuration while launching the all-clusters-app 3 2022-06-02 2022-09-17
Test case to Simulate disconnecting and reconnecting to a Thread network on TH fails with : Error 0x0000002F 2 2022-05-20 2022-09-19
Add 'diffSchema' utility function 2 2021-08-10 2022-09-22
Add support for max_value/min_value in filters for sensors 2 2022-01-07 2022-08-28
Create script for label management 0 2021-05-07 2022-09-24
a lot of 404 for CSS and JS in administrator template 9 2022-01-13 2022-09-28
Conversion of type parameters when creating executable references 10 2022-08-09 2022-09-21
POST on nested data throws 404 8 2021-01-25 2022-09-29
https://cdn.jsdelivr.net域名下的资源加载不出来 2 2022-05-21 2022-09-15
Unable to add multi cluster with IPv6 , secret name appending "[" with metadata.name from the server url 1 2022-01-18 2022-09-25
x/pkgsite: optimize vuln list by reading concurrently 1 2021-11-08 2022-07-16
syscall: linux-ppc64x (incorrectly?) treats R4 as a syscall return value 4 2022-02-14 2022-09-09
Cannot use matched groups in patterns 0 2020-10-14 2022-08-22
Field visible state not implemented 1 2021-07-20 2022-09-27
Percent opperations issue 1 2020-04-14 2022-09-21
[Bug - Downloads] - New theme black background with black text - cant read. 3 2021-10-25 2022-09-23
AttributeError: module 'pycodestyle' has no attribute 'missing_whitespace_after_import_keyword' 3 2022-08-01 2022-09-22
Bubble Transmitter Integration 0 2019-11-07 2022-01-29
Can I use this for detecting segments in an audio file? 1 2017-11-27 2022-09-03
Unable to install specific version of elastic-search 3 2021-01-14 2022-09-27
Seller Experience: Verify Tracks events in the seller intent. 0 2022-01-27 2022-08-28
Type `InputProps` should not require data-* attributes 1 2022-07-28 2022-07-26