Skip to content

docs(hooks): document this binding for preClose hook and strengthen test assertion#6738

Open
harihara-ai wants to merge 1 commit into
fastify:mainfrom
harihara-ai:fix/preclose-this-context-docs-and-test
Open

docs(hooks): document this binding for preClose hook and strengthen test assertion#6738
harihara-ai wants to merge 1 commit into
fastify:mainfrom
harihara-ai:fix/preclose-this-context-docs-and-test

Conversation

@harihara-ai
Copy link
Copy Markdown

What this PR does

Documents the this binding for the preClose hook in docs/Reference/Hooks.md, and strengthens the corresponding test assertions in test/close.test.js.

Problem

The preClose section in the docs did not state that hook functions receive a this binding. Its callback-style code example used an arrow function, which would break this binding if copied literally. Both neighbouring sections (onReady, onListen) already documented this and used the function keyword.

The two preClose tests used t.assert.ok(typeof this === typeof fastify) — a weak type-equality check that passes even when this is bound to the wrong object, as long as both are objects.

Changes

docs/Reference/Hooks.mdpreClose section:

  • Added: "Hook functions are invoked with this bound to the associated Fastify instance."
  • Changed callback example from arrow function to function keyword

test/close.test.jspreClose callback and preClose async tests:

  • Replaced t.assert.ok(typeof this === typeof fastify) with t.assert.strictEqual(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
  • Matches the pattern already used in test/hooks.on-ready.test.js

Testing

node --test test/close.test.js → 22/22 pass, exit code 0. No logic changes; no API changes.

Closes #4967

…est assertion

Add sentence documenting that hook functions are invoked with `this` bound
to the Fastify instance in the preClose section of docs/Reference/Hooks.md.
Also convert the callback example from arrow function to regular function
to avoid misleading readers into thinking arrow functions work here.

Strengthen test/close.test.js assertions from a weak typeof check to
t.assert.strictEqual(this.pluginName, fastify.pluginName) — matching
the existing pattern in test/hooks.on-ready.test.js.

Fixes fastify#4967
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label May 27, 2026
@Fdawgs
Copy link
Copy Markdown
Member

Fdawgs commented May 31, 2026

Based on the username, plus the commit comment, this smells like a bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ApplicationHooks have inconsistent reference to instance

2 participants