Skip to content

Commit

Permalink
use requireActual and requireMock from jest instead of require (faceb…
Browse files Browse the repository at this point in the history
…ook#21849)

Summary:
A while back Jest introduced `jest.requireActual` and `jest.requireMock` which are aliases to `require.requireActual` and `require.requireMock`. We believe that users should use official Jest API and are planning to deprecate the latter.
Pull Request resolved: facebook#21849

Differential Revision: D10448849

Pulled By: TheSavior

fbshipit-source-id: 34fffde97f48c26098c74ee222a56d99071703a6
  • Loading branch information
thymikee authored and facebook-github-bot committed Oct 18, 2018
1 parent b2769c5 commit 4a4918b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion jest/mockComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

module.exports = (moduleName, instanceMethods) => {
const RealComponent = require.requireActual(moduleName);
const RealComponent = jest.requireActual(moduleName);
const React = require('react');

const SuperClass =
Expand Down
30 changes: 13 additions & 17 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@

'use strict';

const MockNativeMethods = require.requireActual('./MockNativeMethods');
const mockComponent = require.requireActual('./mockComponent');
const MockNativeMethods = jest.requireActual('./MockNativeMethods');
const mockComponent = jest.requireActual('./mockComponent');

require.requireActual('../Libraries/polyfills/babelHelpers.js');
require.requireActual('../Libraries/polyfills/Object.es7.js');
require.requireActual('../Libraries/polyfills/error-guard');
jest.requireActual('../Libraries/polyfills/babelHelpers.js');
jest.requireActual('../Libraries/polyfills/Object.es7.js');
jest.requireActual('../Libraries/polyfills/error-guard');

global.__DEV__ = true;

global.Promise = require.requireActual('promise');
global.regeneratorRuntime = require.requireActual(
'regenerator-runtime/runtime',
);
global.Promise = jest.requireActual('promise');
global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');

global.requestAnimationFrame = function(callback) {
return setTimeout(callback, 0);
Expand All @@ -42,12 +40,12 @@ jest
.mock('TextInput', () => mockComponent('TextInput'))
.mock('Modal', () => mockComponent('Modal'))
.mock('View', () => mockComponent('View', MockNativeMethods))
.mock('RefreshControl', () => require.requireMock('RefreshControlMock'))
.mock('ScrollView', () => require.requireMock('ScrollViewMock'))
.mock('RefreshControl', () => jest.requireMock('RefreshControlMock'))
.mock('ScrollView', () => jest.requireMock('ScrollViewMock'))
.mock('ActivityIndicator', () => mockComponent('ActivityIndicator'))
.mock('ListView', () => require.requireMock('ListViewMock'))
.mock('ListView', () => jest.requireMock('ListViewMock'))
.mock('ListViewDataSource', () => {
const DataSource = require.requireActual('ListViewDataSource');
const DataSource = jest.requireActual('ListViewDataSource');
DataSource.prototype.toJSON = function() {
function ListViewDataSource(dataBlob) {
this.items = 0;
Expand All @@ -68,9 +66,7 @@ jest
return DataSource;
})
.mock('AnimatedImplementation', () => {
const AnimatedImplementation = require.requireActual(
'AnimatedImplementation',
);
const AnimatedImplementation = jest.requireActual('AnimatedImplementation');
const oldCreate = AnimatedImplementation.createAnimatedComponent;
AnimatedImplementation.createAnimatedComponent = function(Component) {
const Wrapped = oldCreate(Component);
Expand All @@ -80,7 +76,7 @@ jest
return AnimatedImplementation;
})
.mock('ReactNative', () => {
const ReactNative = require.requireActual('ReactNative');
const ReactNative = jest.requireActual('ReactNative');
const NativeMethodsMixin =
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.NativeMethodsMixin;
Expand Down
4 changes: 2 additions & 2 deletions local-cli/core/__fixtures__/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @format
*/

const fs = require.requireActual('fs');
const path = require.requireActual('path');
const fs = jest.requireActual('fs');
const path = jest.requireActual('path');

const manifest = fs.readFileSync(
path.join(__dirname, './files/AndroidManifest.xml'),
Expand Down
2 changes: 1 addition & 1 deletion local-cli/core/__fixtures__/dependencies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @format */

const fs = require.requireActual('fs');
const fs = jest.requireActual('fs');
const path = require('path');
const android = require('./android');

Expand Down
2 changes: 1 addition & 1 deletion local-cli/core/__fixtures__/ios.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @format */

const fs = require.requireActual('fs');
const fs = jest.requireActual('fs');
const path = require('path');

exports.valid = {
Expand Down
4 changes: 2 additions & 2 deletions local-cli/link/__tests__/ios/writePlist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ jest.mock('fs');
let plistPath = null;
jest.mock('../../ios/getPlistPath', () => () => plistPath);

const {readFileSync} = require.requireActual('fs');
const {readFileSync} = jest.requireActual('fs');
const fs = require('fs');

const xcode = require('xcode');
const writePlist = require('../../ios/writePlist');

const realPath = require.requireActual('path');
const realPath = jest.requireActual('path');
const projectPath = realPath.join(
__dirname,
'../../__fixtures__/project.pbxproj',
Expand Down

0 comments on commit 4a4918b

Please sign in to comment.