Skip to content

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

@BastianHofmannZeiss

Description

@BastianHofmannZeiss

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".

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