pokerogue/test/test-utils/reporters/custom-default-reporter.ts
Bertie690 9815c5eebf
[Test] Revamped MockConsoleLog with color support (#6218)
* Added mock console and fixed up many many things

* Cleaned up handling of colors and such

* Added minor comment

* Fix Focus Punch test

* Fix typo

* Remove redundant comment

* Update vitest.setup.ts

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

* Added color map inside new folder

* Made constants not object bc i was told to

* Update src/constants/colors.ts

* Removed all moves init check

* Removed import

* Fixed up some stuff + added aquamarine color to settings helper

* Added logging for test end

* Removed intentionally failing test

* Fixed console log to use inheritance to not override vitest's wrapping

* Added a custom Vitest reporter to not log the test name every 2 lines

* Moved coloration to a hook to prevent misplacing things

* Fixed import issue by copypasting vitest soure

* Removed intentionally failing test

look i need to check that `test:silent` works on github ok

* Added REUSE annotations to copied parts of source

* Fixed import issue

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2025-09-07 09:18:47 -05:00

63 lines
2.3 KiB
TypeScript

import { relative } from "node:path";
import { parseStacktrace } from "@vitest/utils/source-map";
import chalk from "chalk";
import type { UserConsoleLog } from "vitest";
import type { TestState } from "vitest/node";
import { DefaultReporter } from "vitest/reporters";
/**
* Custom Vitest reporter to strip the current file names from the output.
*/
export default class CustomDefaultReporter extends DefaultReporter {
public override onUserConsoleLog(log: UserConsoleLog, taskState?: TestState): void {
// This code is more or less copied verbatim from `vitest/reporters` source, with minor tweaks to use
// dependencies we actually _have_ (i.e. chalk) rather than ones we don't (i.e. tinyrainbow).
// SPDX-SnippetBegin
// SPDX-SnippetCopyrightText: 2021 VoidZero Inc. and Vitest contributors
// SPDX-License-Identifier: MIT
if (!super.shouldLog(log, taskState)) {
return;
}
const output = log.type === "stdout" ? this.ctx.logger.outputStream : this.ctx.logger.errorStream;
const write = (msg: string) => output.write(msg);
const task = log.taskId ? this.ctx.state.idMap.get(log.taskId) : undefined;
write(log.content); // this is about the only changed line (that and us skipping a newline)
if (!log.origin) {
return;
}
// Code for stack trace, ripped directly out of Vitest source code.
// I wish they had a helper function to do this so we didn't have to import `@vitest/utils`, but oh well...
// browser logs don't have an extra end of line at the end like Node.js does
if (log.browser) {
write("\n");
}
const project = task ? this.ctx.getProjectByName(task.file.projectName ?? "") : this.ctx.getRootProject();
const stack = log.browser ? (project.browser?.parseStacktrace(log.origin) ?? []) : parseStacktrace(log.origin);
const highlight = task && stack.find(i => i.file === task.file.filepath);
for (const frame of stack) {
const color = frame === highlight ? chalk.cyan : chalk.gray;
const path = relative(project.config.root, frame.file);
const positions = [frame.method, `${path}:${chalk.dim(`${frame.line}:${frame.column}`)}`]
.filter(Boolean)
.join(" ");
write(color(` ${chalk.dim(">")} ${positions}\n`));
}
// SPDX-SnippetEnd
}
}