Skip to content

Commit 29f05cf

Browse files
authored
test: add lint rule to prevent new snapshot tests (#28582)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** > > Prevents new file-based Jest snapshots by enabling jest/no-restricted-matchers for *.test/*.spec files and explicitly disallowing toMatchSnapshot() (while still permitting toMatchInlineSnapshot()). > > Adds a targeted override that turns this restriction off for tests under **/snaps/** and **/Snaps/**, and includes eslint-plugin-jest as a new devDependency (lockfile updated accordingly). ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: ## **Related issues** Fixes: ## **Manual testing steps** ```gherkin Feature: my feature name Scenario: user [verb for user action] Given [describe expected initial app state] When user [verb for user action] Then [describe expected outcome] ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I've included tests if applicable - [ ] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Adds a new ESLint enforcement that will fail builds for any remaining/new `toMatchSnapshot()` usage in `*.test`/`*.spec` files, which could cause unexpected CI failures until all tests comply. Code changes are otherwise limited to test assertions and dev tooling. > > **Overview** > **Prevents new file-based Jest snapshots** by enabling `eslint-plugin-jest` and enforcing `jest/no-restricted-matchers` to disallow `toMatchSnapshot()` across `*.test`/`*.spec` files, while explicitly exempting tests under `**/snaps/**` and `**/Snaps/**`. > > Refactors a handful of existing tests (e.g., `BadgeWrapper`, `Main`, `AssetElement`, `SettingsNotification`, `StyledButton`, `TabCountIcon`, `Root`) to replace snapshot assertions with targeted `@testing-library/react-native` queries, and removes the corresponding snapshot files (including deleting the `TokenImage` snapshot test entirely). > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 728a801. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent d5c2f24 commit 29f05cf

21 files changed

Lines changed: 84 additions & 2604 deletions

File tree

.eslintrc.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,33 @@ module.exports = {
385385
],
386386
},
387387
},
388+
{
389+
files: ['**/*.test.{js,ts,tsx,jsx}', '**/*.spec.{js,ts,tsx,jsx}'],
390+
plugins: ['jest'],
391+
rules: {
392+
// Prevent new file-based snapshots. Inline snapshots (toMatchInlineSnapshot)
393+
// are still allowed as they keep assertions co-located with the test.
394+
'jest/no-restricted-matchers': [
395+
'error',
396+
{
397+
toMatchSnapshot:
398+
'Use toMatchInlineSnapshot() or an explicit assertion instead. File-based snapshots are being phased out.',
399+
},
400+
],
401+
},
402+
},
403+
{
404+
// Matches CODEOWNERS `**/snaps/**` and `**/Snaps/**` (@MetaMask/core-platform).
405+
// ESLint cannot read CODEOWNERS.
406+
files: [
407+
'**/snaps/**/*.{test,spec}.{js,ts,tsx,jsx}',
408+
'**/Snaps/**/*.{test,spec}.{js,ts,tsx,jsx}',
409+
],
410+
plugins: ['jest'],
411+
rules: {
412+
'jest/no-restricted-matchers': 'off',
413+
},
414+
},
388415
// ── Perps controller Core-alignment override ──
389416
// Enforces the same ESLint rules that Core's @metamask/eslint-config
390417
// applies to packages/perps-controller so that code written in mobile

app/component-library/components/Badges/BadgeWrapper/BadgeWrapper.test.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ import React from 'react';
33
import { render, screen } from '@testing-library/react-native';
44

55
// Internal dependencies.
6+
import { BADGENETWORK_TEST_ID } from '../Badge/variants/BadgeNetwork/BadgeNetwork.constants';
67
import BadgeWrapper from './BadgeWrapper';
78
import {
89
SAMPLE_BADGEWRAPPER_PROPS,
910
BADGE_WRAPPER_BADGE_TEST_ID,
1011
} from './BadgeWrapper.constants';
1112

1213
describe('BadgeWrapper', () => {
13-
it('should render BadgeWrapper correctly', () => {
14-
const { toJSON } = render(<BadgeWrapper {...SAMPLE_BADGEWRAPPER_PROPS} />);
15-
expect(toJSON()).toMatchSnapshot();
16-
expect(screen.getByTestId(BADGE_WRAPPER_BADGE_TEST_ID)).toBeDefined();
14+
it('renders anchor content, network badge, and wrapper test id', () => {
15+
render(<BadgeWrapper {...SAMPLE_BADGEWRAPPER_PROPS} />);
16+
17+
expect(screen.getByTestId(BADGE_WRAPPER_BADGE_TEST_ID)).toBeOnTheScreen();
18+
expect(screen.getByText('C')).toBeOnTheScreen();
19+
expect(screen.getByTestId(BADGENETWORK_TEST_ID)).toBeOnTheScreen();
20+
expect(screen.getByTestId('network-avatar-image')).toBeOnTheScreen();
1721
});
1822
});

app/component-library/components/Badges/BadgeWrapper/__snapshots__/BadgeWrapper.test.tsx.snap

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)