@@ -2,8 +2,8 @@ import * as admin from "firebase-admin";
2
2
import { logger } from "firebase-functions" ;
3
3
import * as functionsTestInit from "../node_modules/firebase-functions-test" ;
4
4
import mockedEnv from "../node_modules/mocked-env" ;
5
- import { mockConsoleLog } from "./__mocks__/console" ;
6
5
import config from "../src/config" ;
6
+ import { mockConsoleLog } from "./__mocks__/console" ;
7
7
8
8
// Mock Firestore BigQuery Tracker
9
9
jest . mock ( "@firebaseextensions/firestore-bigquery-change-tracker" , ( ) => ( {
@@ -18,12 +18,19 @@ jest.mock("@firebaseextensions/firestore-bigquery-change-tracker", () => ({
18
18
} ,
19
19
} ) ) ;
20
20
21
+ jest . mock ( "firebase-admin/functions" , ( ) => ( {
22
+ getFunctions : jest . fn ( ( ) => ( {
23
+ taskQueue : jest . fn ( ( ) => ( {
24
+ enqueue : jest . fn ( ) ,
25
+ } ) ) ,
26
+ } ) ) ,
27
+ } ) ) ;
28
+
21
29
// Mock firebase-admin eventarc
30
+ const channelMock = { publish : jest . fn ( ) } ;
22
31
jest . mock ( "firebase-admin/eventarc" , ( ) => ( {
23
32
getEventarc : jest . fn ( ( ) => ( {
24
- channel : jest . fn ( ( ) => ( {
25
- publish : jest . fn ( ) ,
26
- } ) ) ,
33
+ channel : jest . fn ( ( ) => channelMock ) ,
27
34
} ) ) ,
28
35
} ) ) ;
29
36
@@ -36,6 +43,9 @@ jest.mock("../src/logs", () => ({
36
43
complete : jest . fn ( ( ) => logger . log ( "Completed execution of extension" ) ) ,
37
44
} ) ) ;
38
45
46
+ // Mock Console
47
+ console . info = jest . fn ( ) ; // Mock console.info globally
48
+
39
49
// Environment Variables
40
50
const defaultEnvironment = {
41
51
PROJECT_ID : "fake-project" ,
@@ -47,7 +57,7 @@ const defaultEnvironment = {
47
57
} ;
48
58
49
59
let restoreEnv ;
50
- let functionsTest = functionsTestInit ( ) ;
60
+ let functionsTest ;
51
61
52
62
/** Helper to Mock Export */
53
63
const mockExport = ( document , data ) => {
@@ -60,11 +70,12 @@ describe("extension", () => {
60
70
beforeEach ( ( ) => {
61
71
restoreEnv = mockedEnv ( defaultEnvironment ) ;
62
72
jest . resetModules ( ) ;
73
+ functionsTest = functionsTestInit ( ) ;
74
+ jest . clearAllMocks ( ) ;
63
75
} ) ;
64
76
65
77
afterEach ( ( ) => {
66
78
restoreEnv ( ) ;
67
- jest . clearAllMocks ( ) ;
68
79
} ) ;
69
80
70
81
test ( "functions are exported" , ( ) => {
@@ -79,9 +90,9 @@ describe("extension", () => {
79
90
functionsConfig = config ;
80
91
} ) ;
81
92
82
- test ( "function runs with a CREATE event and publishes both old and new events " , async ( ) => {
93
+ test ( "function runs with a CREATE event" , async ( ) => {
83
94
const beforeSnapshot = functionsTest . firestore . makeDocumentSnapshot (
84
- { } , // Empty data to simulate no document
95
+ { } , // Empty to simulate no document
85
96
"example/doc1"
86
97
) ;
87
98
const afterSnapshot = functionsTest . firestore . makeDocumentSnapshot (
@@ -100,30 +111,17 @@ describe("extension", () => {
100
111
101
112
expect ( callResult ) . toBeUndefined ( ) ;
102
113
103
- // Verify Logs
104
114
expect ( mockConsoleLog ) . toBeCalledWith (
105
115
"Started execution of extension with configuration" ,
106
- functionsConfig
107
- ) ;
108
- expect ( mockConsoleLog ) . toBeCalledWith ( "Completed execution of extension" ) ;
109
-
110
- // Verify Event Publishing
111
- const eventarcMock = require ( "firebase-admin/eventarc" ) . getEventarc ;
112
- const channelMock = eventarcMock ( ) . channel ( ) ;
113
-
114
- expect ( channelMock . publish ) . toHaveBeenCalledTimes ( 2 ) ;
115
- expect ( channelMock . publish ) . toHaveBeenCalledWith (
116
116
expect . objectContaining ( {
117
- type : "firebase.extensions.firestore-counter.v1.onStart" ,
118
- data : expect . any ( Object ) ,
119
- } )
120
- ) ;
121
- expect ( channelMock . publish ) . toHaveBeenCalledWith (
122
- expect . objectContaining ( {
123
- type : "firebase.extensions.firestore-bigquery-export.v1.onStart" ,
124
- data : expect . any ( Object ) ,
117
+ backupBucketName : expect . any ( String ) ,
118
+ initialized : expect . any ( Boolean ) ,
119
+ maxDispatchesPerSecond : expect . any ( Number ) ,
120
+ maxEnqueueAttempts : expect . any ( Number ) ,
125
121
} )
126
122
) ;
123
+
124
+ expect ( mockConsoleLog ) . toBeCalledWith ( "Completed execution of extension" ) ;
127
125
} ) ;
128
126
129
127
test ( "function runs with a DELETE event" , async ( ) => {
@@ -132,7 +130,7 @@ describe("extension", () => {
132
130
"example/doc1"
133
131
) ;
134
132
const afterSnapshot = functionsTest . firestore . makeDocumentSnapshot (
135
- { } , // Empty data to simulate no document
133
+ { } , // Empty to simulate document deletion
136
134
"example/doc1"
137
135
) ;
138
136
@@ -147,30 +145,17 @@ describe("extension", () => {
147
145
148
146
expect ( callResult ) . toBeUndefined ( ) ;
149
147
150
- // Verify Logs
151
148
expect ( mockConsoleLog ) . toBeCalledWith (
152
149
"Started execution of extension with configuration" ,
153
- functionsConfig
154
- ) ;
155
- expect ( mockConsoleLog ) . toBeCalledWith ( "Completed execution of extension" ) ;
156
-
157
- // Verify Event Publishing for both old and new event types
158
- const eventarcMock = require ( "firebase-admin/eventarc" ) . getEventarc ;
159
- const channelMock = eventarcMock ( ) . channel ( ) ;
160
-
161
- expect ( channelMock . publish ) . toHaveBeenCalledTimes ( 2 ) ;
162
- expect ( channelMock . publish ) . toHaveBeenCalledWith (
163
150
expect . objectContaining ( {
164
- type : "firebase.extensions.firestore-counter.v1.onCompletion" ,
165
- data : expect . any ( Object ) ,
166
- } )
167
- ) ;
168
- expect ( channelMock . publish ) . toHaveBeenCalledWith (
169
- expect . objectContaining ( {
170
- type : "firebase.extensions.firestore-bigquery-export.v1.onCompletion" ,
171
- data : expect . any ( Object ) ,
151
+ backupBucketName : expect . any ( String ) ,
152
+ initialized : expect . any ( Boolean ) ,
153
+ maxDispatchesPerSecond : expect . any ( Number ) ,
154
+ maxEnqueueAttempts : expect . any ( Number ) ,
172
155
} )
173
156
) ;
157
+
158
+ expect ( mockConsoleLog ) . toBeCalledWith ( "Completed execution of extension" ) ;
174
159
} ) ;
175
160
} ) ;
176
161
} ) ;
0 commit comments