Skip to content

Commit 631e75f

Browse files
committed
fix(integration_tests): fix deployment issues in node script
1 parent ab3260b commit 631e75f

File tree

4 files changed

+521
-1056
lines changed

4 files changed

+521
-1056
lines changed

integration_test_declarative/README.md

Lines changed: 113 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,41 @@ This framework uses a template-based code generation approach where:
1919
3. Generated code has static exports that Firebase CLI can discover
2020
4. Each test run gets isolated function instances
2121

22+
## Prerequisites
23+
24+
Before running integration tests, ensure the Firebase Functions SDK is built and packaged:
25+
26+
```bash
27+
# From the root firebase-functions directory
28+
npm run pack-for-integration-tests
29+
```
30+
31+
This creates `integration_test_declarative/firebase-functions-local.tgz` which is used by all test suites.
32+
2233
## Quick Start
2334

2435
```bash
25-
# Run all v1 tests (generate, deploy, test)
36+
# Run all tests sequentially (recommended)
37+
npm run test:all:sequential
38+
39+
# Run all v1 tests sequentially
2640
npm run test:v1:all
2741

28-
# Run a single test suite
29-
./scripts/run-suite.sh v1_firestore
42+
# Run all v2 tests sequentially
43+
npm run test:v2:all
3044

31-
# Run with artifact saving (for later cleanup)
32-
./scripts/run-suite.sh v1_firestore --save-artifact
45+
# Run tests in parallel (faster but may hit rate limits)
46+
npm run test:v1:all:parallel
47+
npm run test:v2:all:parallel
48+
49+
# Run a single test suite
50+
npm run test:firestore # Runs v1_firestore
3351

3452
# Clean up after a test run
35-
./scripts/cleanup-suite.sh
53+
npm run cleanup
3654

37-
# Clean up a specific test run
38-
./scripts/cleanup-suite.sh t_1757979490_xkyqun
55+
# List saved test artifacts
56+
npm run cleanup:list
3957
```
4058

4159
## Configuration
@@ -65,25 +83,31 @@ To work around this:
6583
```
6684
integration_test_declarative/
6785
├── config/
68-
│ ├── suites/ # YAML suite definitions
69-
│ │ └── v1_firestore.yaml
70-
│ └── templates/ # Handlebars templates
71-
│ ├── functions/
72-
│ │ ├── index.ts.hbs
73-
│ │ └── firestore/
74-
│ │ └── onCreate.ts.hbs
75-
│ └── firebase.json.hbs
86+
│ ├── v1/
87+
│ │ └── suites.yaml # All v1 suite definitions
88+
│ ├── v2/
89+
│ │ └── suites.yaml # All v2 suite definitions
90+
│ └── suites.schema.json # YAML schema definition
91+
├── templates/ # Handlebars templates
92+
│ └── functions/
93+
│ ├── package.json.hbs
94+
│ ├── tsconfig.json.hbs
95+
│ └── src/
96+
│ ├── v1/ # V1 function templates
97+
│ └── v2/ # V2 function templates
7698
├── generated/ # Generated code (git-ignored)
7799
│ ├── functions/ # Generated function code
100+
│ │ └── firebase-functions-local.tgz # SDK tarball (copied)
78101
│ ├── firebase.json # Generated Firebase config
79102
│ └── .metadata.json # Generation metadata
80103
├── scripts/
81104
│ ├── generate.js # Template generation script
82-
│ ├── run-suite.sh # Full test orchestration
105+
│ ├── run-tests.js # Unified test runner
106+
│ ├── config-loader.js # YAML configuration loader
83107
│ └── cleanup-suite.sh # Cleanup utilities
84108
└── tests/ # Jest test files
85-
── v1/
86-
└── firestore.test.ts
109+
── v1/ # V1 test suites
110+
└── v2/ # V2 test suites
87111
```
88112

89113
## How It Works
@@ -106,40 +130,66 @@ suite:
106130
document: "tests/{testId}"
107131
```
108132
109-
### 2. Code Generation
133+
### 2. SDK Preparation
134+
135+
The Firebase Functions SDK is packaged once:
136+
- Built from source in the parent directory
137+
- Packed as `firebase-functions-local.tgz`
138+
- Copied into each generated/functions directory during generation
139+
- Referenced locally in package.json as `file:firebase-functions-local.tgz`
140+
141+
This ensures the SDK is available during both local builds and Firebase cloud deployments.
142+
143+
### 3. Code Generation
110144

111145
The `generate.js` script:
112-
- Reads the suite YAML configuration
146+
- Reads the suite YAML configuration from config/v1/ or config/v2/
113147
- Generates a unique TEST_RUN_ID
114148
- Applies Handlebars templates with the configuration
115149
- Outputs static TypeScript code with baked-in TEST_RUN_ID
150+
- Copies the SDK tarball into the functions directory
116151

117-
Generated functions have names like: `firestoreDocumentOnCreateTests_t_1757979490_xkyqun`
152+
Generated functions have names like: `firestoreDocumentOnCreateTeststoi5krf7a`
118153

119-
### 3. Deployment & Testing
154+
### 4. Deployment & Testing
120155

121-
The `run-suite.sh` script orchestrates:
122-
1. **Generate**: Create function code from templates
123-
2. **Build**: Compile TypeScript to JavaScript
124-
3. **Deploy**: Deploy to Firebase with unique function names
125-
4. **Test**: Run Jest tests against deployed functions
126-
5. **Cleanup**: Automatic cleanup on exit (success or failure)
156+
The `run-tests.js` script orchestrates:
157+
1. **Pack SDK**: Package the SDK once at the start (if not already done)
158+
2. **Generate**: Create function code from templates for each suite
159+
3. **Build**: Compile TypeScript to JavaScript
160+
4. **Deploy**: Deploy to Firebase with unique function names
161+
5. **Test**: Run Jest tests against deployed functions
162+
6. **Cleanup**: Automatic cleanup after each suite (functions and generated files)
127163

128-
### 4. Cleanup
164+
### 5. Cleanup
129165

130166
Functions and test data are automatically cleaned up:
131-
- On test completion (via bash trap)
132-
- Manually via `cleanup-suite.sh`
133-
- Using saved artifacts for orphaned deployments
167+
- After each suite completes (success or failure)
168+
- Generated directory is cleared and recreated
169+
- Deployed functions are deleted if deployment was successful
170+
- Test data in Firestore/Database is cleaned up
134171

135172
## Commands
136173

137-
### Run Test Suite
174+
### Running Tests
138175
```bash
139-
./scripts/run-suite.sh <suite-name> [--save-artifact]
176+
# Run all tests sequentially
177+
npm run test:all:sequential
178+
179+
# Run specific version tests
180+
npm run test:v1:all # All v1 tests sequentially
181+
npm run test:v2:all # All v2 tests sequentially
182+
npm run test:v1:all:parallel # All v1 tests in parallel
183+
npm run test:v2:all:parallel # All v2 tests in parallel
184+
185+
# Run individual suites
186+
npm run test:firestore # Runs v1_firestore
187+
npm run run-tests v1_database # Direct suite name
188+
189+
# Run with options
190+
npm run run-tests -- --sequential v1_firestore v1_database
191+
npm run run-tests -- --filter=v2 --exclude=auth
140192
```
141-
- Runs complete test flow: generate → build → deploy → test → cleanup
142-
- `--save-artifact` saves metadata for future cleanup
143193

144194
### Generate Functions Only
145195
```bash
@@ -150,20 +200,16 @@ npm run generate <suite-name>
150200

151201
### Cleanup Functions
152202
```bash
153-
# Clean current deployment
154-
./scripts/cleanup-suite.sh
203+
# Clean up current test run
204+
npm run cleanup
155205
156-
# Clean specific test run
157-
./scripts/cleanup-suite.sh <TEST_RUN_ID>
206+
# List saved test artifacts
207+
npm run cleanup:list
158208
159-
# List saved artifacts
209+
# Manual cleanup with cleanup-suite.sh
210+
./scripts/cleanup-suite.sh <TEST_RUN_ID>
160211
./scripts/cleanup-suite.sh --list-artifacts
161-
162-
# Clean all saved test runs
163212
./scripts/cleanup-suite.sh --clean-artifacts
164-
165-
# Clean by pattern
166-
./scripts/cleanup-suite.sh --pattern <pattern> <project-id>
167213
```
168214

169215
## Adding New Test Suites
@@ -216,19 +262,32 @@ Format: `t_<timestamp>_<random>` (e.g., `t_1757979490_xkyqun`)
216262

217263
## Troubleshooting
218264

265+
### SDK Tarball Not Found
266+
- Run `npm run pack-for-integration-tests` from the root firebase-functions directory
267+
- This creates `integration_test_declarative/firebase-functions-local.tgz`
268+
- The SDK is packed once and reused for all suites
269+
219270
### Functions Not Deploying
220-
- Check that templates generate valid TypeScript
221-
- Verify project ID in suite YAML
222-
- Ensure Firebase CLI is authenticated
271+
- Check that the SDK tarball exists and was copied to generated/functions/
272+
- Verify project ID in suite YAML configuration
273+
- Ensure Firebase CLI is authenticated: `firebase projects:list`
274+
- Check deployment logs for specific errors
275+
276+
### Deployment Fails with "File not found" Error
277+
- The SDK tarball must be in generated/functions/ directory
278+
- Package.json should reference `file:firebase-functions-local.tgz` (local path)
279+
- Run `npm run generate <suite>` to regenerate with correct paths
223280

224281
### Tests Failing
225-
- Verify `sa.json` exists with proper permissions
226-
- Check that functions deployed successfully
282+
- Verify `sa.json` exists in integration_test_declarative/ directory
283+
- Check that functions deployed successfully: `firebase functions:list --project <project-id>`
227284
- Ensure TEST_RUN_ID environment variable is set
285+
- Check test logs in logs/ directory
228286

229287
### Cleanup Issues
230-
- Use `--list-artifacts` to find orphaned test runs
231-
- Manual cleanup: `firebase functions:delete <function-name> --project <project-id>`
288+
- Use `npm run cleanup:list` to find orphaned test runs
289+
- Manual cleanup: `firebase functions:delete <function-name> --project <project-id> --force`
290+
- Check for leftover test functions: `firebase functions:list --project functions-integration-tests | grep Test`
232291
- Check Firestore/Database console for orphaned test data
233292

234293
## Benefits

0 commit comments

Comments
 (0)