question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to clear cache in unit tests

See original GitHub issue

I’m writing unit tests for a selector that depend on external state via an HTTP request.

I’ve mocked the HTTP request and response, but noticed that when test N+1 uses the same selector, as test N, the selector does not get re-evaluated for test N+1, but returns the value cached for test N.

Here’s an example:

import { atom, selector, snapshot_UNSTABLE } from "recoil";

const numberState = atom({ key: "Number", default: 1 });

const callApiToGetMultiplier = jest.fn();

const multipliedState = selector({
  key: "MultipliedNumber",
  get: ({ get }) => get(numberState) * callApiToGetMultiplier()
});

beforeEach(() => {
  callApiToGetMultiplier.mockClear();
});

test("First test", () => {
  callApiToGetMultiplier.mockReturnValueOnce(100);
  const initialSnapshot = snapshot_UNSTABLE();
  expect(initialSnapshot.getLoadable(multipliedState).valueOrThrow()).toBe(100);
  expect(callApiToGetMultiplier).toBeCalledTimes(1);
});

test("Second test", () => {
  callApiToGetMultiplier.mockReturnValueOnce(200);
  const initialSnapshot = snapshot_UNSTABLE();

  // FAILS because value is still 100
  expect(initialSnapshot.getLoadable(multipliedState).valueOrThrow()).toBe(200);

  // FAILS because callApiToGetMultiplier is not called (again)
  // expect(callApiToGetMultiplier).toBeCalledTimes(1);
});

And here it is on CodeSandbox: https://codesandbox.io/s/optimistic-hooks-3714i?file=/src/repro.test.js:0-938

At https://recoiljs.org/docs/guides/testing#example-jest-unit-testing-selectors it says:

You can build a fresh snapshot using snapshot_UNSTABLE() and then use that Snapshot to evaluate selectors for testing.

As the failing test shows, the returned snapshot is not entirely fresh, which is problematic for isolating unit tests.

I’m aware of https://github.com/facebookexperimental/Recoil/issues/972#issuecomment-962996375, but the “solution” there, as well as https://github.com/facebookexperimental/Recoil/pull/1413 both rely on hooks, which are not available in this context.

As far as I know there’s no non-hook API to do a similar refresh, but I’d argue that this shouldn’t be needed anyway, unless the above documentation is incorrect and snapshot_UNSTABLE() is not supposed to return a fresh snapshot.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drarmstrcommented, Dec 13, 2021

@FokkeZB - I was working on #1498 for another use case. It might be a little bit awkward, but it does allow users to refresh selector caches from snapshots without hooks.

0reactions
FokkeZBcommented, Feb 18, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I clear the [Ignore] cache for test methods? (without ...
Under 'Test Settings', 'Unit Test' a had a folder referred in 'Root folder for the assemblies to be loaded:' and the checkbox 'Use...
Read more >
Best practices for unit testing methods that use cache heavily?
If you want true Unit Tests, then you have to mock the cache: write a mock object that implements the same interface as...
Read more >
Add 'Clear Test Explorer Cache' button to Test Explorer to fix ...
There should be a way to clear this cache from within Visual Studio. ... Very obviously will not rerun a unit test to...
Read more >
Unit test reports - GitLab Docs
A unit test report can appear to be empty when viewed in a merge request if the artifact that contained the report expires....
Read more >
Getting started: .NET Core with command line > xUnit.net
If you're having problems discovering or running tests, you may be a victim of a corrupted runner cache inside Visual Studio. To clear...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found