#!/bin/bash
# Color definitionsBLUE='\033[0;34m'GREEN='\033[0;32m'RED='\033[0;31m'YELLOW='\033[1;33m'CYAN='\033[0;36m'NC='\033[0m'# No ColorBOLD='\033[1m'# Function to display usageshow_usage(){echo -e "${YELLOW}Usage:${NC}$0 [--find-by-name] <relative-file-path> [additional test options]"echo -e "${YELLOW}Examples:${NC}"echo -e " $0 src/components/Button.tsx --watch"echo -e " $0 src/components/Button.tsx --find-by-name --watch"exit1}# Initialize variablesFIND_BY_NAME=falseRELATIVE_FILE_PATH=""# Parse argumentswhile[[$# -gt 0]];docase$1 in
--find-by-name)FIND_BY_NAME=trueshift;; *)if[ -z "$RELATIVE_FILE_PATH"];thenRELATIVE_FILE_PATH="$1"elsebreak# Stop parsing and keep remaining args for test optionsfishift;;esacdone# Check if a file name is providedif[ -z "$RELATIVE_FILE_PATH"];then show_usage
fi# Check if the file existsif[ ! -f "$RELATIVE_FILE_PATH"];thenecho -e "${RED}Error: ${NC}File '${YELLOW}${RELATIVE_FILE_PATH}${NC}' not found. 😕"exit1fiFILE_PATH=$(echo"$RELATIVE_FILE_PATH"| cut -d'/' -f3-)# Get just the file name without extension for --find-by-name modeFILE_NAME=$(basename "$RELATIVE_FILE_PATH")FILE_NAME_NO_EXT="${FILE_NAME%.*}"# Construct the corresponding test file pathTEST_FILE_NAME="${FILE_PATH%.*}.test"if["$FIND_BY_NAME"=true];then# Search by file nameTEST_PATH=$(find "tests" -type f \( -name "${FILE_NAME_NO_EXT}.test.tsx" -o -name "${FILE_NAME_NO_EXT}.test.ts"\))else# Original search by pathTEST_PATH=$(find "tests" -type f \( -path "*/${TEST_FILE_NAME}.tsx" -o -path "*/${TEST_FILE_NAME}.ts"\))fi# Check if the test file existsif[ -z "$TEST_PATH"];thenecho -e "${RED}Error: ${NC}Test file for '${YELLOW}${RELATIVE_FILE_PATH}${NC}' not found in 'tests' directory. 😕"echo -e "Test file name: ${YELLOW}${FILE_NAME_NO_EXT}.test.tsx${NC}"echo -e "${YELLOW}Hint:${NC} If you're looking for a file by name, try using the --find-by-name flag."exit1fi# Print test information with stylingecho -e "\n${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"echo -e "${GREEN}▶ Running Coverage Test${NC}"echo -e "${CYAN}📁 Source file:${NC}${RELATIVE_FILE_PATH}"echo -e "${CYAN}🧪 Test file:${NC}${TEST_PATH}"echo -e "${CYAN}⚙️ Options:${NC}$@"echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}\n"yarn test"$TEST_PATH" --coverage --collectCoverageFrom="$RELATIVE_FILE_PATH""$@"
Los casos de prueba están en la carpeta tests. El script buscará el archivo de prueba por ruta de manera predeterminada. Si deseas buscar por nombre, puedes usar la bandera --find-by-name.
1
2
3
4
5
# Si comparten la misma ruta./coverage.sh src/components/Button.tsx --watch
# Si el archivo de prueba tiene una ruta diferente pero el mismo nombre./coverage.sh src/components/Button.tsx --find-by-name --watch