test: CV - add nock and cleanup Trending CV services mocks#27412
Conversation
…update-trending-cv-test-mocks
|
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. |
| import { | ||
| describeForPlatforms, | ||
| itForPlatforms, | ||
| } from '../../../util/test/platform'; |
There was a problem hiding this comment.
Cleanup - we don't need both describeForPlatforms and itForPlatforms, it runs ios and android tests twice.
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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)(...)| // 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([]), | ||
| }; | ||
| }); |
There was a problem hiding this comment.
This has now been replaced with nock
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
|
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:
|
There was a problem hiding this comment.
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.
| const TRENDING_API_BASE = 'https://token.api.cx.metamask.io'; | ||
| const TRENDING_PATH = '/v3/tokens/trending'; | ||
|
|
||
| export interface MockTrendingToken { |
There was a problem hiding this comment.
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)| nock.cleanAll(); | ||
| nock.disableNetConnect(); |
There was a problem hiding this comment.
Clean run, and we also will reject/throw if there are other network requests made (so we can capture and update)
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
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: |
|
@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 |
|
racitores
left a comment
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
We should move selector Ids to the corresponding testIds file app/components/Views/TrendingView/TrendingView.testIds.ts
There was a problem hiding this comment.
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).
| import { | ||
| describeForPlatforms, | ||
| itForPlatforms, | ||
| } from '../../../util/test/platform'; |
There was a problem hiding this comment.
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 👍
| @@ -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(); | |||
| }); | |||
| }); | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.



Description
We have a few areas in the app that perform fetch requests outside of the engine, by using
coreservices. Some examples:This PR adds the
nockmock API requests library that we use for unit tests inMetaMask/coreandMetaMask/metamask-extension.Changelog
CHANGELOG entry: test: CV - add
nockand cleanup Trending CV services mocksRelated issues
Fixes: #26270 https://consensyssoftware.atlassian.net/browse/ASSETS-2734
Manual testing steps
Screenshots/Recordings
Before
After
Pre-merge author checklist
Pre-merge reviewer checklist
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 vianock, making the tests exercise the real fetch path.Updates
tests/component-view/mocks/trendingApiMocks.tsto define nock-based helpers (including optional per-request reply logic for chain filtering), addsnocktodevDependencies, and simplifies the Trending view tests accordingly (removingitForPlatforms/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.