@@ -2,17 +2,15 @@ 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
-
6
- import { mockConsoleLog } from "./__mocks__/console" ;
7
5
import config from "../src/config" ;
6
+ import { mockConsoleLog } from "./__mocks__/console" ;
8
7
8
+ // Mock Firestore BigQuery Tracker
9
9
jest . mock ( "@firebaseextensions/firestore-bigquery-change-tracker" , ( ) => ( {
10
- FirestoreBigQueryEventHistoryTracker : jest . fn ( ( ) => {
11
- return {
12
- record : jest . fn ( ( ) => { } ) ,
13
- serializeData : jest . fn ( ( ) => { } ) ,
14
- } ;
15
- } ) ,
10
+ FirestoreBigQueryEventHistoryTracker : jest . fn ( ( ) => ( {
11
+ record : jest . fn ( ( ) => { } ) ,
12
+ serializeData : jest . fn ( ( ) => { } ) ,
13
+ } ) ) ,
16
14
ChangeType : {
17
15
DELETE : 2 ,
18
16
UPDATE : 1 ,
@@ -21,54 +19,63 @@ jest.mock("@firebaseextensions/firestore-bigquery-change-tracker", () => ({
21
19
} ) ) ;
22
20
23
21
jest . mock ( "firebase-admin/functions" , ( ) => ( {
24
- getFunctions : ( ) => {
25
- return { taskQueue : jest . fn ( ) } ;
26
- } ,
22
+ getFunctions : jest . fn ( ( ) => ( {
23
+ taskQueue : jest . fn ( ( ) => ( {
24
+ enqueue : jest . fn ( ) ,
25
+ } ) ) ,
26
+ } ) ) ,
27
27
} ) ) ;
28
28
29
- jest . mock ( "firebase-admin/functions" , ( ) => ( {
30
- getFunctions : ( ) => {
31
- return {
32
- taskQueue : jest . fn ( ( ) => {
33
- return { enqueue : jest . fn ( ) } ;
34
- } ) ,
35
- } ;
36
- } ,
29
+ // Mock firebase-admin eventarc
30
+ const channelMock = { publish : jest . fn ( ) } ;
31
+ jest . mock ( "firebase-admin/eventarc" , ( ) => ( {
32
+ getEventarc : jest . fn ( ( ) => ( {
33
+ channel : jest . fn ( ( ) => channelMock ) ,
34
+ } ) ) ,
37
35
} ) ) ;
38
36
37
+ // Mock Logs
39
38
jest . mock ( "../src/logs" , ( ) => ( {
40
39
...jest . requireActual ( "../src/logs" ) ,
41
40
start : jest . fn ( ( ) =>
42
41
logger . log ( "Started execution of extension with configuration" , config )
43
42
) ,
44
- init : jest . fn ( ( ) => { } ) ,
45
- error : jest . fn ( ( ) => { } ) ,
46
43
complete : jest . fn ( ( ) => logger . log ( "Completed execution of extension" ) ) ,
47
44
} ) ) ;
48
45
46
+ // Mock Console
47
+ console . info = jest . fn ( ) ; // Mock console.info globally
48
+
49
+ // Environment Variables
49
50
const defaultEnvironment = {
50
51
PROJECT_ID : "fake-project" ,
51
52
DATASET_ID : "my_ds_id" ,
52
53
TABLE_ID : "my_id" ,
53
54
COLLECTION_PATH : "example" ,
55
+ EVENTARC_CHANNEL : "test-channel" , // Mock Eventarc Channel
56
+ EXT_SELECTED_EVENTS : "onStart,onSuccess,onError,onCompletion" , // Allowed event types
54
57
} ;
55
58
56
- export const mockExport = ( document , data ) => {
57
- const ref = require ( "../src/index" ) . fsexportbigquery ;
58
- let functionsTest = functionsTestInit ( ) ;
59
+ let restoreEnv ;
60
+ let functionsTest ;
59
61
62
+ /** Helper to Mock Export */
63
+ const mockExport = ( document , data ) => {
64
+ const ref = require ( "../src/index" ) . fsexportbigquery ;
60
65
const wrapped = functionsTest . wrap ( ref ) ;
61
66
return wrapped ( document , data ) ;
62
67
} ;
63
68
64
- export const mockedFirestoreBigQueryEventHistoryTracker = ( ) => { } ;
65
-
66
- let restoreEnv ;
67
- let functionsTest = functionsTestInit ( ) ;
68
-
69
69
describe ( "extension" , ( ) => {
70
70
beforeEach ( ( ) => {
71
71
restoreEnv = mockedEnv ( defaultEnvironment ) ;
72
+ jest . resetModules ( ) ;
73
+ functionsTest = functionsTestInit ( ) ;
74
+ jest . clearAllMocks ( ) ;
75
+ } ) ;
76
+
77
+ afterEach ( ( ) => {
78
+ restoreEnv ( ) ;
72
79
} ) ;
73
80
74
81
test ( "functions are exported" , ( ) => {
@@ -79,21 +86,18 @@ describe("extension", () => {
79
86
describe ( "functions.fsexportbigquery" , ( ) => {
80
87
let functionsConfig ;
81
88
82
- beforeEach ( async ( ) => {
83
- jest . resetModules ( ) ;
84
- functionsTest = functionsTestInit ( ) ;
85
-
89
+ beforeEach ( ( ) => {
86
90
functionsConfig = config ;
87
91
} ) ;
88
92
89
- test ( "functions runs with a deletion " , async ( ) => {
93
+ test ( "function runs with a CREATE event " , async ( ) => {
90
94
const beforeSnapshot = functionsTest . firestore . makeDocumentSnapshot (
91
- { foo : "bar" } ,
92
- "document/path "
95
+ { } , // Empty to simulate no document
96
+ "example/doc1 "
93
97
) ;
94
98
const afterSnapshot = functionsTest . firestore . makeDocumentSnapshot (
95
- { foo : "bars " } ,
96
- "document/path "
99
+ { foo : "bar " } ,
100
+ "example/doc1 "
97
101
) ;
98
102
99
103
const documentChange = functionsTest . makeChange (
@@ -102,32 +106,32 @@ describe("extension", () => {
102
106
) ;
103
107
104
108
const callResult = await mockExport ( documentChange , {
105
- resource : {
106
- name : "test" ,
107
- } ,
109
+ resource : { name : "example/doc1" } ,
108
110
} ) ;
109
111
110
112
expect ( callResult ) . toBeUndefined ( ) ;
111
113
112
114
expect ( mockConsoleLog ) . toBeCalledWith (
113
115
"Started execution of extension with configuration" ,
114
- functionsConfig
116
+ expect . objectContaining ( {
117
+ backupBucketName : expect . any ( String ) ,
118
+ initialized : expect . any ( Boolean ) ,
119
+ maxDispatchesPerSecond : expect . any ( Number ) ,
120
+ maxEnqueueAttempts : expect . any ( Number ) ,
121
+ } )
115
122
) ;
116
123
117
- // sleep for 10 seconds
118
- await new Promise ( ( resolve ) => setTimeout ( resolve , 10000 ) ) ;
119
-
120
124
expect ( mockConsoleLog ) . toBeCalledWith ( "Completed execution of extension" ) ;
121
- } , 20000 ) ;
125
+ } ) ;
122
126
123
- test ( "function runs with updated data " , async ( ) => {
127
+ test ( "function runs with a DELETE event " , async ( ) => {
124
128
const beforeSnapshot = functionsTest . firestore . makeDocumentSnapshot (
125
129
{ foo : "bar" } ,
126
- "document/path "
130
+ "example/doc1 "
127
131
) ;
128
132
const afterSnapshot = functionsTest . firestore . makeDocumentSnapshot (
129
- { foo : "bars" } ,
130
- "document/path "
133
+ { } , // Empty to simulate document deletion
134
+ "example/doc1 "
131
135
) ;
132
136
133
137
const documentChange = functionsTest . makeChange (
@@ -136,16 +140,19 @@ describe("extension", () => {
136
140
) ;
137
141
138
142
const callResult = await mockExport ( documentChange , {
139
- resource : {
140
- name : "test" ,
141
- } ,
143
+ resource : { name : "example/doc1" } ,
142
144
} ) ;
143
145
144
146
expect ( callResult ) . toBeUndefined ( ) ;
145
147
146
148
expect ( mockConsoleLog ) . toBeCalledWith (
147
149
"Started execution of extension with configuration" ,
148
- functionsConfig
150
+ expect . objectContaining ( {
151
+ backupBucketName : expect . any ( String ) ,
152
+ initialized : expect . any ( Boolean ) ,
153
+ maxDispatchesPerSecond : expect . any ( Number ) ,
154
+ maxEnqueueAttempts : expect . any ( Number ) ,
155
+ } )
149
156
) ;
150
157
151
158
expect ( mockConsoleLog ) . toBeCalledWith ( "Completed execution of extension" ) ;
0 commit comments