-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
276 lines (222 loc) · 7.09 KB
/
test.js
File metadata and controls
276 lines (222 loc) · 7.09 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env node
/**
* Tea Store Demo - Cross-Platform Test Runner
* This script runs all tests for both backend and frontend
* Works on Windows, macOS, and Linux
*/
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
// Colors for output (ANSI codes work on modern terminals including Windows 10+)
const colors = {
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
reset: '\x1b[0m',
};
// Get the directory where this script is located
const SCRIPT_DIR = __dirname;
// Track test results
let backendResult = 0;
let frontendUnitResult = 0;
let e2eResult = 0;
// Parse arguments
const args = process.argv.slice(2);
const RUN_E2E = args.includes('--e2e');
const RUN_COVERAGE = args.includes('--coverage');
const RUN_HEADED = args.includes('--headed');
const SHOW_HELP = args.includes('--help');
function log(message, color = '') {
if (color) {
console.log(`${color}${message}${colors.reset}`);
} else {
console.log(message);
}
}
function showHelp() {
console.log('Usage: node test.js [options]');
console.log('');
console.log('Options:');
console.log(' --e2e Run E2E tests (requires services to be running)');
console.log(' --headed Run E2E tests in headed mode (visible browser)');
console.log(' --coverage Run tests with coverage reports');
console.log(' --help Show this help message');
console.log('');
console.log('By default, runs backend (pytest) and frontend unit tests (vitest)');
process.exit(0);
}
function runCommand(command, args, options = {}) {
return new Promise((resolve) => {
const isWindows = process.platform === 'win32';
// On Windows, use shell to run commands
const spawnOptions = {
cwd: options.cwd || SCRIPT_DIR,
stdio: 'inherit',
shell: isWindows,
};
const proc = spawn(command, args, spawnOptions);
proc.on('close', (code) => {
resolve(code || 0);
});
proc.on('error', (err) => {
console.error(`Failed to start process: ${err.message}`);
resolve(1);
});
});
}
function getPythonPath() {
const isWindows = process.platform === 'win32';
const venvPath = path.join(SCRIPT_DIR, 'backend', 'venv');
if (isWindows) {
return path.join(venvPath, 'Scripts', 'python.exe');
}
return path.join(venvPath, 'bin', 'python');
}
function getNpmCommand() {
return process.platform === 'win32' ? 'npm.cmd' : 'npm';
}
function getNpxCommand() {
return process.platform === 'win32' ? 'npx.cmd' : 'npx';
}
async function runBackendTests() {
log('[1/3] Running Backend Tests (pytest)...', colors.yellow);
console.log('');
const backendDir = path.join(SCRIPT_DIR, 'backend');
const venvDir = path.join(backendDir, 'venv');
const pythonPath = getPythonPath();
// Check if venv exists
if (!fs.existsSync(venvDir)) {
log(' ✗ Backend virtual environment not found', colors.red);
log(' Run start script first to set up dependencies');
return 1;
}
// Check if python exists in venv
if (!fs.existsSync(pythonPath)) {
log(' ✗ Python not found in virtual environment', colors.red);
return 1;
}
// Build pytest arguments
const pytestArgs = ['-m', 'pytest', 'tests/', '-v'];
if (RUN_COVERAGE) {
pytestArgs.push('--cov=app', '--cov-report=term-missing', '--cov-report=html:coverage_html');
}
const result = await runCommand(pythonPath, pytestArgs, { cwd: backendDir });
if (result === 0) {
log(' ✓ Backend tests passed', colors.green);
} else {
log(' ✗ Backend tests failed', colors.red);
}
console.log('');
return result;
}
async function runFrontendUnitTests() {
log('[2/3] Running Frontend Unit Tests (vitest)...', colors.yellow);
console.log('');
const frontendDir = path.join(SCRIPT_DIR, 'frontend');
const nodeModulesDir = path.join(frontendDir, 'node_modules');
const npm = getNpmCommand();
// Check if node_modules exists, install if not
if (!fs.existsSync(nodeModulesDir)) {
log(' Installing frontend dependencies...', colors.yellow);
await runCommand(npm, ['install', '--silent'], { cwd: frontendDir });
}
const testScript = RUN_COVERAGE ? 'test:coverage' : 'test';
const result = await runCommand(npm, ['run', testScript], { cwd: frontendDir });
if (result === 0) {
log(' ✓ Frontend unit tests passed', colors.green);
} else {
log(' ✗ Frontend unit tests failed', colors.red);
}
console.log('');
return result;
}
async function runE2ETests() {
log('[3/3] Running E2E Tests (playwright)...', colors.yellow);
console.log('');
const nodeModulesDir = path.join(SCRIPT_DIR, 'node_modules');
const npm = getNpmCommand();
const npx = getNpxCommand();
// Ensure root dependencies are installed
if (!fs.existsSync(nodeModulesDir)) {
log(' Installing root dependencies...', colors.yellow);
await runCommand(npm, ['install'], { cwd: SCRIPT_DIR });
}
const playwrightArgs = ['playwright', 'test'];
if (RUN_HEADED) {
playwrightArgs.push('--headed');
}
const result = await runCommand(npx, playwrightArgs, { cwd: SCRIPT_DIR });
if (result === 0) {
log(' ✓ E2E tests passed', colors.green);
} else {
log(' ✗ E2E tests failed', colors.red);
}
console.log('');
return result;
}
function printSummary() {
log('========================================', colors.blue);
log(' Test Summary', colors.blue);
log('========================================', colors.blue);
console.log('');
let overallResult = 0;
if (backendResult === 0) {
log(' Backend (pytest): PASSED', colors.green);
} else {
log(' Backend (pytest): FAILED', colors.red);
overallResult = 1;
}
if (frontendUnitResult === 0) {
log(' Frontend (vitest): PASSED', colors.green);
} else {
log(' Frontend (vitest): FAILED', colors.red);
overallResult = 1;
}
if (RUN_E2E) {
if (e2eResult === 0) {
log(' E2E (playwright): PASSED', colors.green);
} else {
log(' E2E (playwright): FAILED', colors.red);
overallResult = 1;
}
} else {
log(' E2E (playwright): SKIPPED', colors.yellow);
}
console.log('');
if (overallResult === 0) {
log('All tests passed!', colors.green);
} else {
log('Some tests failed.', colors.red);
}
console.log('');
return overallResult;
}
async function main() {
if (SHOW_HELP) {
showHelp();
}
log('========================================', colors.blue);
log(' Tea Store Demo - Test Runner', colors.blue);
log('========================================', colors.blue);
console.log('');
// Run backend tests
backendResult = await runBackendTests();
// Run frontend unit tests
frontendUnitResult = await runFrontendUnitTests();
// Run E2E tests if requested
if (RUN_E2E) {
e2eResult = await runE2ETests();
} else {
log('[3/3] Skipping E2E Tests', colors.blue);
log(' (Run with --e2e flag to include E2E tests)');
console.log('');
}
// Print summary and exit
const overallResult = printSummary();
process.exit(overallResult);
}
main().catch((err) => {
console.error('Unexpected error:', err);
process.exit(1);
});