Skip to content

Commit 42cc74e

Browse files
Copilot0xrinegade
andcommitted
Fix Jest mock factory out-of-scope variable errors
- Fixed Buffer import in setupTests.js by moving it inside jest.mock() factory for @solana/web3.js - Fixed Connection import in App.test.tsx by moving it inside jest.mock() factory - Added missing clusterApiUrl mock function to @solana/web3.js mock - Added comprehensive mocks for Ledger hardware wallet libraries - Added mock for @project-serum/serum to prevent initialization issues - Made clipboard API configurable to prevent redefinition errors - All Jest mock factories now comply with out-of-scope variable restrictions Co-authored-by: 0xrinegade <[email protected]>
1 parent 2e5b3ac commit 42cc74e

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

src/App.test.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react';
22
import { render, screen, waitFor } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
44
import { MemoryRouter } from 'react-router-dom';
5-
import { Connection } from '@solana/web3.js';
65
import App from './App';
76
import { ConnectionProvider } from './utils/connection';
87
import { WalletProvider } from './utils/wallet';
@@ -21,10 +20,13 @@ jest.mock('./utils/connection', () => ({
2120
ConnectionProvider: ({ children }: { children: React.ReactNode }) => (
2221
<div data-testid="connection-provider">{children}</div>
2322
),
24-
useConnection: () => ({
25-
connection: new Connection('https://api.devnet.solana.com'),
26-
endpoint: 'https://api.devnet.solana.com',
27-
}),
23+
useConnection: () => {
24+
const { Connection } = require('@solana/web3.js'); // Move import inside mock factory
25+
return {
26+
connection: new Connection('https://api.devnet.solana.com'),
27+
endpoint: 'https://api.devnet.solana.com',
28+
};
29+
},
2830
}));
2931

3032
// Mock wallet

src/setupTests.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// expect(element).toHaveTextContent(/react/i)
44
// learn more: https://github.com/testing-library/jest-dom
55
import '@testing-library/jest-dom';
6-
import { Buffer } from 'buffer';
76

87
// Polyfill for TextEncoder/TextDecoder in Jest
98
global.TextEncoder = require('util').TextEncoder;
@@ -32,6 +31,7 @@ Object.defineProperty(navigator, 'clipboard', {
3231
writeText: jest.fn(() => Promise.resolve()),
3332
readText: jest.fn(() => Promise.resolve('')),
3433
},
34+
configurable: true, // Allow reconfiguration for tests
3535
});
3636

3737
// Mock ResizeObserver
@@ -92,15 +92,15 @@ jest.mock('scrypt-js', () => jest.fn((password, salt, N, r, p, keylen, callback)
9292
callback(null, new Uint8Array(keylen));
9393
}));
9494
jest.mock('@solana/web3.js', () => {
95-
const mockBuffer = require('buffer').Buffer;
95+
const { Buffer } = require('buffer'); // Move Buffer import inside mock factory
9696

9797
const mockPublicKey = {
9898
toBase58: jest.fn(() => 'mock-public-key'),
9999
toString: jest.fn(() => 'mock-public-key'),
100100
equals: jest.fn(() => false),
101101
toJSON: jest.fn(() => 'mock-public-key'),
102102
toBytes: jest.fn(() => new Uint8Array(32)),
103-
toBuffer: jest.fn(() => mockBuffer.alloc(32)),
103+
toBuffer: jest.fn(() => Buffer.alloc(32)),
104104
isOnCurve: jest.fn(() => true),
105105
};
106106

@@ -126,10 +126,46 @@ jest.mock('@solana/web3.js', () => {
126126
SystemProgram: {
127127
transfer: jest.fn(() => ({})),
128128
},
129+
clusterApiUrl: jest.fn((cluster) => `https://api.${cluster}.solana.com`),
129130
LAMPORTS_PER_SOL: 1000000000,
130131
};
131132
});
132133

134+
// Mock Ledger hardware wallet libraries to avoid hardware-specific dependencies
135+
jest.mock('@ledgerhq/hw-transport-webhid', () => ({
136+
__esModule: true,
137+
default: jest.fn().mockImplementation(() => ({
138+
getPublicKey: jest.fn(() => Promise.resolve('mock-ledger-public-key')),
139+
signTransaction: jest.fn(() => Promise.resolve('mock-signature')),
140+
close: jest.fn(() => Promise.resolve()),
141+
})),
142+
}));
143+
144+
jest.mock('@ledgerhq/hw-transport-webusb', () => ({
145+
__esModule: true,
146+
default: jest.fn().mockImplementation(() => ({
147+
getPublicKey: jest.fn(() => Promise.resolve('mock-ledger-public-key')),
148+
signTransaction: jest.fn(() => Promise.resolve('mock-signature')),
149+
close: jest.fn(() => Promise.resolve()),
150+
})),
151+
}));
152+
153+
// Mock @project-serum/serum to avoid initialization issues
154+
jest.mock('@project-serum/serum', () => ({
155+
TokenInstructions: {
156+
initializeMint: jest.fn(),
157+
initializeAccount: jest.fn(),
158+
transfer: jest.fn(),
159+
},
160+
Market: {
161+
load: jest.fn(() => Promise.resolve({
162+
address: 'mock-market-address',
163+
baseMintAddress: 'mock-base-mint',
164+
quoteMintAddress: 'mock-quote-mint',
165+
})),
166+
},
167+
}));
168+
133169
// Mock SVM-Pay to avoid network calls in tests
134170
jest.mock('svm-pay', () => ({
135171
SVMPay: jest.fn().mockImplementation(() => ({

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@
11971197

11981198
"@craco/craco@^7.1.0":
11991199
version "7.1.0"
1200-
resolved "https://registry.npmjs.org/@craco/craco/-/craco-7.1.0.tgz"
1200+
resolved "https://registry.yarnpkg.com/@craco/craco/-/craco-7.1.0.tgz#12bd394c7f0334e214302e4d35a1768f68042fbb"
12011201
integrity sha512-oRAcPIKYrfPXp9rSzlsDNeOaVtDiKhoyqSXUoqiK24jCkHr4T8m/a2f74yXIzCbIheoUWDOIfWZyRgFgT+cpqA==
12021202
dependencies:
12031203
autoprefixer "^10.4.12"

0 commit comments

Comments
 (0)