Added a listenersManager

This commit is contained in:
Michael Li 2025-01-10 16:52:52 -05:00 committed by Sirz Benjie
parent 3878116624
commit afbab9787c
No known key found for this signature in database
GPG Key ID: 4A524B4D196C759E
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,41 @@
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: unknown[] = [];
/**
* 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;
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;
}

View File

@ -21,6 +21,7 @@ import MockImage from "#test/utils/mocks/mocksContainer/mockImage";
import Phaser from "phaser"; import Phaser from "phaser";
import InputText from "phaser3-rex-plugins/plugins/inputtext"; import InputText from "phaser3-rex-plugins/plugins/inputtext";
import { vi } from "vitest"; import { vi } from "vitest";
import { manageListeners } from "./listenersManager";
/** /**
* An initialization function that is run at the beginning of every test file (via `beforeAll()`). * An initialization function that is run at the beginning of every test file (via `beforeAll()`).
@ -95,4 +96,6 @@ export function initTestFile() {
initLoggedInUser(); initLoggedInUser();
initMysteryEncounters(); initMysteryEncounters();
} }
manageListeners();
} }