Skip to content

Commit 72faac8

Browse files
authored
chore: update tests so they exit gracefully and compute more accurate coverage (#682)
* chore: update tests so they exit gracefully and compute more accurate coverage * disallow and remove floating promises * use await instead of return for tus
1 parent 2f0381d commit 72faac8

25 files changed

+438
-383
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ module.exports = {
55
parserOptions: {
66
ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
77
sourceType: 'module', // Allows for the use of imports
8+
"project": "./tsconfig.json",
89
},
910
rules: {
11+
'@typescript-eslint/no-floating-promises': 'error',
1012
'@typescript-eslint/no-explicit-any': 'warn',
1113
'@typescript-eslint/no-unused-vars': [
1214
'warn',

jest.sequencer.cjs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
// eslint-disable-next-line @typescript-eslint/no-var-requires
22
const Sequencer = require('@jest/test-sequencer').default
33

4-
// https://stackoverflow.com/a/68009048
5-
6-
const isRLSTest = (test) => {
7-
return test.path.includes('rls')
8-
}
9-
10-
const isTusTest = (test) => {
11-
return test.path.includes('tus')
12-
}
13-
14-
const isS3Test = (test) => {
15-
return test.path.includes('s3')
16-
}
4+
// order to sort tests based on name matching
5+
const testOrder = [
6+
'jwt.test.ts', // needs to run before it is imported by other libraries for proper coverage
7+
'*', // all other tests not matched
8+
'tus.test.ts',
9+
's3-protocol.test.ts',
10+
'rls.test.ts',
11+
]
1712

1813
class CustomSequencer extends Sequencer {
1914
sort(tests) {
20-
const copyTests = Array.from(tests)
21-
const normalTests = copyTests.filter((t) => !isRLSTest(t) && !isTusTest(t) && !isS3Test(t))
22-
const tusTests = copyTests.filter((t) => isTusTest(t))
23-
const s3Tests = copyTests.filter((t) => isS3Test(t))
24-
const rlsTests = copyTests.filter((t) => isRLSTest(t))
25-
return super
26-
.sort(normalTests)
27-
.concat(tusTests)
28-
.concat(s3Tests)
29-
.concat(rlsTests.sort((a, b) => (a.path > b.path ? 1 : -1)))
15+
const testBuckets = {}
16+
testOrder.forEach(k => {
17+
testBuckets[k] = []
18+
})
19+
20+
Array.from(tests).forEach(t => {
21+
const fileName = t.path.split('/').pop()
22+
const bucket = testBuckets[fileName] || testBuckets['*']
23+
bucket.push(t)
24+
})
25+
return testOrder.flatMap(k => super.sort(testBuckets[k]))
3026
}
3127
}
3228

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"migrations:types": "tsx src/scripts/migrations-types.ts",
1414
"docs:export": "tsx ./src/scripts/export-docs.ts",
1515
"test:dummy-data": "tsx -r dotenv/config ./src/test/db/import-dummy-data.ts",
16-
"test": "npm run infra:restart && npm run test:dummy-data && jest --runInBand --forceExit",
17-
"test:coverage": "npm run infra:restart && npm run test:dummy-data && jest --runInBand --coverage --forceExit",
16+
"test": "npm run infra:restart && npm run test:dummy-data && jest --no-cache --runInBand",
17+
"test:coverage": "npm run infra:restart && npm run test:dummy-data && jest --no-cache --runInBand --coverage",
1818
"eslint:check": "eslint 'src/**'",
1919
"infra:stop": "docker compose --project-directory . -f ./.docker/docker-compose-infra.yml down --remove-orphans",
2020
"infra:start": "docker compose --project-directory . -f ./.docker/docker-compose-infra.yml up -d && sleep 5 && npm run migration:run",

src/http/routes/admin/jwks.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import apiKey from '../../plugins/apikey'
33
import { jwksManager } from '@internal/database'
44
import { FromSchema } from 'json-schema-to-ts'
55
import { UrlSigningJwkGenerator } from '@internal/auth/generators/jwk-generator'
6+
import { logSchema } from '@internal/monitoring'
67

78
const addSchema = {
89
body: {
@@ -126,15 +127,22 @@ export default async function routes(fastify: FastifyInstance) {
126127
}
127128
)
128129

129-
fastify.post('/jwks/generate-all-missing', (request, reply) => {
130+
fastify.post('/jwks/generate-all-missing', async (request, reply) => {
130131
const { running, sent } = UrlSigningJwkGenerator.getGenerationStatus()
131132
if (running) {
132133
return reply
133134
.status(400)
134135
.send(`Generate missing jwks is already running, and has sent ${sent} items so far`)
135136
}
136137

137-
UrlSigningJwkGenerator.generateUrlSigningJwksOnAllTenants(request.signals.disconnect.signal)
138+
UrlSigningJwkGenerator.generateUrlSigningJwksOnAllTenants(
139+
request.signals.disconnect.signal
140+
).catch((e) => {
141+
logSchema.error(request.log, 'Error generating url signing jwks for all tenants', {
142+
type: 'jwk-generator',
143+
error: e,
144+
})
145+
})
138146
return reply.send({ started: true })
139147
})
140148

src/http/routes/tus/index.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ const authenticatedRoutes = fastifyPlugin(
235235
operation: { type: `${ROUTE_OPERATIONS.TUS_CREATE_UPLOAD}${options.operation || ''}` },
236236
},
237237
},
238-
(req, res) => {
239-
options.tusServer.handle(req.raw, res.raw)
238+
async (req, res) => {
239+
await options.tusServer.handle(req.raw, res.raw)
240240
}
241241
)
242242

@@ -248,8 +248,8 @@ const authenticatedRoutes = fastifyPlugin(
248248
operation: { type: `${ROUTE_OPERATIONS.TUS_CREATE_UPLOAD}${options.operation || ''}` },
249249
},
250250
},
251-
(req, res) => {
252-
options.tusServer.handle(req.raw, res.raw)
251+
async (req, res) => {
252+
await options.tusServer.handle(req.raw, res.raw)
253253
}
254254
)
255255

@@ -261,8 +261,8 @@ const authenticatedRoutes = fastifyPlugin(
261261
operation: { type: `${ROUTE_OPERATIONS.TUS_UPLOAD_PART}${options.operation || ''}` },
262262
},
263263
},
264-
(req, res) => {
265-
options.tusServer.handle(req.raw, res.raw)
264+
async (req, res) => {
265+
await options.tusServer.handle(req.raw, res.raw)
266266
}
267267
)
268268
fastify.patch(
@@ -276,8 +276,8 @@ const authenticatedRoutes = fastifyPlugin(
276276
operation: { type: `${ROUTE_OPERATIONS.TUS_UPLOAD_PART}${options.operation || ''}` },
277277
},
278278
},
279-
(req, res) => {
280-
options.tusServer.handle(req.raw, res.raw)
279+
async (req, res) => {
280+
await options.tusServer.handle(req.raw, res.raw)
281281
}
282282
)
283283
fastify.head(
@@ -288,8 +288,8 @@ const authenticatedRoutes = fastifyPlugin(
288288
operation: { type: `${ROUTE_OPERATIONS.TUS_GET_UPLOAD}${options.operation || ''}` },
289289
},
290290
},
291-
(req, res) => {
292-
options.tusServer.handle(req.raw, res.raw)
291+
async (req, res) => {
292+
await options.tusServer.handle(req.raw, res.raw)
293293
}
294294
)
295295
fastify.delete(
@@ -303,8 +303,8 @@ const authenticatedRoutes = fastifyPlugin(
303303
operation: { type: `${ROUTE_OPERATIONS.TUS_DELETE_UPLOAD}${options.operation || ''}` },
304304
},
305305
},
306-
(req, res) => {
307-
options.tusServer.handle(req.raw, res.raw)
306+
async (req, res) => {
307+
await options.tusServer.handle(req.raw, res.raw)
308308
}
309309
)
310310
})
@@ -341,8 +341,8 @@ const publicRoutes = fastifyPlugin(
341341
operation: { type: `${ROUTE_OPERATIONS.TUS_OPTIONS}${options.operation || ''}` },
342342
},
343343
},
344-
(req, res) => {
345-
options.tusServer.handle(req.raw, res.raw)
344+
async (req, res) => {
345+
await options.tusServer.handle(req.raw, res.raw)
346346
}
347347
)
348348

@@ -358,8 +358,8 @@ const publicRoutes = fastifyPlugin(
358358
operation: { type: `${ROUTE_OPERATIONS.TUS_OPTIONS}${options.operation || ''}` },
359359
},
360360
},
361-
(req, res) => {
362-
options.tusServer.handle(req.raw, res.raw)
361+
async (req, res) => {
362+
await options.tusServer.handle(req.raw, res.raw)
363363
}
364364
)
365365
})

src/internal/testing/generators/array.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ export async function eachParallel<T>(times: number, fn: (index: number) => Prom
77
return Promise.all(promises)
88
}
99

10-
export function pickRandomFromArray<T>(arr: T[]): T {
11-
return arr[Math.floor(Math.random() * arr.length)]
12-
}
10+
// export function pickRandomFromArray<T>(arr: T[]): T {
11+
// return arr[Math.floor(Math.random() * arr.length)]
12+
// }
1313

14-
export function pickRandomRangeFromArray<T>(arr: T[], range: number): T[] {
15-
if (arr.length <= range) {
16-
return arr
17-
}
14+
// export function pickRandomRangeFromArray<T>(arr: T[], range: number): T[] {
15+
// if (arr.length <= range) {
16+
// return arr
17+
// }
1818

19-
const result = new Set<T>()
20-
while (result.size < range) {
21-
result.add(pickRandomFromArray(arr))
22-
}
19+
// const result = new Set<T>()
20+
// while (result.size < range) {
21+
// result.add(pickRandomFromArray(arr))
22+
// }
2323

24-
return Array.from(result)
25-
}
24+
// return Array.from(result)
25+
// }

src/scripts/export-docs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ import app from '../app'
1313
await fs.writeFile('static/api.json', response.body)
1414

1515
await storageApp.close()
16-
})()
16+
})().catch(console.error)

src/scripts/migrate-call.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ import { getConfig } from '../config'
99
databaseUrl: databaseURL,
1010
upToMigration: dbMigrationFreezeAt,
1111
})
12-
})()
12+
})().catch(console.error)

src/scripts/orphan-client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ async function writeStreamToJsonArray(
177177
}
178178

179179
main()
180-
.catch((e) => {
181-
console.error('Error:', e)
182-
})
183180
.then(() => {
184181
console.log('Done')
185182
})
183+
.catch((e) => {
184+
console.error('Error:', e)
185+
})

src/storage/backend/s3/adapter.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,7 @@ export class S3Backend implements StorageBackendAdapter {
154154
},
155155
})
156156

157-
signal?.addEventListener(
158-
'abort',
159-
() => {
160-
upload.abort()
161-
},
162-
{ once: true }
163-
)
157+
signal?.addEventListener('abort', () => upload.abort(), { once: true })
164158

165159
if (tracingFeatures?.upload) {
166160
upload.on('httpUploadProgress', (progress: Progress) => {

0 commit comments

Comments
 (0)