-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
173 lines (153 loc) · 4.29 KB
/
vitest.setup.ts
File metadata and controls
173 lines (153 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Vitest setup for enterprise-grade testing
* Configures global test environment and utilities
*/
import { beforeEach, afterEach, afterAll, vi } from 'vitest';
// Mark test environment
globalThis.__MULTI_SHOP_TEST__ = true;
// Enhanced test utilities
declare global {
const createMockShopConfig: (overrides?: Partial<ShopConfig>) => ShopConfig;
const createMockCredentials: (overrides?: Partial<ShopCredentials>) => ShopCredentials;
namespace Vi {
interface ExpectStatic {
toBeShopError: (error: unknown, expectedCode?: string) => void;
toNotExposeSecrets: (data: unknown) => void;
toBeFasterThan: (duration: number) => void;
}
}
}
// Type definitions for test utilities
interface ShopConfig {
shopId: string;
name: string;
shopify: {
stores: {
production: { domain: string; branch: string };
staging: { domain: string; branch: string };
};
authentication: { method: string };
};
}
interface ShopCredentials {
developer: string;
shopify: {
stores: {
production: { themeToken: string };
staging: { themeToken: string };
};
};
notes?: string;
}
// Global test utilities
globalThis.createMockShopConfig = (overrides = {}) => ({
shopId: 'test-shop',
name: 'Test Shop',
shopify: {
stores: {
production: {
domain: 'test-shop.myshopify.com',
branch: 'test-shop/main'
},
staging: {
domain: 'staging-test-shop.myshopify.com',
branch: 'test-shop/staging'
}
},
authentication: {
method: 'theme-access-app'
}
},
...overrides
});
globalThis.createMockCredentials = (overrides = {}) => ({
developer: 'test-developer',
shopify: {
stores: {
production: { themeToken: 'test-production-token' },
staging: { themeToken: 'test-staging-token' }
}
},
notes: 'Test credentials',
...overrides
});
// Custom matchers
expect.extend({
toBeShopError(received: unknown, expectedCode?: string) {
const pass = received instanceof Error &&
received.constructor.name.endsWith('ShopError') &&
(!expectedCode || (received as any).code === expectedCode);
if (pass) {
return {
message: () => `Expected ${received} not to be a ShopError`,
pass: true
};
} else {
return {
message: () => `Expected ${received} to be a ShopError${expectedCode ? ` with code ${expectedCode}` : ''}`,
pass: false
};
}
},
toNotExposeSecrets(received: unknown) {
const serialized = JSON.stringify(received);
const suspiciousPatterns = [
/shptka_[a-zA-Z0-9]{40,}/, // Shopify tokens
/password.*[:=]\s*['"]\w+['"]/i, // Password patterns
/token.*[:=]\s*['"]\w+['"]/i, // Token patterns
/secret.*[:=]\s*['"]\w+['"]/i // Secret patterns
];
for (const pattern of suspiciousPatterns) {
if (pattern.test(serialized)) {
return {
message: () => `Expected data not to contain secrets, but found pattern: ${pattern}`,
pass: false
};
}
}
return {
message: () => `Expected data to expose secrets`,
pass: true
};
},
toBeFasterThan(received: number, expected: number) {
const pass = received < expected;
if (pass) {
return {
message: () => `Expected ${received}ms not to be faster than ${expected}ms`,
pass: true
};
} else {
return {
message: () => `Expected ${received}ms to be faster than ${expected}ms`,
pass: false
};
}
}
});
// Setup before each test
beforeEach(() => {
// Clear environment variables
delete process.env.AUTO_SELECT_DEV;
delete process.env.LOG_LEVEL;
delete process.env.SHOPIFY_CLI_THEME_TOKEN;
delete process.env.SHOPIFY_STORE_DOMAIN;
// Mock console methods to avoid noise in test output
vi.spyOn(console, 'log').mockImplementation(() => {});
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
});
// Cleanup after each test
afterEach(() => {
// Clear all timers
vi.clearAllTimers();
// Restore console
vi.restoreAllMocks();
});
// Global cleanup
afterAll(() => {
// Cleanup any global resources
if (globalThis.mockPerformanceMonitor) {
globalThis.mockPerformanceMonitor.cleanup();
}
});