Skip to content

feat(draft): custom folder to avoid repetitions with __mocks__ #14338

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

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -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);
});
8 changes: 8 additions & 0 deletions e2e/custom-mocks-pattern/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('node:path');

/** @type {import('jest').Config} */
const config = {
customMockPath: path.resolve(__dirname, 'myMocks'),
};

module.exports = config;
3 changes: 3 additions & 0 deletions e2e/custom-mocks-pattern/myMocks/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = (x, y) => x + y;
10 changes: 6 additions & 4 deletions packages/jest-resolve-dependencies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@
}

if (resolvedMockDependency != null) {
const dependencyMockDir = path.resolve(
path.dirname(resolvedDependency),
'__mocks__',
);
// eslint-disable-next-line no-console
console.log(options?.customMockPath);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe remove this console.log?

Copy link
Author

@scarletquasar scarletquasar Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a draft so I didn't remove these checkers yet

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, okay, nice

const dependencyMockDir =
options?.customMockPath != null
? path.resolve(path.dirname(resolvedDependency), '__mocks__')

Check warning on line 86 in packages/jest-resolve-dependencies/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-resolve-dependencies/src/index.ts#L86

Added line #L86 was not covered by tests
: options?.customMockPath;

resolvedMockDependency = path.resolve(resolvedMockDependency);

Expand Down
1 change: 1 addition & 0 deletions packages/jest-resolve/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type ResolveModuleConfig = {
conditions?: Array<string>;
skipNodeResolution?: boolean;
paths?: Array<string>;
customMockPath?: string;
};

const NATIVE_PLATFORM = 'native';
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export type InitialOptions = Partial<{
coverageProvider: CoverageProvider;
coverageReporters: CoverageReporters;
coverageThreshold: CoverageThreshold;
customMockPath?: string;
dependencyExtractor: string;
detectLeaks: boolean;
detectOpenHandles: boolean;
Expand Down Expand Up @@ -438,6 +439,7 @@ export type ProjectConfig = {
collectCoverageFrom: Array<string>;
coverageDirectory: string;
coveragePathIgnorePatterns: Array<string>;
customMockPath?: string;
cwd: string;
dependencyExtractor?: string;
detectLeaks: boolean;
Expand Down