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

Fixed scheduler setTimeout fallback #14358

Merged
merged 3 commits into from
Dec 1, 2018

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented Nov 29, 2018

Resolves #14352 (and facebook/react-native/issues/21967)

  • Restores a working setTimeout based fallback for the scheduler package.
  • Relocates test-specific code into a new NPM package, jest-mock-scheduler.
  • Updates tests.

I tested and verified that these updates to scheduler fix the reported React Native useEffects bug.

I've also published a 0.0.0 release to jest-mock-scheduler to reserve the name.

Moved unit-test-specific setTimeout code into a new NPM package, jest-mock-scheduler.
requestHostCallback = globalImpl[0];
cancelHostCallback = globalImpl[1];
shouldYieldToHost = globalImpl[2];
getCurrentTime = globalImpl[3];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little awkward. I could combine these branches?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's combine.

Copy link
Contributor Author

@bvaughn bvaughn Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, combined in db14ad8

@@ -30,6 +30,9 @@ describe('Scheduler', () => {
jest.useFakeTimers();
jest.resetModules();

const JestMockScheduler = require('jest-mock-scheduler');
JestMockScheduler.mockRestore();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This convention seemed fairly inline with existing Jest mock packages.

@sizebot
Copy link

sizebot commented Nov 30, 2018

Details of bundled changes.

Comparing: 88ada98...2524836

jest-mock-scheduler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
jest-mock-scheduler.development.js n/a n/a 0 B 1.51 KB 0 B 732 B NODE_DEV
jest-mock-scheduler.production.min.js n/a n/a 0 B 680 B 0 B 445 B NODE_PROD
JestMockScheduler-dev.js n/a n/a 0 B 1.48 KB 0 B 711 B FB_WWW_DEV
JestMockScheduler-prod.js n/a n/a 0 B 1.06 KB 0 B 532 B FB_WWW_PROD

Generated by 🚫 dangerJS

@gaearon
Copy link
Collaborator

gaearon commented Nov 30, 2018

Can you tell more about the intended audience of jest-mock-scheduler? Who would need to test the scheduler itself? Or is it about testing code that relies on scheduler? Could that be used to trigger useEffect callbacks from tests?

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 30, 2018

Can you tell more about the intended audience of jest-mock-scheduler?

For the moment– we are the audience (React team and a few Facebook engineers working in www).

It makes it more convenient to test updates at a finer grain (advancing Jest timers a little bit, verify some things, then a little bit more). Seemed like the easiest path forward to replacing the setTimeout branch of scheduler with a functional fallback without breaking existing tests.

@gaearon
Copy link
Collaborator

gaearon commented Nov 30, 2018

I mean more like, do you expect product developers to ever need it? Does it help testing things like passive effects?

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 30, 2018

I'm not sure yet how useful it will end up being. @acdlite has thought about this before. Maybe he has a better intuition.

@gaearon
Copy link
Collaborator

gaearon commented Nov 30, 2018

Can we fix the bug without introducing a new package? My concern is just that we already created a package (jest-react) but it's not clear who or when will use it. This adds one more, and I still don't understand who they're for. It seems like overkill if we add public packages which we only use ourselves. But maybe I'm missing something. If this is meant for public consumption, maybe it would make sense to unify jest-react and jest-mock-scheduler somehow. Or even re-export all of it from react-test-renderer/something.

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 30, 2018

@acdlite suggested a new package initially, so I'd like for him to weigh in here.

The part of this PR I'm most concerned about is fixing the setTimeout fork of scheduler to unbreak useEffect for React Native 😄

"files": [
"LICENSE",
"README.md",
"build-info.json",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build-info is metadata managed and used by our release scripts.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's in it? Sorry if I missed it somewhere. Do we include it in all packages?

Copy link
Contributor Author

@bvaughn bvaughn Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some metadata about this build, e.g.
https://unpkg.com/react@canary/build-info.json

It's useful particularly for the prepare-stable script when it comes to swapping out the shared/ReactVersion value– but also I think it might be generally useful.

{
"name": "jest-mock-scheduler",
"version": "0.1.0",
"description": "Jest matchers and utilities for testing the scheduler package.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this private for now until we're sure we want it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@@ -447,41 +447,44 @@ var requestHostCallback;
var cancelHostCallback;
var shouldYieldToHost;

if (typeof window !== 'undefined' && window._schedMock) {
var globalValue = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you mean globalThis? 😏

moduleType: ISOMORPHIC,
entry: 'jest-mock-scheduler',
global: 'JestMockScheduler',
externals: ['jest-diff'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copypasta

Copy link
Contributor Author

@bvaughn bvaughn Dec 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed this external was required for some reason, since it was listed as a dependency for the jest-react package (even though that package didn't reference it anywhere that I saw).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably copypasta too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well. We can rip it out in of both then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* LICENSE file in the root directory of this source tree.
*/

'use strict';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but I don't think we need to put 'use strict' at the top of these files because they are ES modules, so strict mode is implied. @gaearon?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. That was definitely just copypasta.

@@ -0,0 +1,3 @@
# `jest-mock-scheduler`

Jest matchers and utilities for testing the `scheduler` package.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should add note that this is experimental/unstable like we do with the others

@bvaughn bvaughn merged commit 52bea95 into facebook:master Dec 1, 2018
@bvaughn bvaughn deleted the scheduler-setTimeout-fix branch December 1, 2018 21:03
@ghost
Copy link

ghost commented Dec 20, 2018

Have you read the NPM terms?

A few examples of unacceptable content:
5. Content that exists only to "reserve" a name, whether a package name, user name, or organization name. The Dispute Policy governs how npm handles such cases of "squatting".

jetoneza pushed a commit to jetoneza/react that referenced this pull request Jan 23, 2019
* Fixed scheduler setTimeout fallback
* Moved unit-test-specific setTimeout code into a new NPM package, jest-mock-scheduler.
n8schloss pushed a commit to n8schloss/react that referenced this pull request Jan 31, 2019
* Fixed scheduler setTimeout fallback
* Moved unit-test-specific setTimeout code into a new NPM package, jest-mock-scheduler.
kassens pushed a commit that referenced this pull request Jan 31, 2023
## Summary

Removing package jest-mock-scheduler introduced in PR
#14358, as it is no longer
referenced in the main branch code. The following files previously
referenced it:

- packages/scheduler/src/__tests__/Scheduler-test.js
- packages/scheduler/src/__tests__/SchedulerDOM-test.js
- packages/shared/__tests__/ReactDOMFrameScheduling-test.js
- scripts/jest/setupTests.js
- scripts/rollup/bundles.js

## How did you test this change?

ci green
github-actions bot pushed a commit that referenced this pull request Jan 31, 2023
## Summary

Removing package jest-mock-scheduler introduced in PR
#14358, as it is no longer
referenced in the main branch code. The following files previously
referenced it:

- packages/scheduler/src/__tests__/Scheduler-test.js
- packages/scheduler/src/__tests__/SchedulerDOM-test.js
- packages/shared/__tests__/ReactDOMFrameScheduling-test.js
- scripts/jest/setupTests.js
- scripts/rollup/bundles.js

## How did you test this change?

ci green

DiffTrain build for [d7bb524](d7bb524)
[View git log for this commit](https://github.com/facebook/react/commits/d7bb524ade26513ad310fedab5cad659012efd45)
jerrydev0927 added a commit to jerrydev0927/react that referenced this pull request Jan 5, 2024
## Summary

Removing package jest-mock-scheduler introduced in PR
facebook/react#14358, as it is no longer
referenced in the main branch code. The following files previously
referenced it:

- packages/scheduler/src/__tests__/Scheduler-test.js
- packages/scheduler/src/__tests__/SchedulerDOM-test.js
- packages/shared/__tests__/ReactDOMFrameScheduling-test.js
- scripts/jest/setupTests.js
- scripts/rollup/bundles.js

## How did you test this change?

ci green

DiffTrain build for [d7bb524ade26513ad310fedab5cad659012efd45](facebook/react@d7bb524)
[View git log for this commit](https://github.com/facebook/react/commits/d7bb524ade26513ad310fedab5cad659012efd45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useEffect is broken for React Native with JSC
5 participants