Backend CI / type-check, build, tests (push) Successful in 1m0s
Co-authored-by: Cursor <cursoragent@cursor.com>
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import 'reflect-metadata'
|
|
import assert from 'node:assert/strict'
|
|
import { HttpException, HttpStatus } from '@nestjs/common'
|
|
import { ApiExceptionFilter } from '../src/common/filters/api-exception.filter'
|
|
|
|
const filter = new ApiExceptionFilter()
|
|
|
|
function runFilter(exception: unknown): { status: number; body: { error: string } } {
|
|
let status = 0
|
|
let body: { error: string } = { error: '' }
|
|
const host = {
|
|
switchToHttp: () => ({
|
|
getResponse: () => ({
|
|
status(code: number) {
|
|
status = code
|
|
return {
|
|
send(payload: { error: string }) {
|
|
body = payload
|
|
},
|
|
}
|
|
},
|
|
}),
|
|
}),
|
|
}
|
|
filter.catch(exception, host as never)
|
|
return { status, body }
|
|
}
|
|
|
|
const prev = process.env.NODE_ENV
|
|
process.env.NODE_ENV = 'production'
|
|
|
|
const internal = runFilter(
|
|
new Error('SqliteError: constraint at /secret/path/app.db\n at foo.ts:1:1'),
|
|
)
|
|
assert.equal(internal.status, HttpStatus.INTERNAL_SERVER_ERROR)
|
|
assert.equal(internal.body.error, 'خطای سرور')
|
|
assert.ok(!JSON.stringify(internal.body).includes('app.db'))
|
|
|
|
const httpErr = runFilter(
|
|
new HttpException('SQLITE_BUSY: database is locked', HttpStatus.BAD_REQUEST),
|
|
)
|
|
assert.equal(httpErr.body.error, 'درخواست نامعتبر است')
|
|
|
|
process.env.NODE_ENV = prev
|
|
|
|
console.log('production-error tests passed')
|