Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: convert extraction script to TypeScript and use tsx
- Convert extract-playwright-counts.mjs to TypeScript (.ts)
- Add proper TypeScript types for better type safety
- Use tsx for execution instead of node
- Auto-install tsx in CI if not available
- Better alignment with the TypeScript codebase

This provides better type safety and consistency with the rest of
the codebase while maintaining the same functionality.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
  • Loading branch information
snomiao and claude committed Sep 10, 2025
commit d6174b7005ffca59e10c1fe8d43e848bff64be01
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
#!/usr/bin/env node
#!/usr/bin/env tsx

import fs from 'fs';
import path from 'path';

interface TestStats {
expected?: number;
unexpected?: number;
flaky?: number;
skipped?: number;
finished?: number;
}

interface ReportData {
stats?: TestStats;
}

interface TestCounts {
passed: number;
failed: number;
flaky: number;
skipped: number;
total: number;
}

/**
* Extract test counts from Playwright HTML report
* @param {string} reportDir - Path to the playwright-report directory
* @returns {Object} Test counts { passed, failed, flaky, skipped, total }
* @param reportDir - Path to the playwright-report directory
* @returns Test counts { passed, failed, flaky, skipped, total }
*/
function extractTestCounts(reportDir) {
const counts = {
function extractTestCounts(reportDir: string): TestCounts {
const counts: TestCounts = {
passed: 0,
failed: 0,
flaky: 0,
Expand All @@ -21,13 +41,14 @@ function extractTestCounts(reportDir) {
// First, try to find report.json which Playwright generates with JSON reporter
const jsonReportFile = path.join(reportDir, 'report.json');
if (fs.existsSync(jsonReportFile)) {
const reportJson = JSON.parse(fs.readFileSync(jsonReportFile, 'utf-8'));
const reportJson: ReportData = JSON.parse(fs.readFileSync(jsonReportFile, 'utf-8'));
if (reportJson.stats) {
counts.total = reportJson.stats.expected || 0;
counts.passed = reportJson.stats.expected - (reportJson.stats.unexpected || 0) - (reportJson.stats.flaky || 0) - (reportJson.stats.skipped || 0);
counts.failed = reportJson.stats.unexpected || 0;
counts.flaky = reportJson.stats.flaky || 0;
counts.skipped = reportJson.stats.skipped || 0;
const stats = reportJson.stats;
counts.total = stats.expected || 0;
counts.passed = (stats.expected || 0) - (stats.unexpected || 0) - (stats.flaky || 0) - (stats.skipped || 0);
counts.failed = stats.unexpected || 0;
counts.flaky = stats.flaky || 0;
counts.skipped = stats.skipped || 0;
return counts;
}
}
Expand All @@ -43,14 +64,15 @@ function extractTestCounts(reportDir) {
if (dataMatch) {
try {
const decodedData = Buffer.from(dataMatch[1], 'base64').toString('utf-8');
const reportData = JSON.parse(decodedData);
const reportData: ReportData = JSON.parse(decodedData);

if (reportData.stats) {
counts.total = reportData.stats.expected || 0;
counts.passed = reportData.stats.expected - (reportData.stats.unexpected || 0) - (reportData.stats.flaky || 0) - (reportData.stats.skipped || 0);
counts.failed = reportData.stats.unexpected || 0;
counts.flaky = reportData.stats.flaky || 0;
counts.skipped = reportData.stats.skipped || 0;
const stats = reportData.stats;
counts.total = stats.expected || 0;
counts.passed = (stats.expected || 0) - (stats.unexpected || 0) - (stats.flaky || 0) - (stats.skipped || 0);
counts.failed = stats.unexpected || 0;
counts.flaky = stats.flaky || 0;
counts.skipped = stats.skipped || 0;
return counts;
}
} catch (e) {
Expand All @@ -63,14 +85,15 @@ function extractTestCounts(reportDir) {
if (dataMatch) {
try {
// Use Function constructor instead of eval for safety
const reportData = (new Function('return ' + dataMatch[1]))();
const reportData = (new Function('return ' + dataMatch[1]))() as ReportData;

if (reportData.stats) {
counts.total = reportData.stats.expected || 0;
counts.passed = reportData.stats.expected - (reportData.stats.unexpected || 0) - (reportData.stats.flaky || 0) - (reportData.stats.skipped || 0);
counts.failed = reportData.stats.unexpected || 0;
counts.flaky = reportData.stats.flaky || 0;
counts.skipped = reportData.stats.skipped || 0;
const stats = reportData.stats;
counts.total = stats.expected || 0;
counts.passed = (stats.expected || 0) - (stats.unexpected || 0) - (stats.flaky || 0) - (stats.skipped || 0);
counts.failed = stats.unexpected || 0;
counts.flaky = stats.flaky || 0;
counts.skipped = stats.skipped || 0;
return counts;
}
} catch (e) {
Expand Down Expand Up @@ -118,7 +141,7 @@ function extractTestCounts(reportDir) {
const reportDir = process.argv[2];

if (!reportDir) {
console.error('Usage: extract-playwright-counts.mjs <report-directory>');
console.error('Usage: extract-playwright-counts.ts <report-directory>');
process.exit(1);
}

Expand Down
16 changes: 11 additions & 5 deletions scripts/cicd/pr-playwright-deploy-and-comment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,23 @@ else
echo "$url" > "$temp_dir/$i.url"
echo "Deployment result for $browser: $url"

# Extract test counts if Node.js is available
EXTRACT_SCRIPT="$SCRIPT_DIR/extract-playwright-counts.mjs"
# Extract test counts using tsx (TypeScript executor)
EXTRACT_SCRIPT="$SCRIPT_DIR/extract-playwright-counts.ts"
REPORT_DIR="$BASE_DIR/reports/playwright-report-$browser"

if command -v node > /dev/null 2>&1 && [ -f "$EXTRACT_SCRIPT" ]; then
# Check if tsx is available, install if not
if ! command -v tsx > /dev/null 2>&1; then
echo "Installing tsx..." >&2
npm install -g tsx >&2 || echo "Failed to install tsx" >&2
fi

if command -v tsx > /dev/null 2>&1 && [ -f "$EXTRACT_SCRIPT" ]; then
echo "Extracting counts from $REPORT_DIR using $EXTRACT_SCRIPT" >&2
counts=$(node "$EXTRACT_SCRIPT" "$REPORT_DIR" 2>&1 || echo '{}')
counts=$(tsx "$EXTRACT_SCRIPT" "$REPORT_DIR" 2>&1 || echo '{}')
echo "Extracted counts for $browser: $counts" >&2
echo "$counts" > "$temp_dir/$i.counts"
else
echo "Script not found or Node.js not available: $EXTRACT_SCRIPT" >&2
echo "Script not found or tsx not available: $EXTRACT_SCRIPT" >&2
echo '{}' > "$temp_dir/$i.counts"
fi
) &
Expand Down
Loading