@@ -19,23 +19,41 @@ This framework uses a template-based code generation approach where:
19
19
3 . Generated code has static exports that Firebase CLI can discover
20
20
4 . Each test run gets isolated function instances
21
21
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
+
22
33
## Quick Start
23
34
24
35
``` 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
26
40
npm run test:v1:all
27
41
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
30
44
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
33
51
34
52
# Clean up after a test run
35
- ./scripts/ cleanup-suite.sh
53
+ npm run cleanup
36
54
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
39
57
```
40
58
41
59
## Configuration
@@ -65,25 +83,31 @@ To work around this:
65
83
```
66
84
integration_test_declarative/
67
85
├── 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
76
98
├── generated/ # Generated code (git-ignored)
77
99
│ ├── functions/ # Generated function code
100
+ │ │ └── firebase-functions-local.tgz # SDK tarball (copied)
78
101
│ ├── firebase.json # Generated Firebase config
79
102
│ └── .metadata.json # Generation metadata
80
103
├── scripts/
81
104
│ ├── 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
83
107
│ └── cleanup-suite.sh # Cleanup utilities
84
108
└── tests/ # Jest test files
85
- └ ── v1/
86
- └── firestore. test.ts
109
+ ├ ── v1/ # V1 test suites
110
+ └── v2/ # V2 test suites
87
111
```
88
112
89
113
## How It Works
@@ -106,40 +130,66 @@ suite:
106
130
document : " tests/{testId}"
107
131
` ` `
108
132
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
110
144
111
145
The `generate.js` script :
112
- - Reads the suite YAML configuration
146
+ - Reads the suite YAML configuration from config/v1/ or config/v2/
113
147
- Generates a unique TEST_RUN_ID
114
148
- Applies Handlebars templates with the configuration
115
149
- Outputs static TypeScript code with baked-in TEST_RUN_ID
150
+ - Copies the SDK tarball into the functions directory
116
151
117
- Generated functions have names like : ` firestoreDocumentOnCreateTests_t_1757979490_xkyqun `
152
+ Generated functions have names like : ` firestoreDocumentOnCreateTeststoi5krf7a `
118
153
119
- # ## 3 . Deployment & Testing
154
+ # ## 4 . Deployment & Testing
120
155
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)
127
163
128
- # ## 4 . Cleanup
164
+ # ## 5 . Cleanup
129
165
130
166
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
134
171
135
172
# # Commands
136
173
137
- # ## Run Test Suite
174
+ # ## Running Tests
138
175
` ` ` 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
140
192
` ` `
141
- - Runs complete test flow : generate → build → deploy → test → cleanup
142
- - ` --save-artifact` saves metadata for future cleanup
143
193
144
194
# ## Generate Functions Only
145
195
` ` ` bash
@@ -150,20 +200,16 @@ npm run generate <suite-name>
150
200
151
201
# ## Cleanup Functions
152
202
` ` ` bash
153
- # Clean current deployment
154
- ./scripts/ cleanup-suite.sh
203
+ # Clean up current test run
204
+ npm run cleanup
155
205
156
- # Clean specific test run
157
- ./scripts/cleanup-suite.sh <TEST_RUN_ID>
206
+ # List saved test artifacts
207
+ npm run cleanup:list
158
208
159
- # List saved artifacts
209
+ # Manual cleanup with cleanup-suite.sh
210
+ ./scripts/cleanup-suite.sh <TEST_RUN_ID>
160
211
./scripts/cleanup-suite.sh --list-artifacts
161
-
162
- # Clean all saved test runs
163
212
./scripts/cleanup-suite.sh --clean-artifacts
164
-
165
- # Clean by pattern
166
- ./scripts/cleanup-suite.sh --pattern <pattern> <project-id>
167
213
` ` `
168
214
169
215
# # Adding New Test Suites
@@ -216,19 +262,32 @@ Format: `t_<timestamp>_<random>` (e.g., `t_1757979490_xkyqun`)
216
262
217
263
# # Troubleshooting
218
264
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
+
219
270
# ## 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
223
280
224
281
# ## 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> `
227
284
- Ensure TEST_RUN_ID environment variable is set
285
+ - Check test logs in logs/ directory
228
286
229
287
# ## 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`
232
291
- Check Firestore/Database console for orphaned test data
233
292
234
293
# # Benefits
0 commit comments