Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Node.js AsyncHooks support: BeforeEach increase asyncId for each test #3

@hadarbmdev

Description

@hadarbmdev

🐛 Bug Report

When importing (only!) the package, and trying to set context based on
asyncHooks.executionAsyncId();
in BeforeEach
the returned executionId is X, and in the upcoming test (right after the beforeEach), the returned
asyncHooks.executionAsyncId();
is X+1.

this is the resolution for the issue why getExecutionContext() returns undefined, although setExecutionContext({test:1}) was called in beforeEach, meaning before the test.

To Reproduce

uncomment the line:
const contextProvider = require('@mondaydotcomorg/node-execution-context');
`const asyncHooks = require('async_hooks');
const contextProvider = require('@mondaydotcomorg/node-execution-context');

let asyncIdBefore: number;
describe('AsyncHook test', () => {

beforeEach(() => {
    asyncIdBefore = asyncHooks.executionAsyncId();
    console.log(`asyncIdBefore: ${asyncIdBefore}`);
    // contextProvider.createExecutionContext({test: 1});

});

test('test if asynchook was increased', () => {
   const asyncId = asyncHooks.executionAsyncId();
   console.log(`asyncId: ${asyncId}`);
   expect(asyncId).toEqual(asyncIdBefore);
  // expect(contextProvider.getExecutionContext()).toMatchObject({test: 1});
});

});
`

Expected behavior

jest BeforeEach and the test should share the same context create with setExecutionContext in beforeEach.

envinfo

Node version: v10.15.3
"@mondaydotcomorg/node-execution-context": "^1.0.4",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions