pokerogue/test/test-utils/listeners-manager.ts
Sirz Benjie 51d4c33de0
[Misc] Standardize-file-names (#6137)
* Standardize filenames to kebab-case

Co-authored-by: pymilkmaiden <cassiopeiamahler56@gmail.com>

* Move script outside of public folder

* Move update_exp_sprites to scripts

* Add ls-lint to lint file and directory names

* Update lefthook.yml to skip merge / rebase on all pre-commit commands

---------

Co-authored-by: pymilkmaiden <cassiopeiamahler56@gmail.com>
2025-07-24 16:38:31 -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;
}