Skip to content

the same context for addEventHandler from jest-circus/src/state.ts #11483

@satanTime

Description

@satanTime

🐛 Bug Report

The problem is that I need to distinguish scopes on every beforeAll and beforeEach call.
However, adding a listener on addEventHandler doesn't bring the desired effect, because the listener is added to a different context and hasn't been ever called.

With jasmine it was quite easy:

jasmine.getEnv().addReporter(reporterStack);

And would be great to have the same behavior in jest-circus.

To Reproduce

This is the test and desired code: https://github.com/satanTime/jest-circus-hooks/blob/master/src/app/app.component.spec.ts#L7-L21

Steps to reproduce the behavior:

> jest -w 1 --config jest.js

 FAIL  src/app/app.component.spec.ts
  main block
    ✕ uses listener (2 ms)
    sub block
      ✓ simulates positive (3 ms)
  • edit jest.ts and uncomment testRunner: 'jest-jasmine2',
  • npm run test
  • no failure

This is the place I want to add my callback to.

https://github.com/facebook/jest/blob/master/packages/jest-circus/src/state.ts#L14

Expected behavior

addEventHandler register a callback properly and the callback is triggered on events.

For example, when I change the source code in node_modules to:

const eventHandlers = window.eventHandlers || [_eventHandler.default, _formatNodeAssertErrors.default];
window.eventHandlers = eventHandlers;

it works correctly.

Proposed PR with a fix

The PR is here #11529

envinfo

  System:
    OS: macOS 11.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Binaries:
    Node: 12.22.1 - /opt/local/bin/node
    Yarn: 1.22.10 - /opt/local/bin/yarn
    npm: 6.14.13 - /opt/local/bin/npm
  npmPackages:
    jest: 27.0.3 => 27.0.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions