Skip to content

Commit

Permalink
fix: sync hooks (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
simone-sanfratello authored Dec 26, 2023
1 parent 043bcb0 commit 69f7633
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function restoreClientErrorListeners (server, oldListeners) {

async function executeHooks (hooks, app, opts) {
for (const hook of hooks) {
await hook(app, opts).catch((error) => app.log.error(error))
await hook(app, opts)?.catch((error) => app.log.error(error))
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,30 @@ test('should throw if onRestartHook is not a function', async (t) => {
app.addOnRestartHook('not a function')
}, 'onRestartHook must be a function')
})

test('should not throw if onRestartHook is a sync function', async (t) => {
t.plan(3)

async function createApplication (fastify, opts) {
return fastify(opts)
}

const app = await restartable(createApplication, {
keepAliveTimeout: 1
})

t.teardown(async () => {
await app.close()
})

const expectedRestartOptions = { foo: 'bar' }

app.addOnRestartHook((app, restartOptions) => {
t.equal(app.restarted, true)
t.same(restartOptions, expectedRestartOptions)
})

await app.restart(expectedRestartOptions)

t.equal(app.restarted, true)
})

0 comments on commit 69f7633

Please sign in to comment.