Skip to content

Commit 9c978bb

Browse files
Dmitrii AbramovFacebook Github Bot 4
authored andcommitted
moduleNameMapper substitutions
Summary: Closes jestjs#839 Differential Revision: D3104032 fb-gh-sync-id: 9a251636d25ed0ff25a82d1cf281ac192e530624 fbshipit-source-id: 9a251636d25ed0ff25a82d1cf281ac192e530624
1 parent 8cca67d commit 9c978bb

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

docs/API.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,14 @@ A map from regular expressions to module names that allow to stub out resources,
420420

421421
Use `<rootDir>` string token to refer to [`config.rootDir`](#config-rootdir-string) value if you want to use file paths.
422422

423+
Additionally, you can substitute captured regex groups using numbered backreferences.
424+
423425
Example:
424426
```js
425427
"moduleNameMapper": {
426428
"^image![a-zA-Z0-9$_-]+$": "GlobalImageStub",
427429
"^[./a-zA-Z0-9$_-]+\.png$": "<rootDir>/RelativeImageStub.js",
430+
"module_name_(.*)": "<rootDir>/substituted_module_$1.js"
428431
}
429432
```
430433

src/HasteModuleLoader/HasteModuleLoader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ class Loader {
563563
for (const mappedModuleName in nameMapper) {
564564
const regex = nameMapper[mappedModuleName];
565565
if (regex.test(moduleName)) {
566-
return mappedModuleName;
566+
return moduleName.replace(regex, mappedModuleName);
567567
}
568568
}
569569
}

src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModuleOrMock-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('HasteModuleLoader', function() {
3030
'^image![a-zA-Z0-9$_-]+$': 'GlobalImageStub',
3131
'^[./a-zA-Z0-9$_-]+\.png$': 'RelativeImageStub',
3232
'mappedToPath': '<rootDir>/GlobalImageStub.js',
33+
'module/name/(.*)': '<rootDir>/mapped_module_$1.js',
3334
},
3435
});
3536

@@ -122,6 +123,9 @@ describe('HasteModuleLoader', function() {
122123

123124
exports = loader.requireModuleOrMock(rootPath, 'dog.png');
124125
expect(exports.isRelativeImageStub).toBe(true);
126+
127+
exports = loader.requireModuleOrMock(rootPath, 'module/name/test');
128+
expect(exports).toBe('mapped_module');
125129
});
126130
});
127131

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
module.exports = 'mapped_module';

0 commit comments

Comments
 (0)