node/test/node-api/test_callback_scope/test-async-hooks.js
Vladimir Morozov 53b3c83ed4
node-api: use Node-API in comments
PR-URL: https://github.com/nodejs/node/pull/61320
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2026-01-16 16:00:54 +00:00

39 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
// The async_hook that we enable would register the process.emitWarning()
// call from loading the Node-API addon as asynchronous activity because
// it contains a process.nextTick() call. Monkey patch it to be a no-op
// before we load the addon in order to avoid this.
process.emitWarning = () => {};
const { runInCallbackScope } = require(`./build/${common.buildType}/binding`);
const expectedResource = {};
const expectedResourceType = 'test-resource';
let insideHook = false;
let expectedId;
async_hooks.createHook({
init: common.mustCall((id, type, triggerAsyncId, resource) => {
if (type !== expectedResourceType) {
return;
}
assert.strictEqual(resource, expectedResource);
expectedId = id;
}),
before: common.mustCall((id) => {
assert.strictEqual(id, expectedId);
insideHook = true;
}),
after: common.mustCall((id) => {
assert.strictEqual(id, expectedId);
insideHook = false;
}),
}).enable();
runInCallbackScope(expectedResource, expectedResourceType, common.mustCall(() => {
assert(insideHook);
}));