Skip to content

Commit

Permalink
Create shared mock for scheduler (#4313)
Browse files Browse the repository at this point in the history
Summary:
The version of scheduler we get internally is always the latest. However, on GitHub we get the version is a transitive dependency from `react-test-renderer`.

Some methods in the unstable_mock have been renamed between these two versions. This mock file provides a centralized place to reconcile those differences so that the same tests can work both internally and on GitHub.

I also tried manually upgrading `react-test-renderer` to the `experimental` tag in the hopes of getting GitHub to use the same version we have internally, and then transitively inherit the more recent version of `scheduler`. Unfortunately that created many test failures for reasons I don't currently fully understand.

Pull Request resolved: #4313

Reviewed By: voideanvalue

Differential Revision: D45789944

Pulled By: captbaritone

fbshipit-source-id: 1e2a0fb47667cd12bdbe13d9f819015a8d545f13
  • Loading branch information
captbaritone authored and facebook-github-bot committed May 12, 2023
1 parent e784ee4 commit cc1daa1
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ describe('ReactRelayFragmentContainer', () => {
}

beforeEach(() => {
jest.mock('scheduler', () => {
return jest.requireActual('scheduler/unstable_mock');
});
jest.mock('scheduler', () => require('./mockScheduler'));
environment = createMockEnvironment();
UserQuery = graphql`
query ReactRelayFragmentContainerTestUserQuery($id: ID!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

jest.mock('scheduler', () => require('scheduler/unstable_mock'));
jest.mock('scheduler', () => require('./mockScheduler'));

const ReactRelayContext = require('../ReactRelayContext');
const ReactRelayQueryRenderer = require('../ReactRelayQueryRenderer');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ describe('ReactRelayRefetchContainer', () => {
}
}
beforeEach(() => {
jest.mock('scheduler', () => {
return jest.requireActual('scheduler/unstable_mock');
});
jest.mock('scheduler', () => require('./mockScheduler'));
environment = createMockEnvironment();
UserFragment = graphql`
fragment ReactRelayRefetchContainerTestUserFragment on User
Expand Down
36 changes: 36 additions & 0 deletions packages/react-relay/__tests__/mockScheduler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall relay
*/

const invariant = require('invariant');
const Scheduler = require('scheduler/unstable_mock');

// The version of scheduler we get internally is always the latest. However, the
// version we get on GitHub is a transitive dependency from `react-test-renderer`.
//
// Some methods in the unstable_mock have been renamed between these two
// versions. This mock file provides a centralized place to reconcile those
// differences so that the same tests can work both internally and on GitHub.

if (Scheduler.log == null) {
invariant(
Scheduler.unstable_yieldValue != null,
'Expected to find one of log or unstable_yieldValue',
);
Scheduler.log = Scheduler.unstable_yieldValue;
}
if (Scheduler.unstable_clearLog == null) {
invariant(
Scheduler.unstable_clearYields != null,
'Expected to find one of unstable_clearLog or unstable_clearYields',
);
Scheduler.unstable_clearLog = Scheduler.unstable_clearYields;
}
module.exports = Scheduler;
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ describe('useBlockingPaginationFragment with useTransition', () => {
// Set up mocks
jest.resetModules();
jest.mock('warning');
jest.mock('scheduler', () => {
return jest.requireActual('scheduler/unstable_mock');
});
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));

// Supress `act` warnings since we are intentionally not
// using it for most tests here. `act` currently always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ it('disposes the entry point and nullifies the state when the disposeEntryPoint
});

beforeEach(() => {
jest.mock('scheduler', () => require('scheduler/unstable_mock'));
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let renderSpy;
// TODO(T83890478): enable once double invoked effects lands in xplat
describe.skip('useFragmentNode-react-double-effects-test', () => {
beforeEach(() => {
jest.mock('scheduler', () => require('scheduler/unstable_mock'));
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));
jest.mock('warning');
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
renderSpy = jest.fn<$ReadOnlyArray<mixed>, mixed>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ describe.each([
}

beforeEach(() => {
jest.mock('scheduler', () => {
return jest.requireActual('scheduler/unstable_mock');
});
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));
commitSpy = jest.fn<any | [any], mixed>();
renderSpy = jest.fn<[any], mixed>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('when an initial preloaded query is passed', () => {
});

beforeEach(() => {
jest.mock('scheduler', () => require('scheduler/unstable_mock'));
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe.skip('useQueryLoader-react-double-effects', () => {
let loaderRenderLogs: Array<string>;

beforeEach(() => {
jest.mock('scheduler', () => require('scheduler/unstable_mock'));
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));

environment = createMockEnvironment();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ describe('useQueryLoader', () => {
});

beforeEach(() => {
jest.mock('scheduler', () => require('scheduler/unstable_mock'));
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ describe.each([
// Set up mocks
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
jest.mock('warning');
jest.mock('scheduler', () =>
jest.requireActual('scheduler/unstable_mock'),
);
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));
/* $FlowFixMe[underconstrained-implicit-instantiation] error found when
* enabling Flow LTI mode */
commitSpy = jest.fn<_, mixed>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ describe('useRefetchableFragmentNode with useTransition', () => {
// Set up mocks
jest.resetModules();
jest.mock('warning');
jest.mock('scheduler', () => {
return jest.requireActual('scheduler/unstable_mock');
});
jest.mock('scheduler', () => require('../../__tests__/mockScheduler'));

// Supress `act` warnings since we are intentionally not
// using it for most tests here. `act` currently always
Expand Down

0 comments on commit cc1daa1

Please sign in to comment.