[Bug]: Module mock not hoisted to top with factory
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".
Please provide a minimal reproduction to help us address the issue, we cannot assist with troubleshooting based on the information currently provided. Thanks.
https://github.com/YanxinTang/rstest-module-mock
You can check this repo. The mock is not working after methods are reexported.
Any updates on this? Are you able to reproduce with the clone from @YanxinTang or do you need more information?
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
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.
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 do you have a rough estimate on delivery of the refactor? I'm excited :)
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