-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-pretty-errors.js
More file actions
60 lines (49 loc) · 1.83 KB
/
Copy pathtest-pretty-errors.js
File metadata and controls
60 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict'
const fastify = require('fastify')
const fastifyAta = require('./index')
let pass = 0
let fail = 0
function assert(cond, msg) {
if (cond) { pass++; console.log(` PASS ${msg}`) }
else { fail++; console.log(` FAIL ${msg}`) }
}
const schema = {
body: {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' },
},
required: ['name'],
},
}
async function run() {
console.log('\nfastify-ata prettyErrors Tests\n')
// prettyErrors ON: 400 message carries the compiler-grade code + suggestion
const app = fastify()
await app.register(fastifyAta, { prettyErrors: true })
app.post('/u', { schema }, (req, reply) => reply.send({ ok: true }))
await app.ready()
const r = await app.inject({ method: 'POST', url: '/u', payload: { age: 5 } })
assert(r.statusCode === 400, `prettyErrors: invalid returns 400 (got ${r.statusCode})`)
const msg = JSON.parse(r.payload).message
assert(/ATA\d{4}/.test(msg), `prettyErrors: message carries error code (got "${msg}")`)
assert(msg.includes('did you mean'), `prettyErrors: message carries suggestion (got "${msg}")`)
await app.close()
// prettyErrors OFF (default): message stays AJV-compatible, no ATA code
const app2 = fastify()
await app2.register(fastifyAta)
app2.post('/u', { schema }, (req, reply) => reply.send({ ok: true }))
await app2.ready()
const r2 = await app2.inject({ method: 'POST', url: '/u', payload: { age: 5 } })
assert(r2.statusCode === 400, `default: invalid returns 400 (got ${r2.statusCode})`)
const msg2 = JSON.parse(r2.payload).message
assert(!/ATA\d{4}/.test(msg2), `default: message has no ATA code (got "${msg2}")`)
await app2.close()
console.log(`\n${pass}/${pass + fail} tests passed\n`)
process.exit(fail > 0 ? 1 : 0)
}
run().catch((err) => {
console.error(err)
process.exit(1)
})