Skip to content

Commit 025e1ef

Browse files
chore: update unit testing cursor rule for theme mocks (MetaMask#27825)
## **Description** Updates `.cursor/rules/unit-testing-guidelines.mdc` with clearer guidance for theme-related mocks in unit tests: - Prefer `mockTheme` (real design tokens) instead of hardcoded hex values. - Mock `useTailwind()` with the correct return shape (supports `tw.style(...)`, and only add `tw.color(...)` when the component under test uses it). This is intended to reduce churn and align new tests with the `@metamask/design-tokens/color-no-hex` ESLint rule. Related / prior `color-no-hex` PRs: - MetaMask#26958 - MetaMask#26963 - MetaMask#27008 - MetaMask#27030 - MetaMask#27031 - MetaMask#27149 - MetaMask#27150 - MetaMask#27151 ## **Changelog** CHANGELOG entry: null ## **Related issues** Fixes: ## **Manual testing steps** ```gherkin Feature: Cursor unit testing guidelines Scenario: guidance for theme-related mocks is clear Given a developer is writing or updating a unit test When they follow the updated theme mocking guidance Then they can avoid hardcoded hex colors and mock useTailwind without type/shape mismatches ``` ## **Screenshots/Recordings** ### **Before** N/A ### **After** N/A ## **Pre-merge author checklist** - [x] I've followed MetaMask Contributor Docs and MetaMask Mobile Coding Standards. - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable (N/A) - [x] I've documented my code using JSDoc format if applicable (N/A) - [x] I've applied the right labels on the PR (see labeling guidelines). Not required for external contributors. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Low risk: documentation-only change that updates testing guidance without affecting runtime behavior. > > **Overview** > Updates `.cursor/rules/unit-testing-guidelines.mdc` to add **Theme Mocking Rules** that steer tests toward using the shared `mockTheme` and away from hardcoded hex design tokens (to align with `@metamask/design-tokens/color-no-hex`). > > Adds concrete mock examples for `useTheme`, `useStyles`, and `useTailwind()` (including the correct return shape), plus an explicit anti-pattern example of local hex color mock objects. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 6227068. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 75bef55 commit 025e1ef

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

.cursor/rules/unit-testing-guidelines.mdc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,66 @@ See [PR #25548](https://github.com/MetaMask/metamask-mobile/pull/25548) for refa
234234
- **Mock all external dependencies** including APIs, services, hooks
235235
- **Use realistic mock data** that reflects real usage
236236

237+
### Theme Mocking Rules
238+
239+
- **Prefer shared `mockTheme` from `app/util/theme`** instead of hard-coded color literals in tests.
240+
- **Never hardcode design token hex values** in assertions or theme mocks (enforced by `@metamask/design-tokens/color-no-hex`).
241+
- **Avoid local hex color objects** for `useTheme`, `useStyles`, or tailwind mock color functions.
242+
- **If a test only needs a specific theme field**, derive it from `mockTheme` (or spread `mockTheme` and override minimally).
243+
244+
```ts
245+
// ✅ CORRECT: use shared mockTheme for useTheme mocks
246+
import { mockTheme } from '../../util/theme';
247+
248+
jest.mock('../../util/theme', () => ({
249+
useTheme: () => mockTheme,
250+
}));
251+
```
252+
253+
```ts
254+
// ✅ CORRECT: return { styles, theme } for useStyles mocks
255+
import { mockTheme } from '../../util/theme';
256+
257+
jest.mock('../../../../../component-library/hooks', () => ({
258+
useStyles: jest.fn((styleFn, vars) => ({
259+
styles: styleFn({ theme: mockTheme, vars }),
260+
theme: mockTheme,
261+
})),
262+
}));
263+
```
264+
265+
```ts
266+
// ✅ CORRECT: mock useTailwind with the right shape
267+
jest.mock('@metamask/design-system-twrnc-preset', () => ({
268+
useTailwind: () => ({
269+
// Most components only need tw.style(...)
270+
style: jest.fn(() => ({})),
271+
}),
272+
}));
273+
```
274+
275+
```ts
276+
// ✅ ALSO CORRECT: match real useTailwind() return type (callable function + helpers)
277+
import { mockTheme } from '../../util/theme';
278+
279+
jest.mock('@metamask/design-system-twrnc-preset', () => ({
280+
useTailwind: () => {
281+
const tw = () => ({});
282+
tw.style = jest.fn(() => ({}));
283+
return tw;
284+
},
285+
}));
286+
```
287+
288+
```ts
289+
// ❌ AVOID: local hex color mocks
290+
const mockColors = {
291+
text: { default: '#000000' },
292+
background: { default: '#FFFFFF' },
293+
border: { muted: '#E5E7EB' },
294+
};
295+
```
296+
237297
```ts
238298
// ✅ CORRECT
239299
import { apiService } from '../services/api';

0 commit comments

Comments
 (0)