Skip to content

Commit 2b32fe6

Browse files
authored
Remove providesModuleNodeModules from Jest. (#9583)
1 parent 73acd08 commit 2b32fe6

File tree

18 files changed

+228
-222
lines changed

18 files changed

+228
-222
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
- `[jest-validate]` [**BREAKING**] Use ESM exports ([#8874](https://github.com/facebook/jest/pull/8874))
324324
- `[jest-types]` Mark `InitialOptions` as `Partial` ([#8848](https://github.com/facebook/jest/pull/8848))
325325
- `[jest-config]` Refactor `normalize` to be more type safe ([#8848](https://github.com/facebook/jest/pull/8848))
326+
- `[jest-haste-map]` [**BREAKING**] removed `providesModuleNodeModules` ([#8535](https://github.com/facebook/jest/pull/8535))
326327

327328
## 24.9.0
328329

TestUtils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
8282
globalSetup: null,
8383
globalTeardown: null,
8484
globals: {},
85-
haste: {
86-
providesModuleNodeModules: [],
87-
},
85+
haste: {},
8886
moduleDirectories: [],
8987
moduleFileExtensions: ['js'],
9088
moduleLoader: '/test_module_loader_path',

e2e/__tests__/__snapshots__/showConfig.test.ts.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ exports[`--showConfig outputs config info and exits 1`] = `
2020
"globals": {},
2121
"haste": {
2222
"computeSha1": false,
23-
"providesModuleNodeModules": [],
2423
"throwOnModuleCollision": false
2524
},
2625
"moduleDirectories": [

packages/jest-config/src/Defaults.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const defaultOptions: Config.DefaultOptions = {
2929
globals: {},
3030
haste: {
3131
computeSha1: false,
32-
providesModuleNodeModules: [],
3332
throwOnModuleCollision: false,
3433
},
3534
maxConcurrency: 5,

packages/jest-config/src/ValidConfig.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ const initialOptions: Config.InitialOptions = {
5656
defaultPlatform: 'ios',
5757
hasteImplModulePath: '<rootDir>/haste_impl.js',
5858
platforms: ['ios', 'android'],
59-
providesModuleNodeModules: ['react', 'react-native'],
6059
throwOnModuleCollision: false,
6160
},
6261
json: false,

packages/jest-core/src/__tests__/SearchSource.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ describe('SearchSource', () => {
401401
'__tests__',
402402
'haste_impl.js',
403403
),
404-
providesModuleNodeModules: [],
405404
},
406405
name: 'SearchSource-findRelatedTests-tests',
407406
rootDir,

packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ exports[`prints the config object 1`] = `
1818
"globalSetup": null,
1919
"globalTeardown": null,
2020
"globals": {},
21-
"haste": {
22-
"providesModuleNodeModules": []
23-
},
21+
"haste": {},
2422
"moduleDirectories": [],
2523
"moduleFileExtensions": [
2624
"js"

packages/jest-haste-map/src/__tests__/index.test.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ describe('HasteMap', () => {
329329
const hasteMap = new HasteMap({
330330
...defaultConfig,
331331
mocksPattern: '/__mocks__/',
332-
providesModuleNodeModules: ['react', 'fbjs'],
333332
});
334333

335334
return hasteMap.build().then(({__hasteMapForTest: data}) => {
@@ -341,23 +340,6 @@ describe('HasteMap', () => {
341340
'fruits/Pear.js': ['Pear', 32, 42, 1, 'Banana\0Strawberry', null],
342341
'fruits/Strawberry.js': ['Strawberry', 32, 42, 1, '', null],
343342
'fruits/__mocks__/Pear.js': ['', 32, 42, 1, 'Melon', null],
344-
// node modules
345-
'fruits/node_modules/fbjs/lib/flatMap.js': [
346-
'flatMap',
347-
32,
348-
42,
349-
1,
350-
'',
351-
null,
352-
],
353-
'fruits/node_modules/react/React.js': [
354-
'React',
355-
32,
356-
42,
357-
1,
358-
'Component',
359-
null,
360-
],
361343
'vegetables/Melon.js': ['Melon', 32, 42, 1, '', null],
362344
}),
363345
);
@@ -367,21 +349,9 @@ describe('HasteMap', () => {
367349
Banana: {[H.GENERIC_PLATFORM]: ['fruits/Banana.js', H.MODULE]},
368350
Melon: {[H.GENERIC_PLATFORM]: ['vegetables/Melon.js', H.MODULE]},
369351
Pear: {[H.GENERIC_PLATFORM]: ['fruits/Pear.js', H.MODULE]},
370-
React: {
371-
[H.GENERIC_PLATFORM]: [
372-
'fruits/node_modules/react/React.js',
373-
H.MODULE,
374-
],
375-
},
376352
Strawberry: {
377353
[H.GENERIC_PLATFORM]: ['fruits/Strawberry.js', H.MODULE],
378354
},
379-
flatMap: {
380-
[H.GENERIC_PLATFORM]: [
381-
'fruits/node_modules/fbjs/lib/flatMap.js',
382-
H.MODULE,
383-
],
384-
},
385355
}),
386356
);
387357

packages/jest-haste-map/src/index.ts

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ type Options = {
6161
mocksPattern?: string;
6262
name: string;
6363
platforms: Array<string>;
64-
providesModuleNodeModules?: Array<string>;
6564
resetCache?: boolean;
6665
retainAllFiles: boolean;
6766
rootDir: string;
@@ -127,28 +126,6 @@ const canUseWatchman = ((): boolean => {
127126
return false;
128127
})();
129128

130-
const escapePathSeparator = (string: string) =>
131-
path.sep === '\\' ? string.replace(/(\/|\\)/g, '\\\\') : string;
132-
133-
const getWhiteList = (list: Array<string> | undefined): RegExp | null => {
134-
if (list && list.length) {
135-
const newList = list.map(item =>
136-
escapePathSeparator(item.replace(/(\/)/g, path.sep)),
137-
);
138-
return new RegExp(
139-
'(' +
140-
escapePathSeparator(NODE_MODULES) +
141-
'(?:' +
142-
newList.join('|') +
143-
')(?=$|' +
144-
escapePathSeparator(path.sep) +
145-
'))',
146-
'g',
147-
);
148-
}
149-
return null;
150-
};
151-
152129
function invariant(condition: unknown, message?: string): asserts condition {
153130
if (!condition) {
154131
throw new Error(message);
@@ -241,7 +218,6 @@ class HasteMap extends EventEmitter {
241218
private _console: Console;
242219
private _options: InternalOptions;
243220
private _watchers: Array<Watcher>;
244-
private _whitelist: RegExp | null;
245221
private _worker: WorkerInterface | null;
246222

247223
constructor(options: Options) {
@@ -315,7 +291,6 @@ class HasteMap extends EventEmitter {
315291
hasteImplHash,
316292
dependencyExtractorHash,
317293
);
318-
this._whitelist = getWhiteList(options.providesModuleNodeModules);
319294
this._buildPromise = null;
320295
this._watchers = [];
321296
this._worker = null;
@@ -550,7 +525,7 @@ class HasteMap extends EventEmitter {
550525

551526
// If we retain all files in the virtual HasteFS representation, we avoid
552527
// reading them if they aren't important (node_modules).
553-
if (this._options.retainAllFiles && this._isNodeModulesDir(filePath)) {
528+
if (this._options.retainAllFiles && filePath.includes(NODE_MODULES)) {
554529
if (computeSha1) {
555530
return this._getWorker(workerOptions)
556531
.getSha1({
@@ -1075,32 +1050,10 @@ class HasteMap extends EventEmitter {
10751050

10761051
return (
10771052
ignoreMatched ||
1078-
(!this._options.retainAllFiles && this._isNodeModulesDir(filePath))
1053+
(!this._options.retainAllFiles && filePath.includes(NODE_MODULES))
10791054
);
10801055
}
10811056

1082-
private _isNodeModulesDir(filePath: Config.Path): boolean {
1083-
if (!filePath.includes(NODE_MODULES)) {
1084-
return false;
1085-
}
1086-
1087-
if (this._whitelist) {
1088-
const whitelist = this._whitelist;
1089-
const match = whitelist.exec(filePath);
1090-
const matchEndIndex = whitelist.lastIndex;
1091-
whitelist.lastIndex = 0;
1092-
1093-
if (!match) {
1094-
return true;
1095-
}
1096-
1097-
const filePathInPackage = filePath.substr(matchEndIndex);
1098-
return filePathInPackage.startsWith(NODE_MODULES);
1099-
}
1100-
1101-
return true;
1102-
}
1103-
11041057
private _createEmptyMap(): InternalHasteMap {
11051058
return {
11061059
clocks: new Map(),

packages/jest-runtime/src/__tests__/runtime_require_module.test.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,6 @@ describe('Runtime requireModule', () => {
252252
expect(hastePackage.isHastePackage).toBe(true);
253253
}));
254254

255-
it('resolves node modules properly when crawling node_modules', () =>
256-
// While we are crawling a node module, we shouldn't put package.json
257-
// files of node modules to resolve to `package.json` but rather resolve
258-
// to whatever the package.json's `main` field says.
259-
createRuntime(__filename, {
260-
haste: {
261-
providesModuleNodeModules: ['not-a-haste-package'],
262-
},
263-
}).then(runtime => {
264-
const hastePackage = runtime.requireModule(
265-
runtime.__mockRootPath,
266-
'not-a-haste-package',
267-
);
268-
expect(hastePackage.isNodeModule).toBe(true);
269-
}));
270-
271255
it('resolves platform extensions based on the default platform', () =>
272256
Promise.all([
273257
createRuntime(__filename).then(runtime => {

packages/jest-runtime/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ class Runtime {
285285
mocksPattern: escapePathForRegex(path.sep + '__mocks__' + path.sep),
286286
name: config.name,
287287
platforms: config.haste.platforms || ['ios', 'android'],
288-
providesModuleNodeModules: config.haste.providesModuleNodeModules,
289288
resetCache: options && options.resetCache,
290289
retainAllFiles: false,
291290
rootDir: config.rootDir,

packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.js.snap

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ Object {
1919
"globalSetup": null,
2020
"globalTeardown": null,
2121
"globals": Object {},
22-
"haste": Object {
23-
"providesModuleNodeModules": Array [],
24-
},
22+
"haste": Object {},
2523
"moduleDirectories": Array [],
2624
"moduleFileExtensions": Array [
2725
"js",
@@ -229,7 +227,7 @@ exports[`ScriptTransformer uses multiple preprocessors 1`] = `
229227
const TRANSFORMED = {
230228
filename: '/fruits/banana.js',
231229
script: 'module.exports = "banana";',
232-
config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"filter":null,"forceCoverageMatch":[],"globalSetup":null,"globalTeardown":null,"globals":{},"haste":{"providesModuleNodeModules":[]},"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"resolver":null,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"snapshotResolver":null,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["^.+\\\\.js$","test_preprocessor"],["^.+\\\\.css$","css-preprocessor"]],"transformIgnorePatterns":["/node_modules/"],"unmockedModulePathPatterns":null,"watchPathIgnorePatterns":[]}',
230+
config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"filter":null,"forceCoverageMatch":[],"globalSetup":null,"globalTeardown":null,"globals":{},"haste":{},"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"resolver":null,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"snapshotResolver":null,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["^.+\\\\.js$","test_preprocessor"],["^.+\\\\.css$","css-preprocessor"]],"transformIgnorePatterns":["/node_modules/"],"unmockedModulePathPatterns":null,"watchPathIgnorePatterns":[]}',
233231
};
234232
`;
235233

@@ -246,7 +244,7 @@ exports[`ScriptTransformer uses the supplied preprocessor 1`] = `
246244
const TRANSFORMED = {
247245
filename: '/fruits/banana.js',
248246
script: 'module.exports = "banana";',
249-
config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"filter":null,"forceCoverageMatch":[],"globalSetup":null,"globalTeardown":null,"globals":{},"haste":{"providesModuleNodeModules":[]},"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"resolver":null,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"snapshotResolver":null,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["^.+\\\\.js$","test_preprocessor"]],"transformIgnorePatterns":["/node_modules/"],"unmockedModulePathPatterns":null,"watchPathIgnorePatterns":[]}',
247+
config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"filter":null,"forceCoverageMatch":[],"globalSetup":null,"globalTeardown":null,"globals":{},"haste":{},"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"resolver":null,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"snapshotResolver":null,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["^.+\\\\.js$","test_preprocessor"]],"transformIgnorePatterns":["/node_modules/"],"unmockedModulePathPatterns":null,"watchPathIgnorePatterns":[]}',
250248
};
251249
`;
252250

packages/jest-types/src/Config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export type HasteConfig = {
2020
defaultPlatform?: string | null;
2121
hasteImplModulePath?: string;
2222
platforms?: Array<string>;
23-
providesModuleNodeModules: Array<string>;
2423
throwOnModuleCollision?: boolean;
2524
};
2625

packages/jest-validate/src/__tests__/__snapshots__/validate.test.ts.snap

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,7 @@ exports[`pretty prints valid config for Object 1`] = `
109109
<red></>
110110
<red> Example:</>
111111
<red> {</>
112-
<red> <bold>\\"haste\\"</>: <bold>{</></>
113-
<red><bold> \\"providesModuleNodeModules\\": [</></>
114-
<red><bold> \\"react\\",</></>
115-
<red><bold> \\"react-native\\"</></>
116-
<red><bold> ]</></>
117-
<red><bold> }</></>
112+
<red> <bold>\\"haste\\"</>: <bold>{}</></>
118113
<red> }</>
119114
<red></>"
120115
`;

packages/jest-validate/src/__tests__/fixtures/jestConfig.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ const defaultConfig = {
2929
coverageReporters: ['json', 'text', 'lcov', 'clover'],
3030
expand: false,
3131
globals: {},
32-
haste: {
33-
providesModuleNodeModules: [],
34-
},
32+
haste: {},
3533
moduleDirectories: ['node_modules'],
3634
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
3735
moduleNameMapper: {},
@@ -82,9 +80,7 @@ const validConfig = {
8280
expand: false,
8381
forceExit: false,
8482
globals: {},
85-
haste: {
86-
providesModuleNodeModules: ['react', 'react-native'],
87-
},
83+
haste: {},
8884
logHeapUsage: true,
8985
moduleDirectories: ['node_modules'],
9086
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],

packages/jest-validate/src/__tests__/validate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ test('omits null and undefined config values', () => {
7575

7676
test('recursively omits null and undefined config values', () => {
7777
const config = {
78-
haste: {
79-
providesModuleNodeModules: null,
78+
coverageThreshold: {
79+
global: null,
8080
},
8181
};
8282
expect(

website/versioned_docs/version-22.x/TutorialReactNative.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,3 @@ jest.mock('Text', () => {
201201
In other cases you may want to mock a native module that isn't a React component. The same technique can be applied. We recommend inspecting the native module's source code and logging the module when running a react native app on a real device and then modeling a manual mock after the real module.
202202

203203
If you end up mocking the same modules over and over it is recommended to define these mocks in a separate file and add it to the list of `setupFiles`.
204-
205-
### `@providesModule`
206-
207-
If you'd like to use Facebook's `@providesModule` module system through an npm package, the default haste config option must be overwritten and npm modules must be added to `providesModuleNodeModules`:
208-
209-
```json
210-
"haste": {
211-
"defaultPlatform": "ios",
212-
"platforms": ["android", "ios"],
213-
"providesModuleNodeModules": [
214-
"react",
215-
"react-native",
216-
"my-awesome-module",
217-
"my-text-component"
218-
]
219-
},
220-
```
221-
222-
If you'd like to test a different default platform or if you are building for other platforms, the `defaultPlatform` and `platforms` configuration option can be updated.

0 commit comments

Comments
 (0)