mondaycom / node-execution-context

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

hadarbmdev opened this issue Β· comments

πŸ› 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",