Skip to content

Implement Symbol.dispose in registerHooks() #63846

@remcohaszing

Description

@remcohaszing

What is the problem this feature will solve?

The using keyword is perfect for creating resources in tests. It would be great if this can be used with module.registerHooks() to test these hooks.

import { registerHooks } from 'node:module'
import { test } from 'node:test'

import { createHooks } from 'my-package'

test('my hook works', () => {
  using _ = registerHooks(createHooks())
})

This was also suggested in #55698 (comment), but that was over a year ago. Times have changed. I’d like to believe the using keyword will start to become more integrated.

What is the feature you are proposing to solve the problem?

I suggest to replace the module.registerHooks() return value with a disposable (object with a Symbol.dispose key).

What alternatives have you considered?

Alternatively the return deregister value can be kept as a function, but this function could be assigned the Symbol.dispose key, which would be a reference to itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions