Skip to content

test: CV - add nock and cleanup Trending CV services mocks#27412

Merged
Prithpal-Sooriya merged 3 commits into
mainfrom
test/update-trending-cv-test-mocks
Mar 16, 2026
Merged

test: CV - add nock and cleanup Trending CV services mocks#27412
Prithpal-Sooriya merged 3 commits into
mainfrom
test/update-trending-cv-test-mocks

Conversation

@Prithpal-Sooriya

@Prithpal-Sooriya Prithpal-Sooriya commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

Description

We have a few areas in the app that perform fetch requests outside of the engine, by using core services. Some examples:

  • Perps
  • Predict
  • Explore

This PR adds the nock mock API requests library that we use for unit tests in MetaMask/core and MetaMask/metamask-extension.

  • It allows us to strictly mock API calls and not the whole core service, for more realistic integration tests.

Changelog

CHANGELOG entry: test: CV - add nock and cleanup Trending CV services mocks

Related issues

Fixes: #26270 https://consensyssoftware.atlassian.net/browse/ASSETS-2734

Manual testing steps

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

Before

After

Pre-merge author checklist

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.

Note

Low Risk
Test-only changes that swap service-function mocking for HTTP interception; risk is limited to potential flakiness/over-blocking of network calls in view tests if nock isn’t cleaned up correctly.

Overview
Switches Trending component-view tests from mocking @metamask/assets-controllers (getTrendingTokens) to mocking the actual trending HTTP endpoint via nock, making the tests exercise the real fetch path.

Updates tests/component-view/mocks/trendingApiMocks.ts to define nock-based helpers (including optional per-request reply logic for chain filtering), adds nock to devDependencies, and simplifies the Trending view tests accordingly (removing itForPlatforms/controller mocks and using request-URL-based responses for the BNB filter case).

Written by Cursor Bugbot for commit fc3106d. This will update automatically on new commits. Configure here.

@github-actions

Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

Comment on lines -2 to -5
import {
describeForPlatforms,
itForPlatforms,
} from '../../../util/test/platform';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup - we don't need both describeForPlatforms and itForPlatforms, it runs ios and android tests twice.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is made in case 1 specific test can't be run in 1 os... so far never happened. Beside that, the idea was that the framework was clever enough to run the tests just once independently of how it is used. So I'll have a look to fix that 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw I do like "test tables" by using it.each. I find that it helps clean up some test repitition.
If the above itForPlatforms can support this, amazing, otherwise no worries :)

E.g.

const testCases = [
  { ... },
  { ... },
]

it.each(testCases)(...)

Comment on lines -25 to -36
// TODO: Anti-pattern — only Engine and native modules should be mocked here.
// getTrendingTokens is a standalone service function called directly from
// components, not through a controller on Engine.
// https://github.com/MetaMask/metamask-mobile/issues/26270
// eslint-disable-next-line no-restricted-syntax
jest.mock('@metamask/assets-controllers', () => {
const actual = jest.requireActual('@metamask/assets-controllers');
return {
...actual,
getTrendingTokens: jest.fn().mockResolvedValue([]),
};
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has now been replaced with nock

@socket-security

socket-security Bot commented Mar 12, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addednpm/​nock@​14.0.119810010098100

View full report

@socket-security

socket-security Bot commented Mar 12, 2026

Copy link
Copy Markdown

All alerts resolved. Learn more about Socket for GitHub.

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

Ignoring alerts on:

  • npm/nock@14.0.11

View full report

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Comment thread tests/component-view/mocks/trendingApiMocks.ts Outdated
Comment thread tests/component-view/mocks/trendingApiMocks.ts
const TRENDING_API_BASE = 'https://token.api.cx.metamask.io';
const TRENDING_PATH = '/v3/tokens/trending';

export interface MockTrendingToken {

@Prithpal-Sooriya Prithpal-Sooriya Mar 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved mock to match API.

NOTE - I think we should follow the same pattern I did for notifications core to expose mocks. That way QA and engineers do not need to re-invent the mocks from scratch.

An example idea:

import { createMockTrendingAPIResponse } from  '@metamask/assets-controllers/mocks'

const myMockResponse = createMockTrendingAPIResponse(...)
nock('....').reply(200, myMockResponse)

Comment on lines +89 to +90
nock.cleanAll();
nock.disableNetConnect();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean run, and we also will reject/throw if there are other network requests made (so we can capture and update)

@github-actions

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: None (no tests recommended)
  • Selected Performance tags: None (no tests recommended)
  • Risk Level: low
  • AI Confidence: 92%
click to see 🤖 AI reasoning details

E2E Test Selection:
All changes are limited to test infrastructure and dependencies:

  • package.json: Adds nock as a dependency for HTTP mocking in tests.
  • tests/component-view/mocks/trendingApiMocks.ts: Refactors mocks to use nock to intercept real HTTP requests instead of mocking getTrendingTokens directly.
  • Related changes are confined to component-view tests and mock data structure adjustments.

There are no changes to app runtime code (no modifications in app/, controllers, Engine, UI components used in production, navigation, or business logic). Therefore, no user-facing flows, confirmations, network management, trading, identity, or wallet platform features are impacted.

Because the modifications are strictly within test utilities and component tests, running Detox E2E smoke suites is not necessary for risk mitigation.

No dependent tags (e.g., SmokeWalletPlatform for Trending) are required because the actual Trending implementation code was not modified—only its test mocks were updated.

Performance Test Selection:
No production code affecting rendering, state management, Engine initialization, network calls, or critical user flows was changed. The updates are limited to test mocks and dev dependencies, so there is no potential impact on runtime performance.

View GitHub Actions results

@Prithpal-Sooriya

Copy link
Copy Markdown
Contributor Author

@SocketSecurity ignore npm/nock@14.0.11

This library is used for testing purposes only (added to dev-dependencies). It is used to capture HTTP requests and intercept and mock requests/responses.

We also use nock for testing in both MetaMask/core and MetaMask/metamask-extension

@sonarqubecloud

Copy link
Copy Markdown

@racitores racitores left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Only some small comments that, if you want I can fix in another PR where I'll move nock to the test framework
BTW, thanks a lot for actively contributing with the framework 🚀 🙏


const viewAllButton = getByTestId('section-header-view-all-tokens');
await actButtonPress(viewAllButton);
const viewAllButton = getByTestId('section-header-view-all-tokens');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move selector Ids to the corresponding testIds file app/components/Views/TrendingView/TrendingView.testIds.ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please to the same with
Raw strings used: 'explore-view-search-button', 'trending-tokens-header', 'all-networks-button', 'close-button', 'trending-tokens-header-search-toggle', 'trending-tokens-header-search-bar'. All getByTestId / findByTestId / queryByTestId should use constants from TrendingView.testIds.ts (add missing IDs there or import from child .testIds.ts and re-export/use).

Comment on lines -2 to -5
import {
describeForPlatforms,
itForPlatforms,
} from '../../../util/test/platform';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is made in case 1 specific test can't be run in 1 os... so far never happened. Beside that, the idea was that the framework was clever enough to run the tests just once independently of how it is used. So I'll have a look to fix that 👍

Comment on lines 92 to 108
@@ -116,47 +99,44 @@ describeForPlatforms('ExploreFeed - Component Tests', () => {
});
});

itForPlatforms('renders Explore title on Explore screen', async () => {
it('renders Explore title on Explore screen', async () => {
const { getByText } = renderTrendingViewWithRoutes();

await waitFor(() => {
expect(getByText('Explore')).toBeOnTheScreen();
});
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aim that cv tests have driven by user interaction, meaning that we expect something more that just render (to avoid long test files with that just render screens)
I recommend to join these 3 test in one like Explore screen shows safe area, header, and title + something else (act). The act could be something like and user can open trending full view, etc.
If you prefer to have a test to check all renders, that is fine too, but the test should include all expects in one

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'm not a huge fan of render tests (unless the assertion is useful). Happy to remove these tests, or have them as assertions util.

@Prithpal-Sooriya Prithpal-Sooriya added this pull request to the merge queue Mar 16, 2026
Merged via the queue into main with commit f57e8e9 Mar 16, 2026
69 checks passed
@Prithpal-Sooriya Prithpal-Sooriya deleted the test/update-trending-cv-test-mocks branch March 16, 2026 14:10
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 16, 2026
@metamaskbot metamaskbot added the release-7.71.0 Issue or pull request that will be included in release 7.71.0 label Mar 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.71.0 Issue or pull request that will be included in release 7.71.0 size-M team-assets

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Component View Tests] Move getTrendingTokens behind a controller to eliminate direct service mock in component-view tests

4 participants