pokerogue/test/testUtils/listenersManager.ts
Sirz Benjie 35e733e87a
[Test] [Refactor] [GitHub] Enable no isolate for vitest (#5566)
* Reuse global scene between tests

Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>

* Add missing each method to mockContainer

* Fix select-modifier-phase test

* Sanitize overrides before tests

Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>

* Sanitize overrides before tests

Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>

* [WIP] fix things

* Fix tests not working with --no-isolate

* Update npm tests to use no isolate

* Update test-shard-template

* Update package.json

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

---------

Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2025-03-28 17:30:38 -04:00

42 lines
1.2 KiB
TypeScript

import { expect } from "vitest";
/**
* Whether or not it is currently the first time running this manager.
*/
let firstTime = true;
/**
* The list of listeners that were present during the first time this manager is run.
* These initial listeners are needed throughout the entire test suite, so we never remove them.
*/
const initialListeners: NodeJS.MessageListener[] = [];
/**
* The current listener that is only needed for the current test file.
* We plan to delete it during the next test file, when it is no longer needed.
*/
let currentListener: NodeJS.MessageListener | null;
export function manageListeners() {
if (firstTime) {
initialListeners.push(...process.listeners("message"));
} else {
expect(process.listeners("message").length).toBeLessThan(7);
// Remove the listener that was used during the previous test file
if (currentListener) {
process.removeListener("message", currentListener);
currentListener = null;
}
// Find the new listener that is being used for the current test file
process.listeners("message").forEach(fn => {
if (!initialListeners.includes(fn)) {
currentListener = fn;
}
});
}
firstTime = false;
}