diff --git a/jest-preset.js b/jest-preset.js index 3de412bebd571e..5a3051ff384b2f 100644 --- a/jest-preset.js +++ b/jest-preset.js @@ -9,19 +9,11 @@ 'use strict'; -const dir = __dirname; - module.exports = { haste: { defaultPlatform: 'ios', platforms: ['android', 'ios', 'native'], - hasteImplModulePath: require.resolve('./jest/hasteImpl.js'), - providesModuleNodeModules: ['react-native'], - }, - moduleNameMapper: { - '^React$': require.resolve('react'), }, - modulePathIgnorePatterns: [`${dir}/Libraries/react-native/`], transform: { '^.+\\.(js|ts|tsx)$': 'babel-jest', '^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$': require.resolve( diff --git a/jest.config.js b/jest.config.js index 3ed6cd49c14fb6..a157f236941cf4 100644 --- a/jest.config.js +++ b/jest.config.js @@ -17,9 +17,6 @@ module.exports = { './jest/setup.js', ], 'timers': 'fake', - 'moduleNameMapper': { - '^React$': '/Libraries/react-native/React.js', - }, 'testRegex': '/__tests__/.*-test\\.js$', 'testPathIgnorePatterns': [ '/node_modules/', @@ -29,46 +26,11 @@ module.exports = { ], 'haste': { 'defaultPlatform': 'ios', - 'hasteImplModulePath': '/jest/hasteImpl.js', - 'providesModuleNodeModules': [ - 'react-native', - ], 'platforms': [ 'ios', 'android', ], }, - 'modulePathIgnorePatterns': [ - '/node_modules/(?!react|fbjs|react-native|react-transform-hmr|core-js|promise)/', - 'node_modules/react/node_modules/fbjs/', - 'node_modules/react/lib/ReactDOM.js', - 'node_modules/fbjs/lib/Map.js', - 'node_modules/fbjs/lib/Promise.js', - 'node_modules/fbjs/lib/fetch.js', - 'node_modules/fbjs/lib/ErrorUtils.js', - 'node_modules/fbjs/lib/URI.js', - 'node_modules/fbjs/lib/Deferred.js', - 'node_modules/fbjs/lib/PromiseMap.js', - 'node_modules/fbjs/lib/UserAgent.js', - 'node_modules/fbjs/lib/areEqual.js', - 'node_modules/fbjs/lib/base62.js', - 'node_modules/fbjs/lib/crc32.js', - 'node_modules/fbjs/lib/everyObject.js', - 'node_modules/fbjs/lib/fetchWithRetries.js', - 'node_modules/fbjs/lib/filterObject.js', - 'node_modules/fbjs/lib/flattenArray.js', - 'node_modules/fbjs/lib/forEachObject.js', - 'node_modules/fbjs/lib/isEmpty.js', - 'node_modules/fbjs/lib/removeFromArray.js', - 'node_modules/fbjs/lib/resolveImmediate.js', - 'node_modules/fbjs/lib/someObject.js', - 'node_modules/fbjs/lib/sprintf.js', - 'node_modules/fbjs/lib/xhrSimpleDataSerializer.js', - 'node_modules/jest-cli', - 'node_modules/react/dist', - 'node_modules/fbjs/.*/__mocks__/', - 'node_modules/fbjs/node_modules/', - ], 'unmockedModulePathPatterns': [ 'node_modules/react/', 'Libraries/Renderer', diff --git a/jest/__tests__/hasteImpl-test.js b/jest/__tests__/hasteImpl-test.js deleted file mode 100644 index 9cab29a52215f8..00000000000000 --- a/jest/__tests__/hasteImpl-test.js +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @emails oncall+js_foundation - */ - -'use strict'; - -const path = require('path'); - -const {getHasteName} = require('../hasteImpl'); - -function getPath(...parts) { - return path.join(__dirname, '..', '..', ...parts); -} - -it('returns the correct haste name for a RN library file', () => { - expect( - getHasteName( - getPath( - 'Libraries', - 'Components', - 'AccessibilityInfo', - 'AccessibilityInfo.js', - ), - ), - ).toEqual('AccessibilityInfo'); -}); - -it('returns the correct haste name for a file with a platform suffix', () => { - for (const platform of ['android', 'ios', 'native']) { - expect( - getHasteName( - getPath( - 'Libraries', - 'Components', - 'AccessibilityInfo', - `AccessibilityInfo.${platform}.js`, - ), - ), - ).toEqual('AccessibilityInfo'); - } -}); - -it('returns the correct haste name for a file with a flow suffix', () => { - expect( - getHasteName( - getPath( - 'Libraries', - 'Components', - 'AccessibilityInfo', - 'AccessibilityInfo.ios.js.flow', - ), - ), - ).toEqual('AccessibilityInfo'); -}); - -it('does not calculate the haste name for a file that is not JS', () => { - expect( - getHasteName( - getPath( - 'Libraries', - 'Components', - 'AccessibilityInfo', - 'AccessibilityInfo.txt', - ), - ), - ).toBe(undefined); -}); - -it('does not calculate the haste name for a file outside of RN', () => { - expect( - getHasteName(getPath('..', 'Libraries', 'AccessibilityInfo.txt')), - ).toBe(undefined); -}); - -it('does not calculate the haste name for a blacklisted file', () => { - expect( - getHasteName( - getPath( - 'Libraries', - 'Components', - '__mocks__', - 'AccessibilityInfo', - 'AccessibilityInfo.js', - ), - ), - ).toBe(undefined); -}); diff --git a/jest/hasteImpl.js b/jest/hasteImpl.js deleted file mode 100644 index eae413a937df2f..00000000000000 --- a/jest/hasteImpl.js +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @flow - */ - -'use strict'; - -const path = require('path'); -const cli = require('@react-native-community/cli'); - -// Use duck-typing because of Facebook-internal infra that doesn't have the cli package. -const {haste} = (cli.loadConfig && cli.loadConfig()) || { - haste: { - providesModuleNodeModules: [], - platforms: ['ios', 'android'], - }, -}; - -// Detect out-of-tree platforms and add them to the whitelists -const pluginRoots /*: Array */ = haste.providesModuleNodeModules.map( - name => path.resolve(__dirname, '../../', name) + path.sep, -); - -const pluginNameReducers /*: Array<[RegExp, string]> */ = haste.platforms.map( - name => [new RegExp(`^(.*)\\.(${name})$`), '$1'], -); - -const ROOTS = [path.resolve(__dirname, '..') + path.sep, ...pluginRoots]; - -const BLACKLISTED_PATTERNS /*: Array */ = [ - /.*[\\\/]__(mocks|tests)__[\\\/].*/, - /^Libraries[\\\/]Animated[\\\/]src[\\\/]polyfills[\\\/].*/, - /^Libraries[\\\/]Renderer[\\\/]fb[\\\/].*/, - /DerivedData[\\\/].*/, -]; - -const WHITELISTED_PREFIXES /*: Array */ = [ - 'IntegrationTests', - 'Libraries', - 'ReactAndroid', - 'RNTester', -]; - -const NAME_REDUCERS /*: Array<[RegExp, string]> */ = [ - // extract basename - [/^(?:.*[\\\/])?([a-zA-Z0-9$_.-]+)$/, '$1'], - // strip .js/.js.flow suffix - [/^(.*)\.js(\.flow)?$/, '$1'], - // strip native suffix - [/^(.*)\.(native)$/, '$1'], - // strip plugin platform suffixes - ...pluginNameReducers, -]; - -function isHastePath(filePath /*: string */) /*: boolean */ { - if (!filePath.endsWith('.js') && !filePath.endsWith('.js.flow')) { - return false; - } - - const root = ROOTS.find(r => filePath.startsWith(r)); - if (!root) { - return false; - } - - filePath = filePath.substr(root.length); - if (BLACKLISTED_PATTERNS.some(pattern => pattern.test(filePath))) { - return false; - } - return WHITELISTED_PREFIXES.some(prefix => filePath.startsWith(prefix)); -} - -module.exports = { - /* - * @return {string|void} hasteName for module at filePath; or undefined if - * filePath is not a haste module - */ - getHasteName( - filePath /*: string */, - sourceCode /*: ?string */, - ) /*: string | void */ { - if (!isHastePath(filePath)) { - return undefined; - } - - const hasteName = NAME_REDUCERS.reduce( - (name, [pattern, replacement]) => name.replace(pattern, replacement), - filePath, - ); - - return hasteName; - }, -};