jestjs / jest

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

[Bug]: Mocking function from module doesn't seem to be working #15068

Open takecare opened 1 month ago

takecare commented 1 month ago

Version

29.7.0

Steps to reproduce

  1. Check the tests here
  2. Run them with npm test
  3. Observe as none of them pass

The different tests mock a function from a module in a different way. All of them as per documentation. However none of them seem to be working.

Expected behavior

The mocked function should be mocked, making the tests pass and it's real implementation not executed, as a result of the mocking.

Actual behavior

None of the mocks work. The actual implementation of the mocked function runs and none of the expects calls are passing.

Additional context

I've tried different suggestions from different posts on StackOverflow but none seem to work. There is no transpilation in this setup.

The only thing that worked was changing all the call-sites to reference the fully exported function - i.e. instead of calling myFunctionToMock() I'd have to call module.exports.myFunctionToMock().

Environment

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 21.6.2 - /opt/homebrew/bin/node
    Yarn: 1.22.21 - /opt/homebrew/bin/yarn
    npm: 10.2.4 - /opt/homebrew/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
github-actions[bot] commented 3 weeks 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.

takecare commented 3 weeks ago

sorry to bump this way but i reckon this is not expected behaviour?