Skip to content

Commit 758a075

Browse files
Copilot0xrinegade
andcommitted
Fix Jest mock variable scope issues - prefix with 'mock' and use inline factories
Co-authored-by: 0xrinegade <[email protected]>
1 parent 29d81ef commit 758a075

File tree

2 files changed

+78
-24
lines changed

2 files changed

+78
-24
lines changed

src/__mocks__/testMocks.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* TweetNaCl Mock - Centralized cryptographic function mocking
1212
* Provides consistent, deterministic mock implementations for encryption testing
1313
*/
14-
export const createTweetNaClMock = () => {
14+
const mockCreateTweetNaCl = () => {
1515
const mockSecretbox = jest.fn((message: Uint8Array, nonce: Uint8Array, key: Uint8Array) => {
1616
// Return deterministic mock encrypted result
1717
return new Uint8Array(message.length + 16); // Add overhead for box
@@ -40,17 +40,19 @@ export const createTweetNaClMock = () => {
4040
};
4141
};
4242

43+
export const createTweetNaClMock = mockCreateTweetNaCl;
44+
4345
/**
4446
* Apply TweetNaCl mock to jest
4547
*/
4648
export const mockTweetNaCl = () => {
47-
jest.mock('tweetnacl', () => createTweetNaClMock());
49+
jest.mock('tweetnacl', () => mockCreateTweetNaCl());
4850
};
4951

5052
/**
5153
* Crypto-browserify Mock - For PBKDF2 and other crypto operations
5254
*/
53-
export const createCryptoBrowserifyMock = () => ({
55+
const mockCreateCryptoBrowserify = () => ({
5456
pbkdf2: jest.fn((password, salt, iterations, keyLength, digest, callback) => {
5557
// Simulate async operation
5658
setTimeout(() => {
@@ -64,17 +66,19 @@ export const createCryptoBrowserifyMock = () => ({
6466
}),
6567
});
6668

69+
export const createCryptoBrowserifyMock = mockCreateCryptoBrowserify;
70+
6771
/**
6872
* Apply crypto-browserify mock to jest
6973
*/
7074
export const mockCryptoBrowserify = () => {
71-
jest.mock('crypto-browserify', () => createCryptoBrowserifyMock());
75+
jest.mock('crypto-browserify', () => mockCreateCryptoBrowserify());
7276
};
7377

7478
/**
7579
* Argon2-browser Mock - For password hashing
7680
*/
77-
export const createArgon2BrowserMock = () => ({
81+
const mockCreateArgon2Browser = () => ({
7882
hash: jest.fn(async (options) => {
7983
// Create deterministic hash based on password
8084
const passwordBytes = Buffer.from(options.pass, 'utf8');
@@ -90,11 +94,13 @@ export const createArgon2BrowserMock = () => ({
9094
}
9195
});
9296

97+
export const createArgon2BrowserMock = mockCreateArgon2Browser;
98+
9399
/**
94100
* Apply argon2-browser mock to jest
95101
*/
96102
export const mockArgon2Browser = () => {
97-
jest.mock('argon2-browser', () => createArgon2BrowserMock());
103+
jest.mock('argon2-browser', () => mockCreateArgon2Browser());
98104
};
99105

100106
// ===== DOM MOCKS =====
@@ -332,7 +338,7 @@ export const createMockWallet = () => ({
332338
/**
333339
* Create mock for MUI components with forwardRef
334340
*/
335-
export const createMUIComponentMock = (componentName: string) => {
341+
const mockCreateMUIComponent = (componentName: string) => {
336342
const React = require('react');
337343
return React.forwardRef((props: any, ref: any) => {
338344
return React.createElement('div', {
@@ -343,17 +349,19 @@ export const createMUIComponentMock = (componentName: string) => {
343349
});
344350
};
345351

352+
export const createMUIComponentMock = mockCreateMUIComponent;
353+
346354
/**
347355
* Common MUI component mocks
348356
*/
349357
export const mockMUIComponents = () => {
350-
jest.mock('@mui/material/Grid', () => createMUIComponentMock('Grid'));
351-
jest.mock('@mui/material/Box', () => createMUIComponentMock('Box'));
352-
jest.mock('@mui/material/Typography', () => createMUIComponentMock('Typography'));
353-
jest.mock('@mui/material/Button', () => createMUIComponentMock('Button'));
354-
jest.mock('@mui/material/TextField', () => createMUIComponentMock('TextField'));
355-
jest.mock('@mui/material/Card', () => createMUIComponentMock('Card'));
356-
jest.mock('@mui/material/CardContent', () => createMUIComponentMock('CardContent'));
358+
jest.mock('@mui/material/Grid', () => mockCreateMUIComponent('Grid'));
359+
jest.mock('@mui/material/Box', () => mockCreateMUIComponent('Box'));
360+
jest.mock('@mui/material/Typography', () => mockCreateMUIComponent('Typography'));
361+
jest.mock('@mui/material/Button', () => mockCreateMUIComponent('Button'));
362+
jest.mock('@mui/material/TextField', () => mockCreateMUIComponent('TextField'));
363+
jest.mock('@mui/material/Card', () => mockCreateMUIComponent('Card'));
364+
jest.mock('@mui/material/CardContent', () => mockCreateMUIComponent('CardContent'));
357365
};
358366

359367
// ===== TIMER MOCKS =====

src/utils/__tests__/encryption.test.ts

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,64 @@
1-
// Use centralized mocks to avoid duplication
2-
import {
3-
createTweetNaClMock,
4-
createCryptoBrowserifyMock,
5-
createArgon2BrowserMock,
6-
} from '../../__mocks__/testMocks';
7-
81
// Mock crypto functions to avoid browser-specific issues in tests
9-
jest.mock('tweetnacl', () => createTweetNaClMock());
2+
jest.mock('tweetnacl', () => {
3+
const mockSecretbox = jest.fn((message: any, nonce: any, key: any) => {
4+
// Return deterministic mock encrypted result
5+
return new Uint8Array(message.length + 16); // Add overhead for box
6+
}) as jest.MockedFunction<any> & {
7+
open: jest.MockedFunction<any>;
8+
keyLength: number;
9+
nonceLength: number;
10+
overheadLength: number;
11+
};
12+
13+
mockSecretbox.open = jest.fn((ciphertext: any, nonce: any, key: any) => {
14+
if (ciphertext.length <= 16) return null;
15+
return ciphertext.slice(16);
16+
});
17+
18+
mockSecretbox.keyLength = 32;
19+
mockSecretbox.nonceLength = 24;
20+
mockSecretbox.overheadLength = 16;
21+
22+
return {
23+
randomBytes: jest.fn((length: number) => {
24+
// Return stable mock array for testing
25+
return new Uint8Array(Array.from({ length }, (_, i) => i % 256));
26+
}),
27+
secretbox: mockSecretbox
28+
};
29+
});
1030

1131
// Mock crypto-browserify pbkdf2
12-
jest.mock('crypto-browserify', () => createCryptoBrowserifyMock());
32+
jest.mock('crypto-browserify', () => ({
33+
pbkdf2: jest.fn((password: any, salt: any, iterations: any, keyLength: any, digest: any, callback: any) => {
34+
// Simulate async operation
35+
setTimeout(() => {
36+
// Return stable mock key based on password for deterministic testing
37+
const passwordBytes = Buffer.from(password, 'utf8');
38+
const mockKey = Buffer.from(Array.from({ length: keyLength }, (_, i) =>
39+
(passwordBytes[i % passwordBytes.length] + i) % 256
40+
));
41+
callback(null, mockKey);
42+
}, 0);
43+
}),
44+
}));
1345

1446
// Mock argon2-browser
15-
jest.mock('argon2-browser', () => createArgon2BrowserMock());
47+
jest.mock('argon2-browser', () => ({
48+
hash: jest.fn(async (options: any) => {
49+
// Create deterministic hash based on password
50+
const passwordBytes = Buffer.from(options.pass, 'utf8');
51+
return {
52+
hash: new Uint8Array(Array.from({ length: 32 }, (_, i) =>
53+
(passwordBytes[i % passwordBytes.length] + i) % 256
54+
)),
55+
hashHex: 'abcdef123456789',
56+
};
57+
}),
58+
ArgonType: {
59+
Argon2id: 2
60+
}
61+
}));
1662

1763
import {
1864
WalletEncryptionManager,

0 commit comments

Comments
 (0)