-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-package.sh
More file actions
executable file
·126 lines (100 loc) · 2.8 KB
/
test-package.sh
File metadata and controls
executable file
·126 lines (100 loc) · 2.8 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
# Set up error handling
set -e # Exit on error
FAILED_TESTS=()
# Function to run test and capture both output and errors
run_test() {
local TEST_TYPE=$1
local OUTPUT
local EXIT_CODE
echo "Running $TEST_TYPE test..."
# Run node and capture both output and exit code
if ! OUTPUT=$(node index.js 2>&1); then
echo "❌ $TEST_TYPE test failed with error:"
echo "$OUTPUT"
FAILED_TESTS+=("$TEST_TYPE")
return 1
fi
echo "$OUTPUT" # Display the output
# Only validate output if execution succeeded
if ! validate_test_output "$OUTPUT" "$TEST_TYPE"; then
FAILED_TESTS+=("$TEST_TYPE")
return 1
fi
echo "✅ $TEST_TYPE test passed"
return 0
}
# Function to validate test output
validate_test_output() {
local OUTPUT=$1
local TEST_TYPE=$2
local EXPECTED_INITIAL="$TEST_TYPE Test - Count: 0"
local EXPECTED_FINAL="$TEST_TYPE Test - Count: 1"
if [[ $OUTPUT != *"$EXPECTED_INITIAL"* ]]; then
echo "❌ $TEST_TYPE validation failed: Missing initial output"
echo "Expected: $EXPECTED_INITIAL"
return 1
fi
if [[ $OUTPUT != *"$EXPECTED_FINAL"* ]]; then
echo "❌ $TEST_TYPE validation failed: Missing final output"
echo "Expected: $EXPECTED_FINAL"
return 1
fi
return 0
}
# Clean up previous test directories
rm -rf test-reflex
rm -f *.tgz
# Build and pack the package
echo "📦 Building and packing package..."
npm run local:pack
# Get the name of the generated tarball
PACKAGE=$(ls *.tgz)
echo "📄 Using package: $PACKAGE"
# Create test directory
mkdir -p test-reflex
cd test-reflex
# Test ESM
echo -e "\n🔍 Testing ESM..."
mkdir esm-test
cd esm-test
npm init -y > /dev/null 2>&1
npm pkg set type=module > /dev/null 2>&1
npm install ../../$PACKAGE > /dev/null 2>&1
cat > index.js << 'EOF'
import { reflex } from "@2toad/reflex";
const count = reflex({ initialValue: 0 });
count.subscribe(value => console.log("ESM Test - Count:", value));
count.setValue(1);
EOF
run_test "ESM"
cd ..
# Test CommonJS
echo -e "\n🔍 Testing CommonJS..."
mkdir cjs-test
cd cjs-test
npm init -y > /dev/null 2>&1
npm install ../../$PACKAGE > /dev/null 2>&1
cat > index.js << 'EOF'
const { reflex } = require("@2toad/reflex");
const count = reflex({ initialValue: 0 });
count.subscribe(value => console.log("CJS Test - Count:", value));
count.setValue(1);
EOF
run_test "CJS"
# Return to root directory
cd ../..
# Report final status
echo -e "\n📋 Test Summary:"
if [ ${#FAILED_TESTS[@]} -eq 0 ]; then
echo "✅ All package tests passed!"
else
echo "❌ Failed tests: ${FAILED_TESTS[*]}"
echo "Test files are in ./test-reflex/ for inspection"
exit 1
fi
# Cleanup
echo -e "\n\n🧹 Cleaning up test files..."
rm -rf test-reflex
rm -f *.tgz
echo "✨ Cleanup complete."