Skip to content

Commit

Permalink
do not overwrite existing module mocks (jestjs#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris authored and cpojer committed Jan 12, 2017
1 parent c3f708b commit 2865716
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 7 additions & 3 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ResolverConfig = {|
extensions: Array<string>,
hasCoreModules: boolean,
moduleDirectories: Array<string>,
moduleNameMapper: ?{[key: string]: RegExp},
moduleNameMapper: ?Array<ModuleNameMapperConfig>,
modulePaths: Array<Path>,
platforms?: Array<string>,
|};
Expand All @@ -37,6 +37,11 @@ type FindNodeModuleConfig = {|
paths?: Array<Path>,
|};

type ModuleNameMapperConfig = {|
regex: RegExp,
moduleName: string
|};

export type ResolveModuleConfig = {|
skipNodeResolution?: boolean,
|};
Expand Down Expand Up @@ -215,8 +220,7 @@ class Resolver {

const moduleNameMapper = this._options.moduleNameMapper;
if (moduleNameMapper) {
for (const mappedModuleName in moduleNameMapper) {
const regex = moduleNameMapper[mappedModuleName];
for (const {moduleName: mappedModuleName, regex} of moduleNameMapper) {
if (regex.test(moduleName)) {
const matches = moduleName.match(regex);
if (!matches) {
Expand Down
8 changes: 3 additions & 5 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ const SNAPSHOT_EXTENSION = 'snap';

const getModuleNameMapper = (config: Config) => {
if (config.moduleNameMapper.length) {
const moduleNameMapper = Object.create(null);
config.moduleNameMapper.forEach(
map => moduleNameMapper[map[1]] = new RegExp(map[0]),
);
return moduleNameMapper;
return config.moduleNameMapper.map(([regex, moduleName]) => {
return {moduleName, regex: new RegExp(regex)};
});
}
return null;
};
Expand Down

0 comments on commit 2865716

Please sign in to comment.