-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-safely.js
77 lines (65 loc) · 2.08 KB
/
test-safely.js
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
#!/usr/bin/env node
/**
* This script runs the tests with a global timeout to prevent hanging.
*
* Usage:
* node test-safely.js [--timeout 60000] [--pattern "bank-routing-number"]
*
* Options:
* --timeout: Maximum time in ms before killing the test process (default: 60000)
* --pattern: Test pattern to match (passed to -g flag of web-test-runner)
*/
const { spawn } = require('child_process');
const path = require('path');
// Parse command-line arguments
const args = process.argv.slice(2);
let timeout = 60000; // Default timeout: 60 seconds
let pattern = '';
for (let i = 0; i < args.length; i++) {
if (args[i] === '--timeout' && i + 1 < args.length) {
timeout = parseInt(args[i + 1], 10);
i++;
} else if (args[i] === '--pattern' && i + 1 < args.length) {
pattern = args[i + 1];
i++;
}
}
// Build the test command
let testCommand = 'npx';
let testArgs = ['web-test-runner', 'test/**/*.web-test.js', '--node-resolve', '--watch=false'];
if (pattern) {
testArgs.push('-g', pattern);
}
console.log(`Running tests with a ${timeout}ms timeout...`);
console.log(`Command: ${testCommand} ${testArgs.join(' ')}`);
// Start the test process
const testProcess = spawn(testCommand, testArgs, {
stdio: 'inherit',
shell: true,
});
// Set a timeout to kill the process if it hangs
const timeoutId = setTimeout(() => {
console.error(`\n\n⚠️ Test execution timed out after ${timeout}ms!`);
console.error('Terminating test runner...\n');
testProcess.kill('SIGTERM');
// Give the process a moment to shut down gracefully, then force kill if needed
setTimeout(() => {
if (!testProcess.killed) {
console.error('Force killing test process...');
testProcess.kill('SIGKILL');
}
process.exit(1);
}, 5000);
}, timeout);
// Handle process completion
testProcess.on('close', code => {
clearTimeout(timeoutId);
console.log(`Tests completed with exit code: ${code}`);
process.exit(code);
});
// Handle process errors
testProcess.on('error', err => {
clearTimeout(timeoutId);
console.error('Failed to start test process:', err);
process.exit(1);
});