@pbvision/fastify-firestore-service 0.0.14 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/test/base-test.js +25 -16
package/package.json
CHANGED
package/test/base-test.js
CHANGED
|
@@ -5,8 +5,14 @@ import { BaseTest, runTests } from '@pbvision/jest-unit-test'
|
|
|
5
5
|
import superagentDefaults from 'superagent-defaults'
|
|
6
6
|
import supertest from 'supertest'
|
|
7
7
|
|
|
8
|
+
import fetchWrapper from '../src/fetch-wrapper.js'
|
|
9
|
+
|
|
8
10
|
let FASTIFY_CACHE
|
|
9
11
|
|
|
12
|
+
beforeAll(() => {
|
|
13
|
+
fetchWrapper.__mock = mockNodeFetch()
|
|
14
|
+
})
|
|
15
|
+
|
|
10
16
|
afterAll(async () => {
|
|
11
17
|
await FASTIFY_CACHE?.close()
|
|
12
18
|
FASTIFY_CACHE = undefined
|
|
@@ -50,6 +56,10 @@ class BaseAppTest extends BaseTest {
|
|
|
50
56
|
}
|
|
51
57
|
})
|
|
52
58
|
}
|
|
59
|
+
|
|
60
|
+
afterEach () {
|
|
61
|
+
fetchWrapper.__mock.mockReset()
|
|
62
|
+
}
|
|
53
63
|
}
|
|
54
64
|
|
|
55
65
|
function makeHeadersObj (headers) {
|
|
@@ -60,7 +70,7 @@ function makeHeadersObj (headers) {
|
|
|
60
70
|
|
|
61
71
|
// the promise input conveniently matches the promise produced by supertest
|
|
62
72
|
// so you can pass the output of app.post(), etc. as the promise here as is
|
|
63
|
-
function
|
|
73
|
+
function makeNodeFetchMockValueFromPromise (promise) {
|
|
64
74
|
const mockValue = new Promise(resolve => {
|
|
65
75
|
promise.then(desiredHttpResponse => {
|
|
66
76
|
let body = desiredHttpResponse.text || desiredHttpResponse.body || ''
|
|
@@ -79,7 +89,7 @@ function makeGotMockValueFromPromise (promise) {
|
|
|
79
89
|
return mockValue
|
|
80
90
|
}
|
|
81
91
|
|
|
82
|
-
function
|
|
92
|
+
function makeNodeFetchMockValue (body, status, callback) {
|
|
83
93
|
const mockValue = new Promise(resolve => {
|
|
84
94
|
// setTimeout is used so that this promise does not synchronously resolve
|
|
85
95
|
// because unmocked got will NEVER return synchronously. This ensures
|
|
@@ -108,13 +118,13 @@ function makeGotMockValue (body, status, callback) {
|
|
|
108
118
|
return mockValue
|
|
109
119
|
}
|
|
110
120
|
|
|
111
|
-
function
|
|
112
|
-
const
|
|
121
|
+
function mockNodeFetch () {
|
|
122
|
+
const nodeFetchMock = jest.fn().mockImplementation(({ body }) => {
|
|
113
123
|
expect(body).toEqual(zlib.brotliCompressSync('321'))
|
|
114
124
|
})
|
|
115
125
|
|
|
116
|
-
|
|
117
|
-
|
|
126
|
+
nodeFetchMock.mockResp = (body = '', statusCode = 200, callback) => {
|
|
127
|
+
nodeFetchMock.mockReturnValue(makeNodeFetchMockValue(body, statusCode, callback))
|
|
118
128
|
}
|
|
119
129
|
|
|
120
130
|
/**
|
|
@@ -124,20 +134,20 @@ function mockGot () {
|
|
|
124
134
|
* to check and see if they have a mock response to use; if no callback,
|
|
125
135
|
* provides a mock response then an error will be thrown
|
|
126
136
|
*/
|
|
127
|
-
|
|
128
|
-
|
|
137
|
+
nodeFetchMock.mockRespWithCallback = (...callbacks) => {
|
|
138
|
+
nodeFetchMock.mockImplementation(request => {
|
|
129
139
|
const { url, ...options } = request
|
|
130
140
|
for (const callback of callbacks) {
|
|
131
141
|
const desiredHTTPResponse = callback(request)
|
|
132
142
|
if (desiredHTTPResponse === true) {
|
|
133
|
-
const
|
|
134
|
-
return
|
|
143
|
+
const unmockedFetch = jest.requireActual('../src/got')
|
|
144
|
+
return unmockedFetch(url, options)
|
|
135
145
|
}
|
|
136
146
|
if (desiredHTTPResponse) {
|
|
137
147
|
if (desiredHTTPResponse.then) {
|
|
138
|
-
return
|
|
148
|
+
return makeNodeFetchMockValueFromPromise(desiredHTTPResponse)
|
|
139
149
|
}
|
|
140
|
-
return
|
|
150
|
+
return makeNodeFetchMockValue(
|
|
141
151
|
// follows the names from supertest
|
|
142
152
|
desiredHTTPResponse.text || desiredHTTPResponse.body || '',
|
|
143
153
|
desiredHTTPResponse.status || 200)
|
|
@@ -148,11 +158,11 @@ function mockGot () {
|
|
|
148
158
|
}
|
|
149
159
|
|
|
150
160
|
// will respond to the next N requests with the specified N responses
|
|
151
|
-
|
|
161
|
+
nodeFetchMock.mockRespMulti = (...responses) => {
|
|
152
162
|
let idx = 0
|
|
153
163
|
function setupNextResponse () {
|
|
154
164
|
if (idx < responses.length) {
|
|
155
|
-
|
|
165
|
+
nodeFetchMock.mockResp(...responses[idx], setupNextResponse)
|
|
156
166
|
} else if (idx > responses.length) {
|
|
157
167
|
// exactly equal means we just got our last callback (okay)
|
|
158
168
|
throw new Error('more requests made than we had mock responses')
|
|
@@ -161,12 +171,11 @@ function mockGot () {
|
|
|
161
171
|
}
|
|
162
172
|
setupNextResponse()
|
|
163
173
|
}
|
|
164
|
-
return
|
|
174
|
+
return nodeFetchMock
|
|
165
175
|
}
|
|
166
176
|
|
|
167
177
|
export {
|
|
168
178
|
BaseAppTest,
|
|
169
179
|
BaseTest,
|
|
170
|
-
mockGot,
|
|
171
180
|
runTests
|
|
172
181
|
}
|