Skip to content

[Bug]: isolateModules doesn't work properly with auto mocked modules (reopen #14443) #15022

Open
@Yupeng-li

Description

@Yupeng-li

Version

27.2.1 & 29.6.2 & 29.6.3

Steps to reproduce

I made a mini repo to reproduce the issue. This is to reopen #14443

Expected behavior

I expect this test to pass

/* in __mocks__ folder, lodash is mocked by the file below
   lodash.ts  
*/

import * as exportsFromIndexFile from "./index";
import {range} from 'lodash'

let indexModule : typeof exportsFromIndexFile

beforeEach(()=>{
    jest.isolateModules(()=>{
        indexModule = require('./index')
    })
})

it('mocks for a library in node_modules are not working', () => {
    indexModule.helloWorld(). // helloWorld is using lodash.range inside 
    expect(range).toHaveBeenCalled()  
});

Actual behavior

But expect(range).toHaveBeenCalled() failed. It says lodash.range was not called. If I use debugger, I can see that the it's been called.

Additional context

No response

Environment

System:
    OS: macOS 13.5.1
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 18.17.0 - ~/Library/Caches/fnm_multishells/24849_1670237417415/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.6.7 - ~/Library/Caches/fnm_multishells/24849_1670237417415/bin/npm
  npmPackages:
    jest: 29.6.3 => 29.6.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions