Skip to content

feat(mock): support mock: true option in rs.mock() #907

Merged
9aoy merged 2 commits intomainfrom
auto-mock
Jan 27, 2026
Merged

feat(mock): support mock: true option in rs.mock() #907
9aoy merged 2 commits intomainfrom
auto-mock

Conversation

@9aoy
Copy link
Copy Markdown
Collaborator

@9aoy 9aoy commented Jan 27, 2026

Summary

Support the { mock: true } option in rs.mock() and related APIs, allowing modules to be auto-mocked while the original implementations are not preserved - mock functions return undefined by default.

This is useful when you want to completely replace a module's behavior and configure mock return values or implementations.

import { expect, rs, test } from '@rstest/core';
import { add, multiply } from './math';

rs.mock('./math', { mock: true });

test('mock functions return undefined by default', () => {
  // Original implementation is NOT preserved
  expect(add(1, 2)).toBeUndefined();
  expect(multiply(3, 4)).toBeUndefined();

  // Functions are mock functions
  expect(rs.isMockFunction(add)).toBe(true);
});

test('can configure mock implementations', () => {
  // Configure return values
  rs.mocked(add).mockReturnValue(100);
  expect(add(1, 2)).toBe(100);

  // Configure implementations
  rs.mocked(multiply).mockImplementation((a, b) => a * b * 2);
  expect(multiply(3, 4)).toBe(24);
});

Related Links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Jan 27, 2026

Deploying rstest with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4095f2f
Status: ✅  Deploy successful!
Preview URL: https://31029def.rstest.pages.dev
Branch Preview URL: https://auto-mock.rstest.pages.dev

View logs

@9aoy 9aoy merged commit 7f66f0f into main Jan 27, 2026
10 checks passed
@9aoy 9aoy deleted the auto-mock branch January 27, 2026 03:33
@9aoy 9aoy mentioned this pull request Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant