Skip to content

Commit

Permalink
Add test cases (#10)
Browse files Browse the repository at this point in the history
* Add test cases

Signed-off-by: Matteo Collina <[email protected]>

* readme update

Signed-off-by: Matteo Collina <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored May 18, 2024
1 parent 31c224e commit 7cb879b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# fastify-asyncforge

Provide easy [AsyncLocalStorage](https://nodejs.org/api/async_context.html) magic to your
[Fastify](https://fastify.dev) apps. It's based on [asyncforge](http://npm.im/asyncforge).

## Install

```sh
Expand All @@ -18,6 +21,8 @@ const app = fastify({
logger: true
})

// It's fundamental that `start` is called before any other plugins are registered, otherwise the helpers
// would not work as expected
await start(app)

app.decorate('foo', 'bar')
Expand Down
82 changes: 82 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,85 @@ test('fastify.enterWith', async (t) => {

await p.complete
})

test('with additional onRequest hook', async (t) => {
const p = tspl(t, { plan: 15 })
const fastify = Fastify()

await fastifyAsyncForge.start(fastify)

p.strictEqual(logger(), fastify.log)
p.strictEqual(app(), fastify)

fastify.addHook('onRequest', async function b (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
})

fastify.get('/', {
onRequest: async function a (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
}
}, async function (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
return { hello: 'world' }
})

const res = await fastify.inject({
method: 'GET',
url: '/'
})

p.strictEqual(res.statusCode, 200)

await p.complete
})

test('onRequest hook added before registration fails', async (t) => {
const p = tspl(t, { plan: 15 })
const fastify = Fastify()

fastify.addHook('onRequest', async function b (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(logger(), fastify.log)
p.strictEqual(request(), undefined)
p.strictEqual(reply(), undefined)
})

await fastifyAsyncForge.start(fastify)

p.strictEqual(logger(), fastify.log)
p.strictEqual(app(), fastify)

fastify.get('/', {
onRequest: async function a (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
}
}, async function (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
return { hello: 'world' }
})

const res = await fastify.inject({
method: 'GET',
url: '/'
})

p.strictEqual(res.statusCode, 200)

await p.complete
})

0 comments on commit 7cb879b

Please sign in to comment.