Skip to content

[Bug]: Unable to mock react-router-dom in a react project #809

@saravmajestic

Description

@saravmajestic

Version

System:
    OS: macOS 15.6.1
    CPU: (14) arm64 Apple M4 Max
    Memory: 3.81 GB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 143.0.7499.170
    Safari: 18.6
  npmPackages:
    @rstest/core: ^0.7.7 => 0.7.7

Details

I have a very basic react app with react-router-dom with path params. I am using useParams to get path params in component and just displaying it. I am unable to mock the react-router-dom module with right useParams return value

src/pages/Menu1.test.tsx (in reproduction repo) has 2 tests, first one trying to use react-router-dom mocked version, and it fails. 2nd test uses complete route structure and it passes. I have tried rs.doMock, rs.mock but still not working.

Same test works with jest

Any help would be appreciated.

Reproduce link

https://github.com/saravmajestic/rstest-react-router-dom-mock

Reproduce Steps

  1. yarn install
  2. yarn test
  3. Will show 1 failed and 1 passed test.

Failed test tries to use mocked react-router-dom.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions