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

metro-file-map: Extract mocks handling into nullable MockMap #1402

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 30 additions & 5 deletions packages/metro-file-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,36 @@ describe('FileMap', () => {
expect(fs.readFileSync.mock.calls.length).toBe(5);
});

test('builds a mock map if mocksPattern is non-null', async () => {
const pathToMock = path.join(
'/',
'project',
'fruits1',
'__mocks__',
'Blueberry.js',
);
mockFs[pathToMock] = '/* empty */';

const {mockMap} = await new FileMap({
mocksPattern: '__mocks__',
throwOnModuleCollision: true,
...defaultConfig,
}).build();

expect(mockMap).not.toBeNull();
expect(mockMap.getMockModule('Blueberry')).toEqual(pathToMock);
});

test('returns null mockMap if mocksPattern is empty', async () => {
const {mockMap} = await new FileMap({
mocksPattern: '',
throwOnModuleCollision: true,
...defaultConfig,
}).build();

expect(mockMap).toBeNull();
});

test('warns on duplicate mock files', async () => {
expect.assertions(1);

Expand Down Expand Up @@ -1341,7 +1371,6 @@ describe('FileMap', () => {
enableHastePackages: true,
filePath: path.join('/', 'project', 'fruits', 'Banana.js'),
hasteImplModulePath: undefined,
readLink: false,
rootDir: path.join('/', 'project'),
},
],
Expand All @@ -1353,7 +1382,6 @@ describe('FileMap', () => {
enableHastePackages: true,
filePath: path.join('/', 'project', 'fruits', 'Pear.js'),
hasteImplModulePath: undefined,
readLink: false,
rootDir: path.join('/', 'project'),
},
],
Expand All @@ -1365,7 +1393,6 @@ describe('FileMap', () => {
enableHastePackages: true,
filePath: path.join('/', 'project', 'fruits', 'Strawberry.js'),
hasteImplModulePath: undefined,
readLink: false,
rootDir: path.join('/', 'project'),
},
],
Expand All @@ -1377,7 +1404,6 @@ describe('FileMap', () => {
enableHastePackages: true,
filePath: path.join('/', 'project', 'fruits', '__mocks__', 'Pear.js'),
hasteImplModulePath: undefined,
readLink: false,
rootDir: path.join('/', 'project'),
},
],
Expand All @@ -1389,7 +1415,6 @@ describe('FileMap', () => {
enableHastePackages: true,
filePath: path.join('/', 'project', 'vegetables', 'Melon.js'),
hasteImplModulePath: undefined,
readLink: false,
rootDir: path.join('/', 'project'),
},
],
Expand Down
33 changes: 0 additions & 33 deletions packages/metro-file-map/src/__tests__/worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ jest.mock('fs', () => {
}
throw new Error(`Cannot read path '${path}'.`);
}),
promises: {
readlink: jest.fn(async path => {
const entry = mockFs[path];
if (entry) {
if (typeof entry.link === 'string') {
return entry.link;
} else {
throw new Error('Tried to call readlink on a non-symlink');
}
}
throw new Error(`Cannot read path '${path}'.`);
}),
},
};
});

Expand Down Expand Up @@ -240,26 +227,6 @@ describe('worker', () => {
expect(fs.readFile).not.toHaveBeenCalled();
});

test('calls readLink and returns symlink target when readLink=true', async () => {
expect(
await worker({
computeDependencies: false,
filePath: path.join('/project', 'fruits', 'LinkToStrawberry.js'),
readLink: true,
rootDir,
}),
).toEqual({
dependencies: undefined,
id: undefined,
module: undefined,
sha1: undefined,
symlinkTarget: path.join('.', 'Strawberry.js'),
});

expect(fs.readFileSync).not.toHaveBeenCalled();
expect(fs.promises.readlink).toHaveBeenCalled();
});

test('can be loaded directly without transpilation', async () => {
const code = await jest
.requireActual('fs')
Expand Down
4 changes: 1 addition & 3 deletions packages/metro-file-map/src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type BuildParameters = $ReadOnly<{
export type BuildResult = {
fileSystem: FileSystem,
hasteMap: HasteMap,
mockMap: MockMap,
mockMap: ?MockMap,
};

export type CacheData = $ReadOnly<{
Expand Down Expand Up @@ -326,7 +326,6 @@ export type WorkerMessage = $ReadOnly<{
computeSha1: boolean,
dependencyExtractor?: ?string,
enableHastePackages: boolean,
readLink: boolean,
rootDir: string,
filePath: string,
hasteImplModulePath?: ?string,
Expand All @@ -337,5 +336,4 @@ export type WorkerMetadata = $ReadOnly<{
id?: ?string,
module?: ?HasteMapItemMetaData,
sha1?: ?string,
symlinkTarget?: ?string,
}>;
Loading
Loading