Skip to content

Commit d78c5a1

Browse files
committed
chore: refactor tests to avoid re-initializing admin server
1 parent 40595d7 commit d78c5a1

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

src/test/common.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Registry } from 'prom-client'
2+
3+
import app from '../admin-app'
4+
5+
export const adminApp = app({}, { register: new Registry() })

src/test/tenant.test.ts

+23-25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict'
2-
import { Registry } from 'prom-client'
3-
4-
import app from '../admin-app'
52
import dotenv from 'dotenv'
63
import * as migrate from '../utils/migrate'
74
import { knex } from '../utils/multitenant-db'
5+
import { adminApp } from './common'
86

97
dotenv.config({ path: '.env.test' })
108

@@ -30,7 +28,7 @@ beforeAll(async () => {
3028
})
3129

3230
afterEach(async () => {
33-
await app({}, { register: new Registry() }).inject({
31+
await adminApp.inject({
3432
method: 'DELETE',
3533
url: '/tenants/abc',
3634
headers: {
@@ -45,15 +43,15 @@ afterAll(async () => {
4543

4644
describe('Tenant configs', () => {
4745
test('Get all tenant configs', async () => {
48-
await app({}, { register: new Registry() }).inject({
46+
await adminApp.inject({
4947
method: 'POST',
5048
url: `/tenants/abc`,
5149
payload,
5250
headers: {
5351
apikey: process.env.ADMIN_API_KEYS,
5452
},
5553
})
56-
const response = await app({}, { register: new Registry() }).inject({
54+
const response = await adminApp.inject({
5755
method: 'GET',
5856
url: `/tenants`,
5957
headers: {
@@ -71,7 +69,7 @@ describe('Tenant configs', () => {
7169
})
7270

7371
test('Get nonexistent tenant config', async () => {
74-
const response = await app({}, { register: new Registry() }).inject({
72+
const response = await adminApp.inject({
7573
method: 'GET',
7674
url: `/tenants/abc`,
7775
headers: {
@@ -82,15 +80,15 @@ describe('Tenant configs', () => {
8280
})
8381

8482
test('Get existing tenant config', async () => {
85-
await app({}, { register: new Registry() }).inject({
83+
await adminApp.inject({
8684
method: 'POST',
8785
url: `/tenants/abc`,
8886
payload,
8987
headers: {
9088
apikey: process.env.ADMIN_API_KEYS,
9189
},
9290
})
93-
const response = await app({}, { register: new Registry() }).inject({
91+
const response = await adminApp.inject({
9492
method: 'GET',
9593
url: `/tenants/abc`,
9694
headers: {
@@ -103,7 +101,7 @@ describe('Tenant configs', () => {
103101
})
104102

105103
test('Insert tenant config without required properties', async () => {
106-
const response = await app({}, { register: new Registry() }).inject({
104+
const response = await adminApp.inject({
107105
method: 'POST',
108106
url: `/tenants/abc`,
109107
payload: {},
@@ -115,7 +113,7 @@ describe('Tenant configs', () => {
115113
})
116114

117115
test('Insert tenant config twice', async () => {
118-
const firstInsertResponse = await app({}, { register: new Registry() }).inject({
116+
const firstInsertResponse = await adminApp.inject({
119117
method: 'POST',
120118
url: `/tenants/abc`,
121119
payload,
@@ -124,7 +122,7 @@ describe('Tenant configs', () => {
124122
},
125123
})
126124
expect(firstInsertResponse.statusCode).toBe(201)
127-
const secondInsertResponse = await app({}, { register: new Registry() }).inject({
125+
const secondInsertResponse = await adminApp.inject({
128126
method: 'POST',
129127
url: `/tenants/abc`,
130128
payload,
@@ -136,15 +134,15 @@ describe('Tenant configs', () => {
136134
})
137135

138136
test('Update tenant config', async () => {
139-
await app({}, { register: new Registry() }).inject({
137+
await adminApp.inject({
140138
method: 'POST',
141139
url: `/tenants/abc`,
142140
payload,
143141
headers: {
144142
apikey: process.env.ADMIN_API_KEYS,
145143
},
146144
})
147-
const patchResponse = await app({}, { register: new Registry() }).inject({
145+
const patchResponse = await adminApp.inject({
148146
method: 'PATCH',
149147
url: `/tenants/abc`,
150148
payload: payload2,
@@ -153,7 +151,7 @@ describe('Tenant configs', () => {
153151
},
154152
})
155153
expect(patchResponse.statusCode).toBe(204)
156-
const getResponse = await app({}, { register: new Registry() }).inject({
154+
const getResponse = await adminApp.inject({
157155
method: 'GET',
158156
url: `/tenants/abc`,
159157
headers: {
@@ -165,15 +163,15 @@ describe('Tenant configs', () => {
165163
})
166164

167165
test('Update tenant config partially', async () => {
168-
await app({}, { register: new Registry() }).inject({
166+
await adminApp.inject({
169167
method: 'POST',
170168
url: `/tenants/abc`,
171169
payload,
172170
headers: {
173171
apikey: process.env.ADMIN_API_KEYS,
174172
},
175173
})
176-
const patchResponse = await app({}, { register: new Registry() }).inject({
174+
const patchResponse = await adminApp.inject({
177175
method: 'PATCH',
178176
url: `/tenants/abc`,
179177
payload: { fileSizeLimit: 2 },
@@ -182,7 +180,7 @@ describe('Tenant configs', () => {
182180
},
183181
})
184182
expect(patchResponse.statusCode).toBe(204)
185-
const getResponse = await app({}, { register: new Registry() }).inject({
183+
const getResponse = await adminApp.inject({
186184
method: 'GET',
187185
url: `/tenants/abc`,
188186
headers: {
@@ -194,7 +192,7 @@ describe('Tenant configs', () => {
194192
})
195193

196194
test('Upsert tenant config', async () => {
197-
const firstPutResponse = await app({}, { register: new Registry() }).inject({
195+
const firstPutResponse = await adminApp.inject({
198196
method: 'PUT',
199197
url: `/tenants/abc`,
200198
payload,
@@ -203,7 +201,7 @@ describe('Tenant configs', () => {
203201
},
204202
})
205203
expect(firstPutResponse.statusCode).toBe(204)
206-
const firstGetResponse = await app({}, { register: new Registry() }).inject({
204+
const firstGetResponse = await adminApp.inject({
207205
method: 'GET',
208206
url: `/tenants/abc`,
209207
headers: {
@@ -212,7 +210,7 @@ describe('Tenant configs', () => {
212210
})
213211
const firstGetResponseJSON = JSON.parse(firstGetResponse.body)
214212
expect(firstGetResponseJSON).toEqual(payload)
215-
const secondPutResponse = await app({}, { register: new Registry() }).inject({
213+
const secondPutResponse = await adminApp.inject({
216214
method: 'PUT',
217215
url: `/tenants/abc`,
218216
payload: payload2,
@@ -221,7 +219,7 @@ describe('Tenant configs', () => {
221219
},
222220
})
223221
expect(secondPutResponse.statusCode).toBe(204)
224-
const secondGetResponse = await app({}, { register: new Registry() }).inject({
222+
const secondGetResponse = await adminApp.inject({
225223
method: 'GET',
226224
url: `/tenants/abc`,
227225
headers: {
@@ -233,23 +231,23 @@ describe('Tenant configs', () => {
233231
})
234232

235233
test('Delete tenant config', async () => {
236-
await app({}, { register: new Registry() }).inject({
234+
await adminApp.inject({
237235
method: 'POST',
238236
url: `/tenants/abc`,
239237
payload,
240238
headers: {
241239
apikey: process.env.ADMIN_API_KEYS,
242240
},
243241
})
244-
const deleteResponse = await app({}, { register: new Registry() }).inject({
242+
const deleteResponse = await adminApp.inject({
245243
method: 'DELETE',
246244
url: '/tenants/abc',
247245
headers: {
248246
apikey: process.env.ADMIN_API_KEYS,
249247
},
250248
})
251249
expect(deleteResponse.statusCode).toBe(204)
252-
const getResponse = await app({}, { register: new Registry() }).inject({
250+
const getResponse = await adminApp.inject({
253251
method: 'GET',
254252
url: `/tenants/abc`,
255253
headers: {

src/test/x-forwarded-host.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
import { Registry } from 'prom-client'
33

4-
import adminApp from '../admin-app'
4+
import { adminApp } from './common'
55
import app from '../app'
66
import dotenv from 'dotenv'
77
import * as migrate from '../utils/migrate'
@@ -28,7 +28,7 @@ afterAll(async () => {
2828

2929
describe('with X-Forwarded-Host header', () => {
3030
test('PostgREST URL is constructed using X-Forwarded-Host if regexp matches', async () => {
31-
await adminApp({}, { register: new Registry() }).inject({
31+
await adminApp.inject({
3232
method: 'POST',
3333
url: `/tenants/abcdefghijklmnopqrst`,
3434
payload: {
@@ -52,7 +52,7 @@ describe('with X-Forwarded-Host header', () => {
5252
expect(response.statusCode).toBe(400)
5353
const responseJSON = JSON.parse(response.body)
5454
expect(responseJSON.message).toContain('http://abcdefghijklmnopqrst.supabase.co/rest/v1')
55-
await adminApp({}, { register: new Registry() }).inject({
55+
await adminApp.inject({
5656
method: 'DELETE',
5757
url: '/tenants/abcdefghijklmnopqrst',
5858
headers: {

0 commit comments

Comments
 (0)