Skip to content

Commit

Permalink
jest-haste-map: add test+fix for broken platform module support (#3885)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanlauliac authored and cpojer committed Jun 27, 2017
1 parent c9598ff commit 88e2ee8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
48 changes: 46 additions & 2 deletions packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,12 @@ describe('HasteMap', () => {
e.emit('all', 'delete', filePath, dirPath, undefined);
}

function hm_it(title, fn) {
it(title, async () => {
function hm_it(title, fn, options) {
options = options || {};
(options.only ? it.only : it)(title, async () => {
if (options.mockFs) {
mockFs = options.mockFs;
}
const watchConfig = Object.assign({}, defaultConfig, {watch: true});
const hm = new HasteMap(watchConfig);
await hm.build();
Expand Down Expand Up @@ -763,6 +767,46 @@ describe('HasteMap', () => {
},
);

hm_it(
'correctly tracks changes to both platform-specific versions of a single module name',
async hm => {
const {moduleMap: initMM} = await hm.build();
expect(initMM.getModule('Orange', 'ios')).toBeTruthy();
expect(initMM.getModule('Orange', 'android')).toBeTruthy();
const e = mockEmitters['/fruits'];
e.emit('all', 'change', 'Orange.ios.js', '/fruits/', MOCK_STAT);
e.emit('all', 'change', 'Orange.android.js', '/fruits/', MOCK_STAT);
const {eventsQueue, hasteFS, moduleMap} = await waitForItToChange(hm);
expect(eventsQueue).toHaveLength(2);
expect(eventsQueue).toEqual([
{filePath: '/fruits/Orange.ios.js', stat: MOCK_STAT, type: 'change'},
{
filePath: '/fruits/Orange.android.js',
stat: MOCK_STAT,
type: 'change',
},
]);
expect(hasteFS.getModuleName('/fruits/Orange.ios.js')).toBeTruthy();
expect(hasteFS.getModuleName('/fruits/Orange.android.js')).toBeTruthy();
const iosVariant = moduleMap.getModule('Orange', 'ios');
expect(iosVariant).toBe('/fruits/Orange.ios.js');
const androidVariant = moduleMap.getModule('Orange', 'android');
expect(androidVariant).toBe('/fruits/Orange.android.js');
},
{
mockFs: {
'/fruits/Orange.android.js': `/**
* @providesModule Orange
*/
`,
'/fruits/Orange.ios.js': `/**
* @providesModule Orange
*/
`,
},
},
);

describe('recovery from duplicate module IDs', () => {
async function setupDuplicates(hm) {
mockFs['/fruits/pear.js'] = [
Expand Down
18 changes: 17 additions & 1 deletion packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ class HasteMap extends EventEmitter {
const platform =
getPlatformExtension(module[H.PATH], this._options.platforms) ||
H.GENERIC_PLATFORM;

const existingModule = moduleMap[platform];
if (existingModule && existingModule[H.PATH] !== module[H.PATH]) {
const message =
Expand Down Expand Up @@ -656,8 +657,23 @@ class HasteMap extends EventEmitter {
// Delete the file and all of its metadata.
const moduleName =
hasteMap.files[filePath] && hasteMap.files[filePath][H.ID];
const platform: string =
getPlatformExtension(filePath, this._options.platforms) ||
H.GENERIC_PLATFORM;

delete hasteMap.files[filePath];
delete hasteMap.map[moduleName];
let moduleMap = hasteMap.map[moduleName];
if (moduleMap != null) {
// We are forced to copy the object because jest-haste-map exposes
// the map as an immutable entity.
moduleMap = copy(moduleMap);
delete moduleMap[platform];
if (Object.keys(moduleMap).length === 0) {
delete hasteMap.map[moduleName];
} else {
hasteMap.map[moduleName] = moduleMap;
}
}
if (
this._options.mocksPattern &&
this._options.mocksPattern.test(filePath)
Expand Down

0 comments on commit 88e2ee8

Please sign in to comment.