From f6890a711ae9c834cbb360bad2ea5959a9f90389 Mon Sep 17 00:00:00 2001 From: Danica Shen Date: Tue, 18 Jun 2024 14:14:06 +0100 Subject: [PATCH] fix(25335): fix flaky test "Unlock wallet should send first three Page metric events upon fullscreen page load" (#25351) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** The flakyness comes from unordered events from time to time. Based on the log of events when unlock wallet: 截屏2024-06-14 22 33 12 The `events` array is not sorted, and there is nothing wrong with metrics itself and the order of sending, which can be validated by `timestamp`. Therefore the fix is to order events by `timestamp` and assert the value. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/25351?quickstart=1) ## **Related issues** Fixes: https://github.com/MetaMask/metamask-extension/issues/25335 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.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-extension/blob/develop/.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. --- test/e2e/tests/metrics/unlock-wallet.spec.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/test/e2e/tests/metrics/unlock-wallet.spec.js b/test/e2e/tests/metrics/unlock-wallet.spec.js index 3961bb3a94ce..2e4b9f5ed3eb 100644 --- a/test/e2e/tests/metrics/unlock-wallet.spec.js +++ b/test/e2e/tests/metrics/unlock-wallet.spec.js @@ -40,15 +40,26 @@ describe('Unlock wallet', function () { await unlockWallet(driver); await waitForAccountRendered(driver); const events = await getEventPayloads(driver, mockedEndpoint); - assert.equal(events.length, 3); - assertBatchValue(events[0], 'Home', '/'); - assertBatchValue(events[1], 'Unlock Page', '/unlock'); - assertBatchValue(events[2], 'Home', '/'); + const sortedEvents = sortEventsByTime(events); + await assert.equal(sortedEvents.length, 3); + assertBatchValue(sortedEvents[0], 'Home', '/'); + assertBatchValue(sortedEvents[1], 'Unlock Page', '/unlock'); + assertBatchValue(sortedEvents[2], 'Home', '/'); }, ); }); }); +function sortEventsByTime(events) { + events.sort((event1, event2) => { + const timestamp1 = new Date(event1.timestamp); + const timestamp2 = new Date(event2.timestamp); + // Compare timestamps, return -1 for earlier, 1 for later, 0 for equal + return timestamp1 - timestamp2; + }); + return events; +} + function assertBatchValue(event, assertedTitle, assertedPath) { const { title, path } = event.context.page; assert.equal(title, assertedTitle);