Files
openobserve/web/coverage.sh
T
Bhargavandsai nikhil kethe b032141aae fix: added unit test case coverage (#7788)
### **PR Type**
Tests, Enhancement


___

### **Description**
- Include coverage thresholds in Vitest config

- Introduce Bash script for coverage checks

- Extend CI to run frontend coverage checks

- Update npm scripts for coverage verification


___



<details> <summary><h3> File Walkthrough</h3></summary>

<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Configuration
changes</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>vitest.config.ts</strong><dd><code>Add coverage
thresholds to Vitest config</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; </dd></summary>
<hr>

web/vitest.config.ts

<ul><li>Add "json-summary" to coverage reporters<br> <li> Define
coverage thresholds (80%) for all metrics<br> <li> Keep existing
exclude/include patterns</ul>


</details>


  </td>
<td><a
href="https://github.com/openobserve/openobserve/pull/7788/files#diff-c3fc010b682e68f2ed0b7b1485b8aa513443a66d500903b8dff1d65908d02aae">+7/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>package.json</strong><dd><code>Update npm coverage
scripts</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; </dd></summary>
<hr>

web/package.json

<ul><li>Modify "test:unit:coverage" to use "--run"<br> <li> Add
"test:coverage:check" script pointing to "./coverage.sh check"</ul>


</details>


  </td>
<td><a
href="https://github.com/openobserve/openobserve/pull/7788/files#diff-b861012a5dd72b8a9f3281b7cf09f5a779c98569d040b1bbc1db50f1b15e7cce">+2/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Enhancement</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>coverage.sh</strong><dd><code>Add coverage utility Bash
script</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

web/coverage.sh

<ul><li>New Bash script supporting "check", "html", "test",
"show-env"<br> <li> Enforce coverage thresholds via Python JSON
parsing<br> <li> Provide usage/help output and environment overrides<br>
<li> Handle errors and missing summary file</ul>


</details>


  </td>
<td><a
href="https://github.com/openobserve/openobserve/pull/7788/files#diff-a5c51d1d52fd0ff1b77a2dae626334fe574e76d9269a35655543ec4ead5575d2">+140/-0</a>&nbsp;
</td>

</tr>
</table></td></tr><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>unit-tests.yml</strong><dd><code>Integrate frontend
coverage in CI workflow</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
</dd></summary>
<hr>

.github/workflows/unit-tests.yml

<ul><li>Add "frontend_tests" job for coverage checks<br> <li> Skip tests
if no changes under "web/" directory<br> <li> Setup Node.js and install
dependencies<br> <li> Invoke "./coverage.sh check" in CI</ul>


</details>


  </td>
<td><a
href="https://github.com/openobserve/openobserve/pull/7788/files#diff-cd0effce06f425599e96952f4d5f684641a683f46d9909e7c74a9f8b2f03ccd3">+29/-0</a>&nbsp;
&nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

</details>

___

---------

Co-authored-by: sai nikhil kethe <nikhil@openobserve.ai>
2025-08-06 14:38:00 +05:30

140 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu -o pipefail
# Default coverage thresholds - you can override these with environment variables
export FRONTEND_COVERAGE_LINES=${FRONTEND_COVERAGE_LINES:-27}
export FRONTEND_COVERAGE_FUNCTIONS=${FRONTEND_COVERAGE_FUNCTIONS:-27}
export FRONTEND_COVERAGE_BRANCHES=${FRONTEND_COVERAGE_BRANCHES:-64}
export FRONTEND_COVERAGE_STATEMENTS=${FRONTEND_COVERAGE_STATEMENTS:-27}
usage() {
cat <<EOF
Usage: $0 [-h | --help] [<command>]
Commands:
check Run frontend tests with coverage and verify thresholds
html Generate coverage report in HTML format
show-env Show the coverage thresholds
test Run tests with coverage
help Show this help and exit
Omitted <command> defaults to 'check'.
EOF
}
cmd_test() {
npm run test:unit:coverage
}
cmd_html() {
npm run test:unit:coverage
echo "Coverage report generated in coverage/ directory"
if command -v open >/dev/null 2>&1; then
open coverage/index.html
elif command -v xdg-open >/dev/null 2>&1; then
xdg-open coverage/index.html
else
echo "Open coverage/index.html in your browser to view the report"
fi
}
cmd_show_env() {
cat <<EOF
FRONTEND_COVERAGE_LINES=$FRONTEND_COVERAGE_LINES
FRONTEND_COVERAGE_FUNCTIONS=$FRONTEND_COVERAGE_FUNCTIONS
FRONTEND_COVERAGE_BRANCHES=$FRONTEND_COVERAGE_BRANCHES
FRONTEND_COVERAGE_STATEMENTS=$FRONTEND_COVERAGE_STATEMENTS
EOF
}
cmd_check() {
echo "Running frontend tests with coverage..."
npm run test:unit:coverage
if [ -f "coverage/coverage-summary.json" ]; then
echo "Checking coverage thresholds..."
python3 <(
cat <<'EOF'
import json
import os
import sys
thresholds = {
'lines': float(os.environ['FRONTEND_COVERAGE_LINES']),
'functions': float(os.environ['FRONTEND_COVERAGE_FUNCTIONS']),
'branches': float(os.environ['FRONTEND_COVERAGE_BRANCHES']),
'statements': float(os.environ['FRONTEND_COVERAGE_STATEMENTS']),
}
try:
with open('coverage/coverage-summary.json') as f:
report = json.load(f)
totals = report['total']
exit_status = 0
for metric, threshold in thresholds.items():
actual = totals[metric]['pct']
metric_name = metric.capitalize()
if actual >= threshold:
print(f'✅ {metric_name} coverage: {actual:.2f}%', file=sys.stderr)
else:
print(
f'❌ {metric_name} coverage is below threshold: {actual:.2f}% < {threshold}%',
file=sys.stderr,
)
exit_status = 1
if exit_status == 0:
print('🎉 All coverage thresholds met!', file=sys.stderr)
else:
print('💥 Some coverage thresholds not met. Please add more tests.', file=sys.stderr)
sys.exit(exit_status)
except FileNotFoundError:
print('❌ Coverage summary file not found. Make sure tests ran successfully.', file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f'❌ Error checking coverage: {e}', file=sys.stderr)
sys.exit(1)
EOF
)
else
echo "❌ Coverage summary not found. Tests may have failed."
exit 1
fi
}
main() {
case "${1:-}" in
'')
cmd_check
;;
check)
shift
cmd_check "$@"
;;
test)
shift
cmd_test "$@"
;;
html)
shift
cmd_html "$@"
;;
show-env)
cmd_show_env
;;
-h | --help | help)
usage
;;
*)
echo >&2 "Invalid argument: $1"
echo >&2 "Type '$0 --help' for usage"
exit 1
;;
esac
}
main "$@"