Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notes on mocking the scheduler #2171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions content/docs/mock-scheduler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
id: mock-scheduler
title: Mocking the scheduler
permalink: docs/mock-scheduler.html
---

Mocking the scheduler for tests can guarantee consistent behaviour across modes.

React uses the `scheduler` module to sequence how and when 'work' get queued and executed in time. While this behaviour varies across modes, we still want to write tests that are decoupled from the mode they are running in. To make tests run consistently across modes, we can use [module mocking](/docs/testing-recipes.html#mock-modules) to mock `scheduler` with a test friendly version `scheduler/unstable_mock`. Combined with `act()`, you should be able to write tests that run consistently across modes.

### Setup

For jest, we can setup the mocked scheduler by adding this line before any other imports in a test suite, or adding it to a [global configuration file](https://jestjs.io/docs/en/cli#config-path).

```jsx
jest.mock("scheduler", () => require("scheduler/unstable_mock"));
```


* In environments that don't support module mocks, we could still (with some effort) setup the builds for tests such that the scheduler is replaced with the mock version.
* While not mocking the scheduler means that we can't guarantee the order and timing of updates to the rendering surface, this might not be a problem for certain classes of tests like end-to-end tests, etc.