Skip to content
99 changes: 56 additions & 43 deletions packages/metro-file-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jest.mock('../crawlers/watchman', () => ({
'', // dependencies
hash,
typeof contentOrLink !== 'string' ? 1 : 0,
'', // Haste name
null, // Haste name
]);
}
} else {
Expand Down Expand Up @@ -524,7 +524,7 @@ describe('FileMap', () => {
'Melon',
null,
0,
'',
null,
],
[path.join('vegetables', 'Melon.js')]: [
32,
Expand Down Expand Up @@ -582,7 +582,7 @@ describe('FileMap', () => {
// $FlowFixMe[missing-local-annot]
node.mockImplementation(options => {
// The node crawler returns "null" for the SHA-1.
const changedFiles = createMap({
const changedFiles = createMap<FileMetadata>({
[path.join('fruits', 'Banana.js')]: [
32,
42,
Expand Down Expand Up @@ -617,7 +617,7 @@ describe('FileMap', () => {
'Melon',
null,
0,
'',
null,
],
[path.join('vegetables', 'Melon.js')]: [
32,
Expand All @@ -637,7 +637,7 @@ describe('FileMap', () => {
'',
null,
1,
'',
null,
],
}
: null),
Expand Down Expand Up @@ -695,7 +695,7 @@ describe('FileMap', () => {
'Melon',
'8d40afbb6e2dc78e1ba383b6d02cafad35cceef2',
0,
'',
null,
],
[path.join('vegetables', 'Melon.js')]: [
32,
Expand All @@ -714,7 +714,7 @@ describe('FileMap', () => {
1,
'',
null,
'',
null,
],
}
: null),
Expand Down Expand Up @@ -1422,7 +1422,7 @@ describe('FileMap', () => {
// $FlowFixMe[missing-local-annot]
watchman.mockImplementation(async options => {
const {changedFiles} = await mockImpl(options);
changedFiles.set(invalidFilePath, [34, 44, 0, '', null, 0, '']);
changedFiles.set(invalidFilePath, [34, 44, 0, '', null, 0, null]);
return {
changedFiles,
removedFiles: new Set(),
Expand Down Expand Up @@ -1455,6 +1455,24 @@ describe('FileMap', () => {
expect.objectContaining({
// With maxFilesPerWorker = 2 and 5 files, we should have 3 workers.
numWorkers: 3,
setupArgs: [
{
dependencyExtractor,
plugins: [
{
lazy: false,
match: /package\.json$/,
workerModulePath: expect.stringMatching(
/src[\\\/]plugins[\\\/]haste[\\\/]worker\.js/,
),
workerSetupArgs: {
enableHastePackages: true,
hasteImplModulePath: null,
},
},
],
},
],
}),
);

Expand All @@ -1465,54 +1483,44 @@ describe('FileMap', () => {
{
computeDependencies: true,
computeSha1: false,
dependencyExtractor,
enableHastePackages: true,
filePath: path.join('/', 'project', 'fruits', 'Banana.js'),
hasteImplModulePath: undefined,
isNodeModules: false,
maybeReturnContent: false,
},
],
[
{
computeDependencies: true,
computeSha1: false,
dependencyExtractor,
enableHastePackages: true,
filePath: path.join('/', 'project', 'fruits', 'Pear.js'),
hasteImplModulePath: undefined,
isNodeModules: false,
maybeReturnContent: false,
},
],
[
{
computeDependencies: true,
computeSha1: false,
dependencyExtractor,
enableHastePackages: true,
filePath: path.join('/', 'project', 'fruits', 'Strawberry.js'),
hasteImplModulePath: undefined,
isNodeModules: false,
maybeReturnContent: false,
},
],
[
{
computeDependencies: true,
computeSha1: false,
dependencyExtractor,
enableHastePackages: true,
filePath: path.join('/', 'project', 'fruits', '__mocks__', 'Pear.js'),
hasteImplModulePath: undefined,
isNodeModules: false,
maybeReturnContent: false,
},
],
[
{
computeDependencies: true,
computeSha1: false,
dependencyExtractor,
enableHastePackages: true,
filePath: path.join('/', 'project', 'vegetables', 'Melon.js'),
hasteImplModulePath: undefined,
isNodeModules: false,
maybeReturnContent: false,
},
],
Expand All @@ -1533,7 +1541,7 @@ describe('FileMap', () => {
node.mockImplementation((() => {
return Promise.resolve({
changedFiles: createMap({
[path.join('fruits', 'Banana.js')]: [32, 42, 0, '', null, 0, ''],
[path.join('fruits', 'Banana.js')]: [32, 42, 0, '', null, 0, null],
}),
removedFiles: new Set(),
});
Expand Down Expand Up @@ -1575,7 +1583,7 @@ describe('FileMap', () => {
node.mockImplementation(() => {
return Promise.resolve({
changedFiles: createMap<FileMetadata>({
[path.join('fruits', 'Banana.js')]: [32, 42, 0, '', null, 0, ''],
[path.join('fruits', 'Banana.js')]: [32, 42, 0, '', null, 0, null],
}),
removedFiles: new Set(),
});
Expand Down Expand Up @@ -1681,7 +1689,8 @@ describe('FileMap', () => {
fm_it('build returns a "live" fileSystem and hasteMap', async hm => {
const {fileSystem, hasteMap} = await hm.build();
const filePath = path.join('/', 'project', 'fruits', 'Banana.js');
expect(fileSystem.getModuleName(filePath)).toBeDefined();
expect(fileSystem.exists(filePath)).toBe(true);
expect(hasteMap.getModuleNameByPath(filePath)).toBe('Banana');
expect(hasteMap.getModule('Banana')).toBe(filePath);
mockDeleteFile(path.join('/', 'project', 'fruits'), 'Banana.js');
mockDeleteFile(path.join('/', 'project', 'fruits'), 'Banana.js');
Expand All @@ -1698,7 +1707,8 @@ describe('FileMap', () => {
};
expect(eventsQueue).toEqual([deletedBanana]);
// Verify that the initial result has been updated
expect(fileSystem.getModuleName(filePath)).toBeNull();
expect(fileSystem.exists(filePath)).toBe(false);
expect(hasteMap.getModuleNameByPath(filePath)).toBeNull();
expect(hasteMap.getModule('Banana')).toBeNull();
});

Expand Down Expand Up @@ -1765,10 +1775,8 @@ describe('FileMap', () => {
},
]);
expect(
fileSystem.getModuleName(
path.join('/', 'project', 'fruits', 'Tomato.js'),
),
).not.toBeNull();
fileSystem.exists(path.join('/', 'project', 'fruits', 'Tomato.js')),
).toBe(true);
expect(hasteMap.getModule('Tomato')).toBeDefined();
expect(hasteMap.getModule('Pear')).toBe(
path.join('/', 'project', 'fruits', 'Pear.js'),
Expand Down Expand Up @@ -1940,7 +1948,7 @@ describe('FileMap', () => {
expect(eventsQueue).toEqual([
{filePath, metadata: MOCK_CHANGE_FILE, type: 'add'},
]);
expect(fileSystem.getModuleName(filePath)).toBeDefined();
expect(fileSystem.exists(filePath)).toBe(true);
},
);

Expand All @@ -1967,7 +1975,7 @@ describe('FileMap', () => {
expect(eventsQueue).toEqual([
{filePath, metadata: MOCK_CHANGE_FILE, type: 'change'},
]);
expect(fileSystem.getModuleName(filePath)).toBeDefined();
expect(fileSystem.exists(filePath)).toBe(true);
},
);

Expand All @@ -1990,7 +1998,7 @@ describe('FileMap', () => {
expect(eventsQueue).toEqual([
{filePath, metadata: MOCK_DELETE_FILE, type: 'delete'},
]);
expect(fileSystem.getModuleName(filePath)).toBeDefined();
expect(fileSystem.exists(filePath)).toBe(false);
expect(console.warn).not.toHaveBeenCalled();
expect(console.error).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -2025,8 +2033,13 @@ describe('FileMap', () => {
modifiedTime: 46,
size: 5,
});
// getModuleName traverses the symlink, verifying the link is read.
expect(fileSystem.getModuleName(filePath)).toEqual('Strawberry');
// lookup traverses the symlink, verifying the link is read.
expect(fileSystem.lookup(filePath)).toEqual(
expect.objectContaining({
exists: true,
realPath: expect.stringMatching(/Strawberry\.js$/),
}),
);
},
{config: {enableSymlinks: true}},
);
Expand All @@ -2044,8 +2057,8 @@ describe('FileMap', () => {
);
const realPath = path.join('/', 'project', 'fruits', 'Strawberry.js');

expect(fileSystem.getModuleName(symlinkPath)).toEqual('Strawberry');
expect(fileSystem.getModuleName(realPath)).toEqual('Strawberry');
expect(hasteMap.getModuleNameByPath(symlinkPath)).toEqual('Strawberry');
expect(hasteMap.getModuleNameByPath(realPath)).toEqual('Strawberry');
expect(hasteMap.getModule('Strawberry', 'g')).toEqual(realPath);

// Delete the symlink
Expand All @@ -2065,8 +2078,8 @@ describe('FileMap', () => {
// Symlink is deleted without affecting the Haste module or real file.
expect(fileSystem.exists(symlinkPath)).toBe(false);
expect(fileSystem.exists(realPath)).toBe(true);
expect(fileSystem.getModuleName(symlinkPath)).toEqual(null);
expect(fileSystem.getModuleName(realPath)).toEqual('Strawberry');
expect(hasteMap.getModuleNameByPath(symlinkPath)).toEqual(null);
expect(hasteMap.getModuleNameByPath(realPath)).toEqual('Strawberry');
expect(hasteMap.getModule('Strawberry', 'g')).toEqual(realPath);
},
{config: {enableSymlinks: true}},
Expand All @@ -2075,7 +2088,7 @@ describe('FileMap', () => {
fm_it(
'correctly tracks changes to both platform-specific versions of a single module name',
async hm => {
const {hasteMap, fileSystem} = await hm.build();
const {hasteMap} = await hm.build();
expect(hasteMap.getModule('Orange', 'ios')).toBeTruthy();
expect(hasteMap.getModule('Orange', 'android')).toBeTruthy();
const e = mockEmitters[path.join('/', 'project', 'fruits')];
Expand Down Expand Up @@ -2104,12 +2117,12 @@ describe('FileMap', () => {
},
]);
expect(
fileSystem.getModuleName(
hasteMap.getModuleNameByPath(
path.join('/', 'project', 'fruits', 'Orange.ios.js'),
),
).toBeTruthy();
expect(
fileSystem.getModuleName(
hasteMap.getModuleNameByPath(
path.join('/', 'project', 'fruits', 'Orange.android.js'),
),
).toBeTruthy();
Expand Down
Loading
Loading