rstest icon indicating copy to clipboard operation
rstest copied to clipboard

[Bug]: Module mock not hoisted to top with factory

Open BastianHofmannZeiss opened this issue 4 months ago • 8 comments

Version

System:
    OS: macOS 15.6
    CPU: (10) arm64 Apple M1 Pro
    Memory: 1.17 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 139.0.7258.139
    Edge: 139.0.3405.111
    Safari: 18.6
  npmPackages:
    @rstest/core: ^0.2.2 => 0.2.2

Details

When providing a factory function to the module mocking method, the mock seems to no longer be hoisted to the top. The module is not mocked in the system under test in this case.

Reproduce link

https://codepen.io/Justineo/pen/yLbxxOR

Reproduce Steps

App.test.tsx:

import { expect, test } from '@rstest/core';
import { render, screen } from '@testing-library/react';
import App from '../src/App';

rs.mock('../src/module', () => ({
  some: () => 'mocked',
}));

test('should render App correctly', async () => {
  render(<App />);

  const element = screen.getByRole('title');

  expect(element.textContent).toBe('mocked');
});

App.tsx:

import './App.css';

import { some } from './module';

const App = () => {
  return (
    <div className="content">
      <h1 role="title">{some()}</h1>
      <p>Start building amazing things with Rsbuild.</p>
    </div>
  );
};

export default App;

module.ts

export const some = () => 'example';

The test case will fail with the text content being "example".

BastianHofmannZeiss avatar Aug 26 '25 05:08 BastianHofmannZeiss

Please provide a minimal reproduction to help us address the issue, we cannot assist with troubleshooting based on the information currently provided. Thanks.

Why reproduction is required

fi3ework avatar Aug 26 '25 06:08 fi3ework

https://github.com/YanxinTang/rstest-module-mock

You can check this repo. The mock is not working after methods are reexported.

YanxinTang avatar Aug 29 '25 16:08 YanxinTang

Any updates on this? Are you able to reproduce with the clone from @YanxinTang or do you need more information?

BastianHofmannZeiss avatar Sep 10 '25 13:09 BastianHofmannZeiss

Any updates on this? Are you able to reproduce with the clone from @YanxinTang or do you need more information?

If you urgently need this feature, you can downgrade to version 0.0.9 for now

njzydark avatar Sep 11 '25 02:09 njzydark

Are there any updates on this? We would like to use the latest version of the library in a bigger project, but this is blocking the migration.

BastianHofmannZeiss avatar Oct 07 '25 04:10 BastianHofmannZeiss

after internal discussion, we believe that supporting async module factory would be a complex and non-essential feature. currently, we have decided to change the way mocks are implemented, only supporting synchronous mock factories. we will resolve this issue after refactoring.

fi3ework avatar Oct 09 '25 09:10 fi3ework

@fi3ework do you have a rough estimate on delivery of the refactor? I'm excited :)

jhage-snapsheet avatar Oct 24 '25 15:10 jhage-snapsheet

https://github.com/YanxinTang/rstest-module-mock

You can check this repo. The mock is not working after methods are reexported.

All tests passed after upgrading to 0.6.1

YanxinTang avatar Nov 03 '25 16:11 YanxinTang