Repository navigation
test: fix flaky tests Perps Take Profit / Stop Loss simulates... - #47165
Conversation
✨ Files requiring CODEOWNER review ✨👨🔧 @MetaMask/perps (3 files, +50 -11)
🧪 @MetaMask/qa (1 files, +5 -0)
|
Builds ready [88a75c2] [reused from 7e4e984]
⚡ Performance Benchmarks (Total: 🟢 8 pass · 🟡 2 warn · 🔴 3 fail)
Bundle Size Diffs
|
Builds ready [5372edb] [reused from 7e4e984]
⚡ Performance Benchmarks (Total: 🟢 8 pass · 🟡 2 warn · 🔴 3 fail)
Bundle Size Diffs
|
Builds ready [60272d4] [reused from 6e15c0b]
⚡ Performance Benchmarks (Total: 🟢 8 pass · 🟡 3 warn · 🔴 2 fail)
Bundle Size Diffs
|
Builds ready [01bc3d0] [reused from 6e15c0b]
⚡ Performance Benchmarks (Total: 🟢 8 pass · 🟡 3 warn · 🔴 2 fail)
Bundle Size Diffs
|
Builds ready [3715803] [reused from 6e15c0b]
⚡ Performance Benchmarks (Total: 🟢 8 pass · 🟡 3 warn · 🔴 2 fail)
Bundle Size Diffs
|
Builds ready [a422401] [reused from 6e15c0b]
⚡ Performance Benchmarks (Total: 🟢 8 pass · 🟡 3 warn · 🔴 2 fail)
Bundle Size Diffs
|
Builds ready [a493083] [reused from 6e15c0b]
⚡ Performance Benchmarks (Total: 🟢 8 pass · 🟡 3 warn · 🔴 2 fail)
Bundle Size Diffs
|
Builds ready [45f9d12]
⚡ Performance Benchmarks (Total: 🟢 10 pass · 🟡 0 warn · 🔴 3 fail)
Bundle Size Diffs [🚨 Warning! Bundle size has increased!]
|
|



Description
This PR fixes two separate flakes in the Perps TP/SL and position-lifecycle E2E tests. Both come from mocked WebSocket messages being lost or overwritten.
The closed trade sometimes doesn't appear in Activity
Problem: A just-closed trade only reaches Activity through the live userFills push, because the history fetched from the server is cached for 60 seconds. The wallet can drop that single push while the test is leaving the market detail page.
Fix: perps-activity-close-fill.flow.ts pushes the fill again once Perps home is stable.
The position sometimes stays open after the mocked close
Problem: About 2.5 seconds after saving, the TP/SL modal refreshes positions. The mock always answered with the original open ETH long, so if that answer landed after the "position closed" push, the position came back.
Fix: The mocks now remember the last account state pushed by pushPositionUpdate or pushPositionClosed. Later subscribe follow-ups and both WebSocket and REST clearinghouseState reads return that state, as the real exchange would. It's reset for each test.
Changelog
CHANGELOG entry:
Related issues
Fixes:
Manual testing steps
Screenshots/Recordings
Before
After
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Low Risk
Test-only changes to E2E mocks and flows; no production wallet or trading logic.
Overview
Reduces flakes in Perps TP/SL and position-lifecycle E2E by aligning mocks and the Activity close-fill flow with how live fills and account state behave.
Activity after close:
assertPerpsActivityShowsCloseFillwaits for market detail to load, then re-pushesuserFillsonce Perps home is stable so a close row still appears when the first push was dropped during navigation (Activity relies on the live fill while REST history is cached).Position state after simulated close: Mocks now remember the last
clearinghouseStatefrompushPositionUpdate/pushPositionClosed. Later subscribe follow-ups, WSclearinghouseStatePOSTs, and the ETH-long REST fixture usegetCurrentClearinghouseState, so a refetch after close does not resurrect the open ETH long. State is cleared at the start of each Perps WS mock setup.Reviewed by Cursor Bugbot for commit 45f9d12. Bugbot is set up for automated code reviews on this repo. Configure here.