@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/test/base-test.js +21 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbvision/fastify-firestore-service",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "Web Framework using Fastify and Firestore ORM",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
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 makeGotMockValueFromPromise (promise) {
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 makeGotMockValue (body, status, callback) {
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 got will NEVER return synchronously. This ensures
86
- // functions which call got() never resolve synchronously (which can
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(makeGotMockValue(body, statusCode, callback))
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 unmockedGot = jest.requireActual('../src/got')
134
- return unmockedGot(url, options)
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 makeGotMockValueFromPromise(desiredHTTPResponse)
148
+ return makeNodeFetchMockValueFromPromise(desiredHTTPResponse)
139
149
  }
140
- return makeGotMockValue(
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 got() request: ${JSON.stringify(request)}`)
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
  }