Skip to content

Commit

Permalink
Revert changes in RN preprocessor
Browse files Browse the repository at this point in the history
Summary: Changelog: [General][Fixed] Revert changes in Jest preprocessor to fix tests in external projects

Reviewed By: yungsters

Differential Revision: D32250044

fbshipit-source-id: 0ed4c9f7bcfa82349b5c2ec7af2ccda970bbb0ef
  • Loading branch information
rubennorte authored and Luna Wei committed Nov 10, 2021
1 parent f35369e commit 142090a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 93 deletions.
12 changes: 6 additions & 6 deletions Libraries/Components/ScrollView/__tests__/ScrollView-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

'use strict';

import * as React from 'react';
import ScrollView from '../ScrollView';
import * as ReactNativeTestTools from '../../../Utilities/ReactNativeTestTools';
import ReactTestRenderer from 'react-test-renderer';
import View from '../../View/View';
import Text from '../../../Text/Text';
const React = require('react');
const ScrollView = require('../ScrollView');
const ReactNativeTestTools = require('../../../Utilities/ReactNativeTestTools');
const ReactTestRenderer = require('react-test-renderer');
const View = require('../../View/View');
const Text = require('../../../Text/Text');

describe('<ScrollView />', () => {
it('should render as expected', () => {
Expand Down
13 changes: 7 additions & 6 deletions Libraries/Pressability/__tests__/Pressability-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
*/

import type {PressEvent} from '../../Types/CoreEventTypes';
import * as HoverState from '../HoverState';
import Pressability from '../Pressability';
import invariant from 'invariant';
import nullthrows from 'nullthrows';
import Platform from '../../Utilities/Platform';
import UIManager from '../../ReactNative/UIManager';

const HoverState = require('../HoverState');
const Pressability = require('../Pressability').default;
const invariant = require('invariant');
const nullthrows = require('nullthrows');
const Platform = require('../../Utilities/Platform');
const UIManager = require('../../ReactNative/UIManager');

// TODO: Move this util to a shared location.
function getMock<TArguments: $ReadOnlyArray<mixed>, TReturn>(
Expand Down
86 changes: 9 additions & 77 deletions jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,10 @@
'use strict';

const babelRegisterOnly = require('metro-babel-register');
const nullthrows = require('nullthrows');
const createCacheKeyFunction = require('@jest/create-cache-key-function')
.default;
const t = require('@babel/types');
const {statements} = require('@babel/template').default;
const createCacheKeyFunction =
require('@jest/create-cache-key-function').default;

const importDefault = '__importDefault__';
const importAll = '__importAll__';

// prelude
const importPrelude = statements(`
function ${importDefault}(moduleId) {
const exports = require(moduleId);
if (exports && exports.__esModule) {
return exports.default;
}
return exports;
};
function ${importAll}(moduleId) {
const exports = require(moduleId);
if (exports && exports.__esModule) {
return exports;
}
return Object.assign({}, exports, {default: exports});
};
`);

const {
transformSync: babelTransformSync,
transformFromAstSync: babelTransformFromAstSync,
} = require('@babel/core');
const {transformSync: babelTransformSync} = require('@babel/core');
const generate = require('@babel/generator').default;

const nodeFiles = new RegExp(
Expand All @@ -73,13 +41,13 @@ module.exports = {
}).code;
}

let {ast} = transformer.transform({
const {ast} = transformer.transform({
filename: file,
options: {
ast: true, // needed for open source (?) https://github.com/facebook/react-native/commit/f8d6b97140cffe8d18b2558f94570c8d1b410d5c#r28647044
dev: true,
enableBabelRuntime: false,
experimentalImportSupport: true,
experimentalImportSupport: false,
globalPrefix: '',
hot: false,
inlineRequires: true,
Expand Down Expand Up @@ -111,6 +79,10 @@ module.exports = {
[require('@babel/plugin-transform-regenerator')],
[require('@babel/plugin-transform-sticky-regex')],
[require('@babel/plugin-transform-unicode-regex')],
[
require('@babel/plugin-transform-modules-commonjs'),
{strict: false, allowTopLevelThis: true},
],
[require('@babel/plugin-transform-classes')],
[require('@babel/plugin-transform-arrow-functions')],
[require('@babel/plugin-transform-spread')],
Expand All @@ -127,46 +99,6 @@ module.exports = {
],
});

// We're not using @babel/plugin-transform-modules-commonjs so
// we need to add 'use strict' manually
const directives = ast.program.directives;

if (
ast.program.sourceType === 'module' &&
(directives == null ||
directives.findIndex(d => d.value.value === 'use strict') === -1)
) {
ast.program.directives = [
...(directives || []),
t.directive(t.directiveLiteral('use strict')),
];
}

// Postprocess the transformed module to handle ESM and inline requires.
// We need to do this in a separate pass to avoid issues tracking references.
const babelTransformResult = babelTransformFromAstSync(ast, src, {
ast: true,
retainLines: true,
plugins: [
[
require('metro-transform-plugins').importExportPlugin,
{importDefault, importAll},
],
[
require('babel-preset-fbjs/plugins/inline-requires.js'),
{inlineableCalls: [importDefault, importAll]},
],
],
sourceType: 'module',
});

ast = nullthrows(babelTransformResult.ast);

// Inject import helpers *after* running the inline-requires transform,
// because otherwise it will assume they are user code and bail out of
// inlining calls to them.
ast.program.body.unshift(...importPrelude());

return generate(
ast,
// $FlowFixMe[prop-missing] Error found when improving flow typing for libs
Expand Down
5 changes: 1 addition & 4 deletions repo-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
"dependencies": {
"@babel/core": "^7.14.0",
"@babel/generator": "^7.14.0",
"@babel/template": "^7.0.0",
"@babel/types": "^7.0.0",
"@react-native-community/eslint-plugin": "*",
"@reactions/component": "^2.0.2",
"async": "^2.4.0",
"babel-eslint": "^10.1.0",
"babel-preset-fbjs": "^3.4.0",
"clang-format": "^1.2.4",
"connect": "^3.6.5",
"coveralls": "^3.0.2",
Expand All @@ -40,7 +37,7 @@
"jest": "^26.6.3",
"jest-junit": "^10.0.0",
"jscodeshift": "^0.11.0",
"metro-transform-plugins": "^0.66.0",
"metro-babel-register": "0.66.2",
"mkdirp": "^0.5.1",
"prettier": "1.19.1",
"react": "17.0.2",
Expand Down

0 comments on commit 142090a

Please sign in to comment.