Skip to content

Commit

Permalink
Migrate Jest setup scripts from Haste to path-based requires (#24753)
Browse files Browse the repository at this point in the history
Summary:
This is another step in moving RN towards standard path-based requires. All the requires in Jest's setup script have been rewritten to use relative requires. This commit uses relative requires instead of `react-native/...` so that if Facebook were to stop syncing out certain folders and therefore remove code from the react-native package, internal code at Facebook would not need to change.

[General] [Changed] - Migrate Jest setup scripts from Haste to path-based requires
Pull Request resolved: #24753

Differential Revision: D15258238

Pulled By: cpojer

fbshipit-source-id: aa05dc8ea2e4ba195226e8ec7ba6482b7de03240
  • Loading branch information
ide authored and facebook-github-bot committed May 8, 2019
1 parent 09f17a4 commit 59749f5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion jest/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

'use strict';

const React = require('React');
const React = require('react');

const TestRenderer = require('react-test-renderer');
const ShallowRenderer = require('react-test-renderer/shallow');
Expand Down
70 changes: 52 additions & 18 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,50 @@ global.cancelAnimationFrame = function(id) {
clearTimeout(id);
};

jest.mock('setupDevtools');
jest.mock('../Libraries/Core/Devtools/setupDevtools');

// there's a __mock__ for it.
jest.setMock('ErrorUtils', require('ErrorUtils'));
jest.setMock(
'../Libraries/vendor/core/ErrorUtils',
require('../Libraries/vendor/core/ErrorUtils'),
);

jest
.mock('InitializeCore', () => {})
.mock('Image', () => mockComponent('Image'))
.mock('Text', () => mockComponent('Text', MockNativeMethods))
.mock('TextInput', () => mockComponent('TextInput'))
.mock('Modal', () => mockComponent('Modal'))
.mock('View', () => mockComponent('View', MockNativeMethods))
.mock('RefreshControl', () => jest.requireMock('RefreshControlMock'))
.mock('ScrollView', () => jest.requireMock('ScrollViewMock'))
.mock('ActivityIndicator', () => mockComponent('ActivityIndicator'))
.mock('AnimatedImplementation', () => {
const AnimatedImplementation = jest.requireActual('AnimatedImplementation');
.mock('../Libraries/Core/InitializeCore', () => {})
.mock('../Libraries/Image/Image', () =>
mockComponent('../Libraries/Image/Image'),
)
.mock('../Libraries/Text/Text', () =>
mockComponent('../Libraries/Text/Text', MockNativeMethods),
)
.mock('../Libraries/Components/TextInput/TextInput', () =>
mockComponent('../Libraries/Components/TextInput/TextInput'),
)
.mock('../Libraries/Modal/Modal', () =>
mockComponent('../Libraries/Modal/Modal'),
)
.mock('../Libraries/Components/View/View', () =>
mockComponent('../Libraries/Components/View/View', MockNativeMethods),
)
.mock('../Libraries/Components/RefreshControl/RefreshControl', () =>
jest.requireActual(
'../Libraries/Components/RefreshControl/__mocks__/RefreshControlMock',
),
)
.mock('../Libraries/Components/ScrollView/ScrollView', () =>
jest.requireActual(
'../Libraries/Components/ScrollView/__mocks__/ScrollViewMock',
),
)
.mock('../Libraries/Components/ActivityIndicator/ActivityIndicator', () =>
mockComponent(
'../Libraries/Components/ActivityIndicator/ActivityIndicator',
),
)
.mock('../Libraries/Animated/src/AnimatedImplementation', () => {
const AnimatedImplementation = jest.requireActual(
'../Libraries/Animated/src/AnimatedImplementation',
);
const oldCreate = AnimatedImplementation.createAnimatedComponent;
AnimatedImplementation.createAnimatedComponent = function(
Component,
Expand All @@ -55,8 +82,10 @@ jest
};
return AnimatedImplementation;
})
.mock('ReactNative', () => {
const ReactNative = jest.requireActual('ReactNative');
.mock('../Libraries/Renderer/shims/ReactNative', () => {
const ReactNative = jest.requireActual(
'../Libraries/Renderer/shims/ReactNative',
);
const NativeMethodsMixin =
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.NativeMethodsMixin;
Expand All @@ -66,7 +95,9 @@ jest

return ReactNative;
})
.mock('ensureComponentIsNative', () => () => true);
.mock('../Libraries/Components/Touchable/ensureComponentIsNative', () => () =>
true,
);

const mockNativeModules = {
AccessibilityInfo: {
Expand Down Expand Up @@ -314,9 +345,12 @@ Object.keys(mockNativeModules).forEach(module => {
}
});

jest.doMock('NativeModules', () => mockNativeModules);
jest.doMock(
'../Libraries/BatchedBridge/NativeModules',
() => mockNativeModules,
);

jest.doMock('requireNativeComponent', () => {
jest.doMock('../Libraries/ReactNative/requireNativeComponent', () => {
const React = require('react');

return viewName =>
Expand Down

0 comments on commit 59749f5

Please sign in to comment.