Skip to content

Commit

Permalink
Throw on module collision (#1750)
Browse files Browse the repository at this point in the history
* add throwOnModuleCollision option that, when set to true, will throw an error when a module name collision is detected (as opposed to simply printing a warning)

* fix lint errors

* alphabetize options; made code more compact

* Update index.js

* Update index-test.js.snap
  • Loading branch information
matryoshcow authored and cpojer committed Sep 22, 2016
1 parent a5de858 commit a5832d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
exports[`HasteMap throws on duplicate module ids if "throwOnModuleCollision" is set to true 1`] = `
[Error: jest-haste-map: @providesModule naming collision:
Duplicate module name: Strawberry
Paths: /fruits/raspberry.js collides with /fruits/strawberry.js
This error is caused by a @providesModule declaration with the same name across two different files.]
`;

exports[`HasteMap tries to crawl using node as a fallback 1`] = `
"jest-haste-map: Watchman crawl failed. Retrying once with node crawler.
Usually this happens when watchman isn\'t running. Create an empty \`.watchmanconfig\` file in your project\'s root folder or initialize a git or hg repository in your project.
Expand Down
16 changes: 16 additions & 0 deletions packages/jest-haste-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,22 @@ describe('HasteMap', () => {
});
});

it('throws on duplicate module ids if "throwOnModuleCollision" is set to true', () => {
// Raspberry thinks it is a Strawberry
mockFs['/fruits/raspberry.js'] = [
'/**',
' * @providesModule Strawberry',
' */',
'const Banana = require("Banana");',
].join('\n');

return new HasteMap(
Object.assign({throwOnModuleCollision: true}, defaultConfig),
).build().catch(err => {
expect(err).toMatchSnapshot();
});
});

it('splits up modules by platform', () => {
mockFs = Object.create(null);
mockFs['/fruits/strawberry.js'] = [
Expand Down
16 changes: 11 additions & 5 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Options = {
resetCache?: boolean,
retainAllFiles: boolean,
roots: Array<string>,
throwOnModuleCollision?: boolean,
useWatchman?: boolean,
};

Expand Down Expand Up @@ -187,6 +188,7 @@ class HasteMap {
resetCache: options.resetCache,
retainAllFiles: options.retainAllFiles,
roots: options.roots,
throwOnModuleCollision: !!options.throwOnModuleCollision,
useWatchman:
options.useWatchman == null ? true : options.useWatchman,
};
Expand Down Expand Up @@ -273,14 +275,18 @@ class HasteMap {
getPlatformExtension(module[H.PATH]) || H.GENERIC_PLATFORM;
const existingModule = moduleMap[platform];
if (existingModule && existingModule[H.PATH] !== module[H.PATH]) {
this._console.warn(
const message =
`jest-haste-map: @providesModule naming collision:\n` +
` Duplicate module name: ${id}\n` +
` Paths: ${module[H.PATH]} collides with ` +
`${existingModule[H.PATH]}\n\n` +
`This warning is caused by a @providesModule declaration ` +
`with the same name across two different files.`,
);
`${existingModule[H.PATH]}\n\nThis ` +
`${this._options.throwOnModuleCollision ? 'error' : 'warning'} ` +
`is caused by a @providesModule declaration ` +
`with the same name across two different files.`;
if (this._options.throwOnModuleCollision) {
throw new Error(message);
}
this._console.warn(message);
return;
}

Expand Down

0 comments on commit a5832d8

Please sign in to comment.