test_unit_run_dir 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. declare -a vitest_args=("$@")
  3. has_mocha_test=0
  4. has_mocha_mjs_test=0
  5. has_vitest_test=0
  6. mocha_mjs_dirs=()
  7. for dir_path in "$@"; do
  8. if [ -n "$(find "$dir_path" -name "*.js" -type f -print -quit 2>/dev/null)" ]; then
  9. has_mocha_test=1
  10. fi
  11. if [ -n "$(find "$dir_path" -name "*Tests.mjs" -type f -print -quit 2>/dev/null)" ]; then
  12. has_mocha_mjs_test=1
  13. mocha_mjs_dirs+=("$dir_path/**/*Tests.mjs")
  14. fi
  15. if [ -n "$(find "$dir_path" -name "*.test.mjs" -type f -print -quit 2>/dev/null)" ]; then
  16. has_vitest_test=1
  17. fi
  18. done
  19. if [[ -n "$MOCHA_GREP" ]]; then
  20. vitest_args+=("--testNamePattern" "$MOCHA_GREP")
  21. fi
  22. if [[ -n "$VITEST_NO_CACHE" ]]; then
  23. echo "Disabling cache for vitest."
  24. vitest_args+=("--no-cache")
  25. fi
  26. echo "Running unit tests in directory: $*"
  27. # Remove this if/else when we have converted all module tests to vitest.
  28. if (( has_vitest_test == 1 )); then
  29. npm run test:unit:esm -- "${vitest_args[@]}"
  30. vitest_status=$?
  31. else
  32. echo "No vitest tests found in $*, skipping vitest step."
  33. vitest_status=0
  34. fi
  35. if (( has_mocha_test == 1 )); then
  36. mocha --recursive --timeout 25000 --exit --grep="$MOCHA_GREP" --require test/unit/bootstrap.js --extension=js "$@"
  37. mocha_status=$?
  38. fi
  39. # Remove this if/else when we have converted all module tests to vitest.
  40. if (( has_mocha_mjs_test == 1)); then
  41. mocha --recursive --timeout 25000 --exit --grep="$MOCHA_GREP" --require test/unit/bootstrap.js --extension=mjs "${mocha_mjs_dirs[@]}"
  42. mocha_status=$((mocha_status != 0 ? mocha_status : $?))
  43. fi
  44. if (( has_mocha_mjs_test == 0 && has_mocha_test == 0 )); then
  45. echo "No mocha tests found in $TARGET_DIR, skipping mocha step."
  46. mocha_status=0
  47. fi
  48. if [ "$mocha_status" -eq 0 ] && [ "$vitest_status" -eq 0 ]; then
  49. exit 0
  50. fi
  51. # Report status briefly at the end for failures
  52. if [ "$mocha_status" -ne 0 ]; then
  53. echo "Mocha tests failed with status: $mocha_status"
  54. fi
  55. if [ "$vitest_status" -ne 0 ]; then
  56. echo "Vitest tests failed with status: $vitest_status"
  57. fi
  58. exit 1