jest-environment-emit is a utility package for Jest that overcomes the limitation of having only one test environment per project. It provides a mechanism to add multiple event handlers to any Jest test environment, acting similarly to multiple test reporters. The package offers `WithEmitter`, a higher-order function to wrap custom environments, and pre-wrapped `TestEnvironment` classes for `jest-environment-node` and `jest-environment-jsdom` via subpath exports. Currently at version 1.2.0, released in June 2025, the package maintains a steady release cadence, frequently addressing compatibility with new Jest versions (e.g., Jest 30 support in v1.2.0) and refining import/export mechanisms for robust ESM and CJS interoperability. Its primary differentiator is enabling modular, composable event-driven logic within the Jest testing lifecycle, which is otherwise restricted to a single environment definition. It ships with full TypeScript types, enhancing developer experience.
npm install jest-environment-emitVerified import paths — ran on the pinned version, not inferred.
Demonstrates configuring Jest to use `jest-environment-emit/node` and attaching multiple event listener modules. It includes an example listener module showing how to subscribe to various Jest lifecycle events to perform custom setup, teardown, or other actions.
Ensure your `jest` peer dependency is `>=30.0.0` or compatible. Review Jest's own breaking changes between your previous version and Jest 30.
Always use specific subpath imports like `jest-environment-emit/node` or `jest-environment-emit/jsdom` for the pre-wrapped `TestEnvironment` classes.
Upgrade to `jest-environment-emit@1.0.8` or newer, which includes fixes to make listeners resilient to errors. Ensure your listener functions handle their own errors gracefully.
Upgrade to `jest-environment-emit@1.0.5` or newer. This version introduced fixes for `module.exports.default` fallback and ensured exclusive use of default and named exports, improving import reliability.
Use the correct subpath import for the desired environment, e.g., `import { TestEnvironment } from 'jest-environment-emit/node';`.Ensure `jest-environment-emit` is installed. If using a specific subpath (e.g., `jest-environment-emit/node`), verify the path is correct and accessible from your Jest configuration file. This often happens if the package isn't in `node_modules` or a symlink is broken.
Verify that your listener module is correctly listed in `testEnvironmentOptions.eventListeners` in your Jest config and that it exports the listener function as a `default` export.