-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathresolver.ts
More file actions
43 lines (37 loc) · 1.3 KB
/
resolver.ts
File metadata and controls
43 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import type { MetroConfig } from '@react-native/metro-config';
import type { Config as HarnessConfig } from '@react-native-harness/config';
type CustomResolver = NonNullable<
NonNullable<MetroConfig['resolver']>['resolveRequest']
>;
export const getHarnessResolver = (
metroConfig: MetroConfig,
harnessConfig: HarnessConfig
): CustomResolver => {
// Can be relative to the project root or absolute, need to normalize it
const resolvedEntryPointPath = require.resolve(harnessConfig.entryPoint, {
paths: [process.cwd()],
});
return (context, moduleName, platform) => {
const existingResolver =
metroConfig.resolver?.resolveRequest ?? context.resolveRequest;
const resolvedModule = existingResolver(context, moduleName, platform);
// Replace the entry point with Harness
if (
resolvedModule.type === 'sourceFile' &&
resolvedModule.filePath === resolvedEntryPointPath
) {
return {
type: 'sourceFile',
filePath: require.resolve('@react-native-harness/runtime/entry-point'),
};
}
// Intercept @jest/globals imports and redirect to mock module
if (moduleName === '@jest/globals') {
return {
type: 'sourceFile',
filePath: require.resolve('./jest-globals-mock'),
};
}
return resolvedModule;
};
};