Skip to content

Commit

Permalink
use requireActual and requireMock from jest instead of require (#21849)
Browse files Browse the repository at this point in the history
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: #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 dbc864c commit 5e997f9
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'))

This comment has been minimized.

Copy link
@raven-chen

raven-chen Nov 22, 2018

Hi @thymikee

We've occurred the Cannot find module 'setupDevtools' from 'setup.js' in react-native/jest/setup.js after upgrading RN to 0.57.5. I tried the solutions in #19859, but none of it helped.

By digging into the react-native/jest/setup.js. I found if I change

jest.mock('setupDevtools') to jest.mock('../Libraries/Core/Devtools/setupDevtools.js').

This error will be gone.

But another one Cannot find module 'InitializeCore' showed up. Changing this to jest.mock('../Libraries/Core/InitializeCore.js') resolved the error too. then next one, Cannot find module 'Image' appeared. So it looks like something wrong when loading the modules inside ../Libraries.

Do you happen to know how mock find the module without specifying the exact path? Is there a configuration or something like this to config the setup.js to load modules from ../Libraries as a default prefix path?

This comment has been minimized.

Copy link
@thymikee

thymikee Nov 22, 2018

Author Contributor

This is called Haste modules it's not affected by this PR.
There are some issues around that, which you may find helpful: https://github.com/facebook/react-native/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+cannot+find+module+setupDevtools
If you're on Windows, then I think you'll have to wait for a fix

This comment has been minimized.

Copy link
@raven-chen

raven-chen Nov 22, 2018

Thank you! I'm on Mac though.

I found the hasteImpl. Going to find where the jest.mock defined and the logic inside it.

This comment has been minimized.

Copy link
@jcbiznoff

jcbiznoff Dec 3, 2018

hey @raven-chen, I'm actually running into the same problem that you are seeing. Would be great if you can share your solution if you had found one.

This comment has been minimized.

Copy link
@raven-chen

raven-chen Dec 4, 2018

Hi @jcbiznoff

I haven't found a solution. But I tried to build a demo typescript+RN+jest project with the same version we use in the project. It works with no error. So I think this error might be caused by some legacy dependencies. I suspect the legacy dependencies broke the Haste function. still investigating...

.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 5e997f9

Please sign in to comment.