Skip to content

Commit 3aa3982

Browse files
test: reverts the patch in favor or Playwright-only Node shim
1 parent 404249b commit 3aa3982

8 files changed

Lines changed: 133 additions & 62 deletions

File tree

.yarn/patches/@ethereumjs-util-npm-9.1.0-7e85509408.patch

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ diff --git a/dist/cjs/account.js b/dist/cjs/account.js
22
index 9c7b96d50a1e1e9a08463e0be74f1462576b8b53..04f390b50c11a10b20971bccb46d212978c2ac89 100644
33
--- a/dist/cjs/account.js
44
+++ b/dist/cjs/account.js
5-
@@ -476,7 +476,9 @@ const pubToAddress = function (pubKey, sanitize = false) {
5+
@@ -476,7 +476,7 @@ const pubToAddress = function (pubKey, sanitize = false) {
66
// Only take the lower 160bits of the hash
77
return (0, keccak_js_1.keccak256)(pubKey).subarray(-20);
88
};
99
-exports.pubToAddress = pubToAddress;
10-
+let _nativeUtils;
11-
+try { _nativeUtils = require('@metamask/native-utils'); } catch (_) {}
12-
+exports.pubToAddress = (_nativeUtils && _nativeUtils.pubToAddress) || pubToAddress;
10+
+exports.pubToAddress = require('@metamask/native-utils').pubToAddress;
1311
exports.publicToAddress = exports.pubToAddress;
1412
/**
1513
* Returns the ethereum public key of a given private key.

.yarn/patches/@metamask-key-tree-npm-10.1.1-0bfab435ac.patch

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ diff --git a/dist/curves/ed25519.cjs b/dist/curves/ed25519.cjs
22
index 3f6b0951c046dbda89f18edb7f17e6d23b839fc5..d2aee95598d942c0219128a77f6f83abdf206b80 100644
33
--- a/dist/curves/ed25519.cjs
44
+++ b/dist/curves/ed25519.cjs
5-
@@ -14,6 +14,8 @@ const isValidPrivateKey = (_privateKey) => true;
5+
@@ -14,6 +14,7 @@ const isValidPrivateKey = (_privateKey) => true;
66
exports.isValidPrivateKey = isValidPrivateKey;
77
exports.deriveUnhardenedKeys = false;
88
exports.publicKeyLength = 33;
9-
+let _nativeUtils;
10-
+try { _nativeUtils = require('@metamask/native-utils'); } catch (_) {}
9+
+const nativeUtils = require('@metamask/native-utils')
1110
const getGetPublicKey = () => {
1211
let hasSetWindowSize = false;
1312
const getPublicKey = (privateKey, _compressed) => {
14-
@@ -21,7 +23,7 @@ const getGetPublicKey = () => {
13+
@@ -21,7 +22,7 @@ const getGetPublicKey = () => {
1514
ed25519_1.ed25519.ExtendedPoint.BASE._setWindowSize(4);
1615
hasSetWindowSize = true;
1716
}
1817
- const publicKey = ed25519_1.ed25519.getPublicKey(privateKey);
19-
+ const publicKey = (_nativeUtils && _nativeUtils.getPublicKeyEd25519) ? _nativeUtils.getPublicKeyEd25519(privateKey) : ed25519_1.ed25519.getPublicKey(privateKey);
18+
+ const publicKey = nativeUtils.getPublicKeyEd25519(privateKey);
2019
return (0, utils_1.concatBytes)([new Uint8Array([0]), publicKey]);
2120
};
2221
return getPublicKey;

tests/docs/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [E2E Framework Structure](#e2e-framework-structure)
66
- [Playwright: local emulator `buildPath` and app install](PLAYWRIGHT_LOCAL_EMULATOR.md)
77
- [E2E Testing Best Practices](#e2e-testing-best-practices)
8+
- [Playwright Node Runtime Shims](#playwright-node-runtime-shims)
89
- [E2E Test Examples and Patterns](#e2e-test-examples-and-patterns)
910
- [E2E Testing Anti-Patterns (AVOID THESE)](#e2e-testing-anti-patterns-avoid-these)
1011
- [E2E Code Review Checklist](#e2e-code-review-checklist)
@@ -90,6 +91,15 @@ await withFixtures(
9091
- Default mocks are loaded from `tests/api-mocking/mock-responses/defaults/`
9192
- Feature flags mocked via `setupRemoteFeatureFlagsMock` helper
9293

94+
## Playwright Node Runtime Shims
95+
96+
Playwright specs and `FixtureBuilder` run in Node, while the app runs in React Native/Metro. If a native-only module fails during Playwright import or fixture setup, keep the app on the native implementation and isolate the Node workaround in test infrastructure.
97+
98+
- Use `tests/framework/nodeNativeUtilsShim.cjs` for `@metamask/native-utils` in Playwright's Node process.
99+
- Register Node shims from Playwright framework entrypoints before importing code that may load native modules.
100+
- Do not add JS fallbacks to shared dependency patches unless the app runtime also needs them.
101+
- Validate with `yarn playwright test --list --project android --config tests/playwright.config.ts` before running devices.
102+
93103
**Element State Configuration:**
94104

95105
```typescript

tests/framework/config/ConfigHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable import-x/no-nodejs-modules */
2+
import '../nodeNativeUtilsShim.cjs';
23
import path from 'path';
34
import {
45
defineConfig as defineConfigPlaywright,

tests/framework/fixture/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../nodeNativeUtilsShim.cjs';
12
import { test as base, type FullProject } from '@playwright/test';
23
import {
34
WebDriverConfig,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-disable import-x/no-commonjs, import-x/no-nodejs-modules */
2+
/* global globalThis */
3+
/*
4+
* Playwright runs FixtureBuilder in Node, but the mobile app resolves
5+
* @metamask/native-utils through React Native/Metro. The native package is ESM
6+
* and depends on react-native-nitro-modules, so CommonJS dependency patches
7+
* cannot require it in the Playwright process. This shim only affects Node's
8+
* CommonJS loader in the current process.
9+
*/
10+
const Module = require('module');
11+
const { secp256k1 } = require('@noble/curves/secp256k1');
12+
const { ed25519 } = require('@noble/curves/ed25519');
13+
const { hmac } = require('@noble/hashes/hmac');
14+
const { sha512 } = require('@noble/hashes/sha2');
15+
const { keccak_256: keccak256 } = require('@noble/hashes/sha3');
16+
17+
const registerKey = Symbol.for('metamask.playwright.nativeUtilsNodeShim');
18+
19+
const nativeUtilsMock = {
20+
getPublicKey: secp256k1.getPublicKey,
21+
getPublicKeyEd25519: ed25519.getPublicKey,
22+
hmacSha512: (key, data) => hmac(sha512, key, data),
23+
keccak256,
24+
multiply: (a, b) => a * b,
25+
pubToAddress: (pubKey, sanitize = false) => {
26+
let key = pubKey;
27+
28+
if (sanitize && pubKey.length !== 64) {
29+
key = secp256k1.ProjectivePoint.fromHex(pubKey)
30+
.toRawBytes(false)
31+
.slice(1);
32+
}
33+
34+
if (key.length !== 64) {
35+
throw new Error('Expected pubKey to be of length 64');
36+
}
37+
38+
return keccak256(key).subarray(-20);
39+
},
40+
};
41+
42+
const existingRegistration = globalThis[registerKey];
43+
44+
if (existingRegistration) {
45+
module.exports = existingRegistration.nativeUtilsMock;
46+
} else {
47+
const originalLoad = Module._load;
48+
49+
Module._load = function loadWithNativeUtilsShim(request, parent, isMain) {
50+
if (request === '@metamask/native-utils') {
51+
return nativeUtilsMock;
52+
}
53+
54+
return originalLoad.call(this, request, parent, isMain);
55+
};
56+
57+
globalThis[registerKey] = {
58+
nativeUtilsMock,
59+
};
60+
61+
module.exports = nativeUtilsMock;
62+
}

tests/performance/fixtures/test.spec.ts

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,59 @@ import { remoteFeatureFlagHomepageSectionsV1Enabled } from '../../api-mocking/mo
1212
import { RampsRegions, RampsRegionsEnum } from '../../framework/Constants';
1313
import { LocalNodeType } from '../../framework';
1414
import { Hardfork } from '../../seeder/anvil-manager';
15+
import { Performance } from '../../tags.performance';
1516

1617
/* Dummy test to test fixtures */
17-
test.skip('Test Fixtures', async ({
18-
currentDeviceDetails,
19-
driver,
20-
}, testInfo) => {
21-
await withFixtures(
22-
{
23-
fixture: new FixtureBuilder()
24-
.withPerpsProfile('no-positions')
25-
.withPerpsFirstTimeUser(false)
26-
.withNetworkController({
27-
type: 'rpc',
28-
chainId: '0xa4b1',
29-
rpcUrl: 'https://arb1.arbitrum.io/rpc',
30-
nickname: 'Arbitrum One',
31-
ticker: 'ETH',
32-
})
33-
.withTokensForAllPopularNetworks([
18+
test.describe(Performance, () => {
19+
test('Test Fixtures', async ({ currentDeviceDetails, driver }, testInfo) => {
20+
await withFixtures(
21+
{
22+
fixture: new FixtureBuilder()
23+
.withPerpsProfile('no-positions')
24+
.withPerpsFirstTimeUser(false)
25+
.withNetworkController({
26+
type: 'rpc',
27+
chainId: '0xa4b1',
28+
rpcUrl: 'https://arb1.arbitrum.io/rpc',
29+
nickname: 'Arbitrum One',
30+
ticker: 'ETH',
31+
})
32+
.withTokensForAllPopularNetworks([
33+
{
34+
address: '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8',
35+
symbol: 'USDC',
36+
decimals: 6,
37+
name: 'USD Coin',
38+
type: 'erc20',
39+
},
40+
])
41+
.withPopularNetworks()
42+
.build(),
43+
restartDevice: true,
44+
currentDeviceDetails,
45+
testSpecificMock: async (mockServer: Mockttp) => {
46+
await setupRemoteFeatureFlagsMock(mockServer, {
47+
...remoteFeatureFlagHomepageSectionsV1Enabled(),
48+
});
49+
await PERPS_ARBITRUM_MOCKS(mockServer);
50+
await mockPerpsGeolocation(
51+
mockServer,
52+
RampsRegions[RampsRegionsEnum.SPAIN],
53+
);
54+
},
55+
localNodeOptions: [
3456
{
35-
address: '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8',
36-
symbol: 'USDC',
37-
decimals: 6,
38-
name: 'USD Coin',
39-
type: 'erc20',
57+
type: LocalNodeType.anvil,
58+
options: { hardfork: 'prague' as Hardfork },
4059
},
41-
])
42-
.withPopularNetworks()
43-
.build(),
44-
restartDevice: true,
45-
currentDeviceDetails,
46-
testSpecificMock: async (mockServer: Mockttp) => {
47-
await setupRemoteFeatureFlagsMock(mockServer, {
48-
...remoteFeatureFlagHomepageSectionsV1Enabled(),
49-
});
50-
await PERPS_ARBITRUM_MOCKS(mockServer);
51-
await mockPerpsGeolocation(
52-
mockServer,
53-
RampsRegions[RampsRegionsEnum.SPAIN],
54-
);
60+
],
5561
},
56-
localNodeOptions: [
57-
{
58-
type: LocalNodeType.anvil,
59-
options: { hardfork: 'prague' as Hardfork },
60-
},
61-
],
62-
},
63-
async () => {
64-
console.log('currentDeviceDetails', currentDeviceDetails);
65-
await loginToAppPlaywright({ scenarioType: 'e2e' });
62+
async () => {
63+
console.log('currentDeviceDetails', currentDeviceDetails);
64+
await loginToAppPlaywright({ scenarioType: 'e2e' });
6665

67-
// Add any actions you may want to test.
68-
},
69-
);
66+
// Add any actions you may want to test.
67+
},
68+
);
69+
});
7070
});

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,11 +3034,11 @@ __metadata:
30343034

30353035
"@ethereumjs/util@patch:@ethereumjs/util@npm%3A9.1.0#~/.yarn/patches/@ethereumjs-util-npm-9.1.0-7e85509408.patch":
30363036
version: 9.1.0
3037-
resolution: "@ethereumjs/util@patch:@ethereumjs/util@npm%3A9.1.0#~/.yarn/patches/@ethereumjs-util-npm-9.1.0-7e85509408.patch::version=9.1.0&hash=2510bd"
3037+
resolution: "@ethereumjs/util@patch:@ethereumjs/util@npm%3A9.1.0#~/.yarn/patches/@ethereumjs-util-npm-9.1.0-7e85509408.patch::version=9.1.0&hash=f1f3d0"
30383038
dependencies:
30393039
"@ethereumjs/rlp": "npm:^5.0.2"
30403040
ethereum-cryptography: "npm:^2.2.1"
3041-
checksum: 10/5080e01285d05c8eb99b35df6940b44f1609bb56f6c71365d6f31dc5173b8e202d99e89c469ffa757aa5cdc577344051d5b8a43b3c4e12a29fface2d42f73eb5
3041+
checksum: 10/ab8e9ff226989daf026de6297932598f180de2306b6d059db628c1fdb5338d36dfa622a94c7a4d0f768a95975c31e526c5b12837ab6d1c322ce5d6888d40b398
30423042
languageName: node
30433043
linkType: hard
30443044

@@ -8913,14 +8913,14 @@ __metadata:
89138913

89148914
"@metamask/key-tree@patch:@metamask/key-tree@npm%3A10.1.1#~/.yarn/patches/@metamask-key-tree-npm-10.1.1-0bfab435ac.patch":
89158915
version: 10.1.1
8916-
resolution: "@metamask/key-tree@patch:@metamask/key-tree@npm%3A10.1.1#~/.yarn/patches/@metamask-key-tree-npm-10.1.1-0bfab435ac.patch::version=10.1.1&hash=7ecdc9"
8916+
resolution: "@metamask/key-tree@patch:@metamask/key-tree@npm%3A10.1.1#~/.yarn/patches/@metamask-key-tree-npm-10.1.1-0bfab435ac.patch::version=10.1.1&hash=40cbda"
89178917
dependencies:
89188918
"@metamask/scure-bip39": "npm:^2.1.1"
89198919
"@metamask/utils": "npm:^11.0.1"
89208920
"@noble/curves": "npm:^1.8.1"
89218921
"@noble/hashes": "npm:^1.3.2"
89228922
"@scure/base": "npm:^1.0.0"
8923-
checksum: 10/5af2d0347334a477ba6c00f29aece8308219ac339f919fa9ed26070cc1fab34b56e52ffb7b9c9e24942607204cf2c1465b92baea1904577e684401d7a6bdce4b
8923+
checksum: 10/27e41df10066976063d91cfa66fa5dd2c9e460afd12e79550f72eb94ddcd9d3b6603922a57614e128dfefc14f93dcfbf493067e4a20ae57a754516ca4ba3834d
89248924
languageName: node
89258925
linkType: hard
89268926

0 commit comments

Comments
 (0)