diff --git a/e2e/custom-mocks-pattern/__tests__/useMockedAddWithCustomPath.test.js b/e2e/custom-mocks-pattern/__tests__/useMockedAddWithCustomPath.test.js new file mode 100644 index 000000000000..4eb56b4677c9 --- /dev/null +++ b/e2e/custom-mocks-pattern/__tests__/useMockedAddWithCustomPath.test.js @@ -0,0 +1,6 @@ +jest.mock('add'); + +test('use mocked add method with custom path', () => { + const result = require('add')(1, 1); + expect(result).toBe(2); +}); diff --git a/e2e/custom-mocks-pattern/jest.config.js b/e2e/custom-mocks-pattern/jest.config.js new file mode 100644 index 000000000000..a3d1b64e40ff --- /dev/null +++ b/e2e/custom-mocks-pattern/jest.config.js @@ -0,0 +1,8 @@ +const path = require('node:path'); + +/** @type {import('jest').Config} */ +const config = { + customMockPath: path.resolve(__dirname, 'myMocks'), +}; + +module.exports = config; diff --git a/e2e/custom-mocks-pattern/myMocks/add.js b/e2e/custom-mocks-pattern/myMocks/add.js new file mode 100644 index 000000000000..3f72acb6b4cf --- /dev/null +++ b/e2e/custom-mocks-pattern/myMocks/add.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = (x, y) => x + y; diff --git a/packages/jest-resolve-dependencies/src/index.ts b/packages/jest-resolve-dependencies/src/index.ts index 99c53d2ea464..4e68f9a746c8 100644 --- a/packages/jest-resolve-dependencies/src/index.ts +++ b/packages/jest-resolve-dependencies/src/index.ts @@ -79,10 +79,12 @@ export class DependencyResolver { } if (resolvedMockDependency != null) { - const dependencyMockDir = path.resolve( - path.dirname(resolvedDependency), - '__mocks__', - ); + // eslint-disable-next-line no-console + console.log(options?.customMockPath); + const dependencyMockDir = + options?.customMockPath != null + ? path.resolve(path.dirname(resolvedDependency), '__mocks__') + : options?.customMockPath; resolvedMockDependency = path.resolve(resolvedMockDependency); diff --git a/packages/jest-resolve/src/resolver.ts b/packages/jest-resolve/src/resolver.ts index 7f378e859a19..ea61a7f072bd 100644 --- a/packages/jest-resolve/src/resolver.ts +++ b/packages/jest-resolve/src/resolver.ts @@ -39,6 +39,7 @@ export type ResolveModuleConfig = { conditions?: Array; skipNodeResolution?: boolean; paths?: Array; + customMockPath?: string; }; const NATIVE_PLATFORM = 'native'; diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 024896fb8a0b..504f4714bba4 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -239,6 +239,7 @@ export type InitialOptions = Partial<{ coverageProvider: CoverageProvider; coverageReporters: CoverageReporters; coverageThreshold: CoverageThreshold; + customMockPath?: string; dependencyExtractor: string; detectLeaks: boolean; detectOpenHandles: boolean; @@ -438,6 +439,7 @@ export type ProjectConfig = { collectCoverageFrom: Array; coverageDirectory: string; coveragePathIgnorePatterns: Array; + customMockPath?: string; cwd: string; dependencyExtractor?: string; detectLeaks: boolean;