Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest-haste-map: add test+fix for broken platform module support #3885

Merged
merged 1 commit into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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