jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
43.89k stars 6.4k forks source link

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

Open Yupeng-li opened 3 months ago

Yupeng-li commented 3 months ago

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
github-actions[bot] commented 2 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

Yupeng-li commented 2 months ago

not stale

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

liamjones commented 1 month ago

Not stale

github-actions[bot] commented 1 week ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

liamjones commented 1 week ago

Not stale