Skip to content

Commit

Permalink
use a feature flag for the missing mock scheduler warning
Browse files Browse the repository at this point in the history
I also tried writing a test for it, but couldn't get the scheduler to unmock. included the failing test.
  • Loading branch information
Sunil Pai committed May 11, 2019
1 parent 9f2d141 commit 6920e9f
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 37 deletions.
23 changes: 23 additions & 0 deletions packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let act;
let container;
let isConcurrentMode;
let concurrentRoot = null;
let ReactFeatureFlags;

function render(el, dom) {
if (isConcurrentMode) {
Expand Down Expand Up @@ -54,6 +55,7 @@ describe('ReactTestUtils.act()', () => {
ReactDOM = require('react-dom');
ReactTestUtils = require('react-dom/test-utils');
SchedulerTracing = require('scheduler/tracing');
ReactFeatureFlags = require('shared/ReactFeatureFlags');
act = ReactTestUtils.act;
container = document.createElement('div');
document.body.appendChild(container);
Expand All @@ -63,6 +65,27 @@ describe('ReactTestUtils.act()', () => {
document.body.removeChild(container);
});

describe('mocked scheduler', () => {
beforeEach(() => {
ReactFeatureFlags.warnAboutMissingMockScheduler = true;
// todo - this doesn't actually work
// jest.unmock('scheduler');
// jest.unmock('scheduler/src/SchedulerHostConfig');
// neither does this. am investigating.
// jest.mock('scheduler', () => {
// return require.requireActual('scheduler');
// });
// jest.mock('scheduler/src/SchedulerHostConfig', () => {
// return require.requireActual('scheduler/src/forks/SchedulerHostConfig.default');
// });
});
it("should warn when the scheduler isn't mocked", () => {
expect(() => act(() => {})).toWarnDev([
'Starting from React v17, the "scheduler" module will need to be mocked',
]);
});
});

[false, true].forEach(concurrent => {
isConcurrentMode = concurrent;
describe(isConcurrentMode ? 'concurrent mode' : 'heritage mode', () => {
Expand Down
26 changes: 14 additions & 12 deletions packages/react-dom/src/test-utils/ReactTestUtilsAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {Thenable} from 'react-reconciler/src/ReactFiberScheduler';
import warningWithoutStack from 'shared/warningWithoutStack';
import ReactDOM from 'react-dom';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {warnAboutMissingMockScheduler} from 'shared/ReactFeatureFlags';
import enqueueTask from 'shared/enqueueTask';
import * as Scheduler from 'scheduler';

Expand Down Expand Up @@ -45,19 +46,20 @@ let hasWarnedAboutMissingMockScheduler = false;
const flushWork =
Scheduler.unstable_flushWithoutYielding ||
function() {
if (hasWarnedAboutMissingMockScheduler === false) {
warningWithoutStack(
null,
'Starting from React v17, the "scheduler" module will need to be mocked ' +
'to guarantee consistent behaviour across tests and browsers. To fix this, add the following ' +
"to the top of your tests, or in your framework's global config file -\n\n" +
'As an example, for jest - \n' +
"jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));\n\n" +
'For more info, visit https://fb.me/react-mock-scheduler',
);
hasWarnedAboutMissingMockScheduler = true;
if (warnAboutMissingMockScheduler === true) {
if (hasWarnedAboutMissingMockScheduler === false) {
warningWithoutStack(
null,
'Starting from React v17, the "scheduler" module will need to be mocked ' +
'to guarantee consistent behaviour across tests and browsers. To fix this, add the following ' +
"to the top of your tests, or in your framework's global config file -\n\n" +
'As an example, for jest - \n' +
"jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));\n\n" +
'For more info, visit https://fb.me/react-mock-scheduler',
);
hasWarnedAboutMissingMockScheduler = true;
}
}

while (flushPassiveEffects()) {}
};

Expand Down
30 changes: 17 additions & 13 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import warning from 'shared/warning';
import enqueueTask from 'shared/enqueueTask';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import warningWithoutStack from 'shared/warningWithoutStack';
import {enableEventAPI} from 'shared/ReactFeatureFlags';
import {
warnAboutMissingMockScheduler,
enableEventAPI,
} from 'shared/ReactFeatureFlags';

type EventTargetChildElement = {
type: string,
Expand Down Expand Up @@ -654,19 +657,20 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
const flushWork =
Scheduler.unstable_flushWithoutYielding ||
function() {
if (hasWarnedAboutMissingMockScheduler === false) {
warningWithoutStack(
null,
'Starting from React v17, the "scheduler" module will need to be mocked ' +
'to guarantee consistent behaviour across tests and browsers. To fix this, add the following ' +
"to the top of your tests, or in your framework's global config file -\n\n" +
'As an example, for jest - \n' +
"jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));\n\n" +
'For more info, visit https://fb.me/react-mock-scheduler',
);
hasWarnedAboutMissingMockScheduler = true;
if (warnAboutMissingMockScheduler === true) {
if (hasWarnedAboutMissingMockScheduler === false) {
warningWithoutStack(
null,
'Starting from React v17, the "scheduler" module will need to be mocked ' +
'to guarantee consistent behaviour across tests and browsers. To fix this, add the following ' +
"to the top of your tests, or in your framework's global config file -\n\n" +
'As an example, for jest - \n' +
"jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));\n\n" +
'For more info, visit https://fb.me/react-mock-scheduler',
);
hasWarnedAboutMissingMockScheduler = true;
}
}

while (flushPassiveEffects()) {}
};

Expand Down
26 changes: 14 additions & 12 deletions packages/react-test-renderer/src/ReactTestRendererAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'react-reconciler/inline.test';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import warningWithoutStack from 'shared/warningWithoutStack';
import {warnAboutMissingMockScheduler} from 'shared/ReactFeatureFlags';
import enqueueTask from 'shared/enqueueTask';
import * as Scheduler from 'scheduler';

Expand All @@ -26,19 +27,20 @@ let hasWarnedAboutMissingMockScheduler = false;
const flushWork =
Scheduler.unstable_flushWithoutYielding ||
function() {
if (hasWarnedAboutMissingMockScheduler === false) {
warningWithoutStack(
null,
'Starting from React v17, the "scheduler" module will need to be mocked ' +
'to guarantee consistent behaviour across tests and browsers. To fix this, add the following ' +
"to the top of your tests, or in your framework's global config file -\n\n" +
'As an example, for jest - \n' +
"jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));\n\n" +
'For more info, visit https://fb.me/react-mock-scheduler',
);
hasWarnedAboutMissingMockScheduler = true;
if (warnAboutMissingMockScheduler === true) {
if (hasWarnedAboutMissingMockScheduler === false) {
warningWithoutStack(
null,
'Starting from React v17, the "scheduler" module will need to be mocked ' +
'to guarantee consistent behaviour across tests and browsers. To fix this, add the following ' +
"to the top of your tests, or in your framework's global config file -\n\n" +
'As an example, for jest - \n' +
"jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));\n\n" +
'For more info, visit https://fb.me/react-mock-scheduler',
);
hasWarnedAboutMissingMockScheduler = true;
}
}

while (flushPassiveEffects()) {}
};

Expand Down
4 changes: 4 additions & 0 deletions packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ export const enableEventAPI = false;

// New API for JSX transforms to target - https://github.com/reactjs/rfcs/pull/107
export const enableJSXTransformAPI = false;

// We will enforce mocking scheduler with scheduler/unstable_mock at some point. (v17?)
// Till then, we warn about the missing mock, but still fallback to a sync mode compatible version
export const warnAboutMissingMockScheduler = false;
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.native-fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const warnAboutDeprecatedLifecycles = true;
export const warnAboutDeprecatedSetNativeProps = true;
export const enableEventAPI = false;
export const enableJSXTransformAPI = false;
export const warnAboutMissingMockScheduler = true;

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.native-oss.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const enableSchedulerDebugging = false;
export const warnAboutDeprecatedSetNativeProps = false;
export const enableEventAPI = false;
export const enableJSXTransformAPI = false;
export const warnAboutMissingMockScheduler = false;

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.persistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const enableSchedulerDebugging = false;
export const warnAboutDeprecatedSetNativeProps = false;
export const enableEventAPI = false;
export const enableJSXTransformAPI = false;
export const warnAboutMissingMockScheduler = true;

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.test-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const enableSchedulerDebugging = false;
export const warnAboutDeprecatedSetNativeProps = false;
export const enableEventAPI = false;
export const enableJSXTransformAPI = false;
export const warnAboutMissingMockScheduler = false;

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const disableJavaScriptURLs = false;
export const disableYielding = false;
export const enableEventAPI = true;
export const enableJSXTransformAPI = true;
export const warnAboutMissingMockScheduler = true;

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/forks/ReactFeatureFlags.www.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export const enableEventAPI = true;

export const enableJSXTransformAPI = true;

export const warnAboutMissingMockScheduler = true;

// Flow magic to verify the exports of this file match the original version.
// eslint-disable-next-line no-unused-vars
type Check<_X, Y: _X, X: Y = _X> = null;
Expand Down

0 comments on commit 6920e9f

Please sign in to comment.