MCPcopy
hub / github.com/fastify/fastify / testExecutionHook

Function testExecutionHook

test/route-hooks.test.js:17–180  ·  view source on GitHub ↗
(hook)

Source from the content-addressed store, hash-verified

15}
16
17function testExecutionHook (hook) {
18 test(`${hook}`, (t, testDone) => {
19 t.plan(3)
20 const fastify = Fastify()
21
22 fastify.post('/', {
23 [hook]: (req, reply, doneOrPayload, done) => {
24 t.assert.ok('hook called')
25 endRouteHook(doneOrPayload, done)
26 }
27 }, (req, reply) => {
28 reply.send(req.body)
29 })
30
31 fastify.inject({
32 method: 'POST',
33 url: '/',
34 payload: { hello: 'world' }
35 }, (err, res) => {
36 t.assert.ifError(err)
37 const payload = JSON.parse(res.payload)
38 t.assert.deepStrictEqual(payload, { hello: 'world' })
39 testDone()
40 })
41 })
42
43 test(`${hook} option should be called after ${hook} hook`, (t, testDone) => {
44 t.plan(3)
45 const fastify = Fastify()
46 const checker = Object.defineProperty({ calledTimes: 0 }, 'check', {
47 get: function () { return ++this.calledTimes }
48 })
49
50 fastify.addHook(hook, (req, reply, doneOrPayload, done) => {
51 t.assert.strictEqual(checker.check, 1)
52 endRouteHook(doneOrPayload, done)
53 })
54
55 fastify.post('/', {
56 [hook]: (req, reply, doneOrPayload, done) => {
57 t.assert.strictEqual(checker.check, 2)
58 endRouteHook(doneOrPayload, done)
59 }
60 }, (req, reply) => {
61 reply.send({})
62 })
63
64 fastify.inject({
65 method: 'POST',
66 url: '/',
67 payload: { hello: 'world' }
68 }, (err, res) => {
69 t.assert.ifError(err)
70 testDone()
71 })
72 })
73
74 test(`${hook} option could accept an array of functions`, (t, testDone) => {

Callers 1

Calls 4

endRouteHookFunction · 0.85
sendMethod · 0.80
injectMethod · 0.80
addHookMethod · 0.80

Tested by

no test coverage detected