Mock object in jest with eslint

Suppose I have the following code

export function myFunction(user) {
    return transform(user);

I have created a __tests__ folder, and inside it I created a __mocks__ folder with user.mock.ts

which has

export const user = { id: 1, name: "John" } 

Now in my __test__ folder, I have user.spec.ts

import { user } from './__mocks__/user.mock';

describe('user', () => {
  it('should map user correctly', () => {
    const expected = { id: 1, name: "John" } 
    const result = myFunction(user);

I have the following eslintrc

  "parser": "@typescript-eslint/parser",
  "plugins": [
  "extends": [
  "rules": {

I get the following error

Mocks should not be manually imported from a mocks directory. Instead use jest.mock and import from the original module path.eslintjest/no-mocks-import


The mechanism of using a specially-named directory called __mocks__ is a way of jest being able to auto-detect mocks for packages (not as a place to put code you plan to run explicitly).

Files from this folder would never be explicitly imported by you in your code as part of a test.

So if you just use a normal folder name for your case, and don’t cross-wire with the jest convention, then you should be able to eliminate the error.

By contrast if you wanted to actually mock an existing package (like replace fetch with your own version of fetch) then see for how you would construct a file e.g. called __mocks__/fetch.ts to swap out the real implementation of fetch for one which fakes its behaviour. That is the only thing the specially-named directory should be used for.