@pbvision/fastify-firestore-service 0.0.15 → 0.0.17
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 +21 -12
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
|
|
@@ -18,6 +24,11 @@ class BaseAppTest extends BaseTest {
|
|
|
18
24
|
return func
|
|
19
25
|
}
|
|
20
26
|
|
|
27
|
+
beforeEach () {
|
|
28
|
+
this.fetchMock = fetchWrapper.__mock
|
|
29
|
+
fetchWrapper.__mock.mockClear()
|
|
30
|
+
}
|
|
31
|
+
|
|
21
32
|
async beforeAll () {
|
|
22
33
|
const makeService = await this.getMakeServiceFunc()
|
|
23
34
|
this.fastify = FASTIFY_CACHE ?? await makeService()
|
|
@@ -60,7 +71,7 @@ function makeHeadersObj (headers) {
|
|
|
60
71
|
|
|
61
72
|
// the promise input conveniently matches the promise produced by supertest
|
|
62
73
|
// so you can pass the output of app.post(), etc. as the promise here as is
|
|
63
|
-
function
|
|
74
|
+
function makeNodeFetchMockValueFromPromise (promise) {
|
|
64
75
|
const mockValue = new Promise(resolve => {
|
|
65
76
|
promise.then(desiredHttpResponse => {
|
|
66
77
|
let body = desiredHttpResponse.text || desiredHttpResponse.body || ''
|
|
@@ -79,11 +90,11 @@ function makeGotMockValueFromPromise (promise) {
|
|
|
79
90
|
return mockValue
|
|
80
91
|
}
|
|
81
92
|
|
|
82
|
-
function
|
|
93
|
+
function makeNodeFetchMockValue (body, status, callback) {
|
|
83
94
|
const mockValue = new Promise(resolve => {
|
|
84
95
|
// setTimeout is used so that this promise does not synchronously resolve
|
|
85
|
-
// because unmocked
|
|
86
|
-
// functions which call
|
|
96
|
+
// because unmocked fetch will NEVER return synchronously. This ensures
|
|
97
|
+
// functions which call fetch() never resolve synchronously (which can
|
|
87
98
|
// change their behavior... e.g., allow them to throw when called,
|
|
88
99
|
// instead of only rejecting later when await'ed).
|
|
89
100
|
// https://github.com/facebook/jest/issues/6028 (since jest 21.x)
|
|
@@ -114,7 +125,7 @@ function mockNodeFetch () {
|
|
|
114
125
|
})
|
|
115
126
|
|
|
116
127
|
nodeFetchMock.mockResp = (body = '', statusCode = 200, callback) => {
|
|
117
|
-
nodeFetchMock.mockReturnValue(
|
|
128
|
+
nodeFetchMock.mockReturnValue(makeNodeFetchMockValue(body, statusCode, callback))
|
|
118
129
|
}
|
|
119
130
|
|
|
120
131
|
/**
|
|
@@ -126,24 +137,23 @@ function mockNodeFetch () {
|
|
|
126
137
|
*/
|
|
127
138
|
nodeFetchMock.mockRespWithCallback = (...callbacks) => {
|
|
128
139
|
nodeFetchMock.mockImplementation(request => {
|
|
129
|
-
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/fetch-wrapper.js')
|
|
144
|
+
return unmockedFetch(request)
|
|
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)
|
|
144
154
|
}
|
|
145
155
|
}
|
|
146
|
-
throw new Error(`un-mocked
|
|
156
|
+
throw new Error(`un-mocked fetchWrapper() request: ${JSON.stringify(request)}`)
|
|
147
157
|
})
|
|
148
158
|
}
|
|
149
159
|
|
|
@@ -167,6 +177,5 @@ function mockNodeFetch () {
|
|
|
167
177
|
export {
|
|
168
178
|
BaseAppTest,
|
|
169
179
|
BaseTest,
|
|
170
|
-
mockNodeFetch,
|
|
171
180
|
runTests
|
|
172
181
|
}
|