feat(run): Sort Alphabetically / Sort by Duration in the test results toolbar (#4576)

The test results toolbar gets Sorting Options with Sort Alphabetically (by name at every level, ignoring case, numbers by value) and Sort by Duration (slowest first at every level, a suite by its total, untimed last; disabled and not applied while the run is in progress). The two replace each other, run after the filters and are kept with Show Passed / Show Ignored.

Co-authored-by: mansur-akhmadi <10739460+mansur-akhmadi@users.noreply.github.com>
This commit is contained in:
Mansur Akhmadi
2026-11-20 01:31:29 +08:00
committed by GitHub
co-authored by mansur-akhmadi
parent da1bcd67cd
commit 47b4cb55ba
7 changed files with 191 additions and 8 deletions
+112
View File
@@ -332,3 +332,115 @@ describe("Export Test Results", () => {
expect(testTree.exportResults).not.toBeNull();
});
});
describe("Sorting Options", () => {
beforeEach(() => localStorage.setItem("openidea.run.testFilters", JSON.stringify({ showPassed: true })));
afterEach(() => localStorage.clear());
/** Run order: beta (item10, Item2, item1), Alpha (zeta); item1 reported no duration. */
function timed() {
const r = new TestResults();
r.apply([
{ type: "result", path: ["beta", "item10()"], status: "passed", durationMs: 5 },
{ type: "result", path: ["beta", "Item2()"], status: "passed", durationMs: 30 },
{ type: "result", path: ["beta", "item1()"], status: "failed" },
{ type: "result", path: ["Alpha", "zeta()"], status: "passed", durationMs: 100 },
]);
r.finish(1);
return r;
}
const view = (results: TestResults, running = false) => (
<TestTreeView results={results} version={1} cwd="/p" selected={null} onSelect={() => {}} running={running} />
);
const rows = () => screen.getAllByRole("treeitem").map((el) => el.textContent!.replace(/[\d.]+ m?s$/, ""));
const option = (name: string) => {
fireEvent.click(screen.getByRole("button", { name: "Sorting Options" }));
return screen.getByRole("menuitemcheckbox", { name });
};
it("keeps the run order until a sort is chosen", () => {
render(view(timed()));
expect(rows()).toEqual(["beta", "item10()", "Item2()", "item1()", "Alpha", "zeta()"]);
expect(screen.getByRole("button", { name: "Sorting Options" })).toHaveAttribute("aria-pressed", "false");
});
it("Sort Alphabetically orders suites and tests by name, ignoring case and comparing numbers, and is remembered", () => {
const { unmount } = render(view(timed()));
fireEvent.click(option("Sort Alphabetically"));
expect(rows()).toEqual(["Alpha", "zeta()", "beta", "item1()", "Item2()", "item10()"]);
expect(screen.getByRole("button", { name: "Sorting Options" })).toHaveAttribute("aria-pressed", "true");
unmount();
render(view(timed()));
expect(rows()).toEqual(["Alpha", "zeta()", "beta", "item1()", "Item2()", "item10()"]);
fireEvent.click(option("Sort Alphabetically"));
expect(rows()).toEqual(["beta", "item10()", "Item2()", "item1()", "Alpha", "zeta()"]);
});
it("Sort by Duration puts the slowest first at each level, tests without a duration last", () => {
render(view(timed()));
fireEvent.click(option("Sort by Duration"));
expect(rows()).toEqual(["Alpha", "zeta()", "beta", "Item2()", "item10()", "item1()"]);
});
it("the two sorts replace each other, as in IDEA", () => {
render(view(timed()));
fireEvent.click(option("Sort by Duration"));
expect(option("Sort by Duration")).toHaveAttribute("aria-checked", "true");
fireEvent.click(screen.getByRole("menuitemcheckbox", { name: "Sort Alphabetically" }));
expect(option("Sort Alphabetically")).toHaveAttribute("aria-checked", "true");
expect(screen.getByRole("menuitemcheckbox", { name: "Sort by Duration" })).toHaveAttribute("aria-checked", "false");
fireEvent.click(screen.getByRole("menuitemcheckbox", { name: "Sort by Duration" }));
expect(rows()).toEqual(["Alpha", "zeta()", "beta", "Item2()", "item10()", "item1()"]);
expect(option("Sort Alphabetically")).toHaveAttribute("aria-checked", "false");
});
it("a saved pair of sorts loads as Sort by Duration alone", () => {
localStorage.setItem(
"openidea.run.testFilters",
JSON.stringify({ showPassed: true, sortAlphabetically: true, sortByDuration: true }),
);
render(view(timed()));
expect(rows()).toEqual(["Alpha", "zeta()", "beta", "Item2()", "item10()", "item1()"]);
expect(option("Sort Alphabetically")).toHaveAttribute("aria-checked", "false");
});
it("the button opens and closes the menu", () => {
render(view(timed()));
const button = screen.getByRole("button", { name: "Sorting Options" });
expect(button).toHaveAttribute("aria-haspopup", "menu");
fireEvent.click(button);
expect(button).toHaveAttribute("aria-expanded", "true");
fireEvent.click(button);
expect(button).toHaveAttribute("aria-expanded", "false");
expect(screen.queryByRole("menuitemcheckbox", { name: "Sort by Duration" })).toBeNull();
});
it("Sort by Duration waits for the run to finish", () => {
localStorage.setItem("openidea.run.testFilters", JSON.stringify({ showPassed: true, sortByDuration: true }));
const { rerender } = render(view(timed(), true));
expect(rows()).toEqual(["beta", "item10()", "Item2()", "item1()", "Alpha", "zeta()"]);
expect(option("Sort by Duration")).toHaveAttribute("aria-disabled", "true");
fireEvent.keyDown(document.activeElement!, { key: "Escape" });
rerender(view(timed(), false));
expect(rows()).toEqual(["Alpha", "zeta()", "beta", "Item2()", "item10()", "item1()"]);
});
it("Previous / Next Failed Test follow the sorted order", () => {
const r = new TestResults();
r.apply([
{ type: "result", path: ["s", "b()"], status: "failed" },
{ type: "result", path: ["s", "a()"], status: "failed" },
]);
r.finish(1);
function Selecting() {
const [selected, setSelected] = useState<string | null>(null);
return (
<TestTreeView results={r} version={1} cwd="/p" selected={selected} onSelect={setSelected} running={false} />
);
}
render(<Selecting />);
fireEvent.click(option("Sort Alphabetically"));
act(() => void occurrenceNavigator("run")!.go(1, false));
expect(screen.getByRole("treeitem", { selected: true }).textContent).toBe("a()");
});
});
+58 -6
View File
@@ -5,6 +5,7 @@ import {
findOccurrence,
Icon,
IconButton,
Menu,
showContextMenu,
useContextMenuStore,
Spinner,
@@ -21,21 +22,28 @@ import { ExportTestResultsDialog } from "./ExportTestResultsDialog";
import styles from "./Run.module.css";
import { tx } from "@/platform/i18n";
/** Test tree filters, kept across runs and restarts (IDEA hides passed tests by default). */
/** Test tree filters and sorting, kept across runs and restarts (IDEA hides passed tests by default). */
const FILTERS_KEY = "openidea.run.testFilters";
interface TestFilters {
showPassed: boolean;
showIgnored: boolean;
sortAlphabetically: boolean;
sortByDuration: boolean;
}
export function loadTestFilters(): TestFilters {
try {
const raw = globalThis.localStorage?.getItem(FILTERS_KEY);
const v = raw ? (JSON.parse(raw) as Partial<TestFilters>) : {};
return { showPassed: v.showPassed === true, showIgnored: v.showIgnored !== false };
return {
showPassed: v.showPassed === true,
showIgnored: v.showIgnored !== false,
sortAlphabetically: v.sortAlphabetically === true && v.sortByDuration !== true,
sortByDuration: v.sortByDuration === true,
};
} catch {
return { showPassed: false, showIgnored: true };
return { showPassed: false, showIgnored: true, sortAlphabetically: false, sortByDuration: false };
}
}
@@ -106,6 +114,17 @@ function filterTree(nodes: readonly TestNode[], keep: (n: TestNode) => boolean):
return out;
}
/** IDEA's test tree sorts: by name (ignoring case, numbers by value), or slowest first with untimed nodes last. */
const names = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
const byName = (a: TestNode, b: TestNode) => names.compare(a.name, b.name);
function sortTree(nodes: readonly TestNode[], byDuration: boolean): TestNode[] {
const sorted = nodes.map((n) => (n.children.length ? { ...n, children: sortTree(n.children, byDuration) } : n));
if (!byDuration) return sorted.sort(byName);
const duration = new Map(sorted.map((n) => [n, nodeDuration(n) ?? -1]));
return sorted.sort((a, b) => duration.get(b)! - duration.get(a)!);
}
export interface TestTreeViewProps {
results: TestResults;
version: number;
@@ -132,13 +151,18 @@ export function TestTreeView({
canRunTests,
}: TestTreeViewProps) {
const [filters, setFilters] = useState(loadTestFilters);
const { showPassed, showIgnored } = filters;
const { showPassed, showIgnored, sortAlphabetically, sortByDuration } = filters;
const toggle = (key: keyof TestFilters) =>
setFilters((f) => {
const next = { ...f, [key]: !f[key] };
// The sorts replace each other, as in IDEA.
if (key === "sortAlphabetically" && next.sortAlphabetically) next.sortByDuration = false;
if (key === "sortByDuration" && next.sortByDuration) next.sortAlphabetically = false;
saveTestFilters(next);
return next;
});
const sortButton = useRef<HTMLButtonElement>(null);
const [sortMenu, setSortMenu] = useState(false);
const [expanded, setExpanded] = useState<Set<string> | null>(null);
const [exporting, setExporting] = useState(false);
const [selection, setSelection] = useState<Set<string>>(new Set());
@@ -203,10 +227,13 @@ export function TestTreeView({
};
const counts = useMemo(() => results.counts(), [results, version]); // eslint-disable-line react-hooks/exhaustive-deps
// Like IDEA, Sort by Duration applies once the run has finished, so rows don't jump while tests report.
const byDuration = sortByDuration && !running;
const roots = useMemo(() => {
const keep = (n: TestNode) => (showPassed || n.own !== "passed") && (showIgnored || n.own !== "skipped");
return showPassed && showIgnored ? [...results.roots] : filterTree(results.roots, keep);
}, [results, version, showPassed, showIgnored]); // eslint-disable-line react-hooks/exhaustive-deps
const shown = showPassed && showIgnored ? [...results.roots] : filterTree(results.roots, keep);
return byDuration || sortAlphabetically ? sortTree(shown, byDuration) : shown;
}, [results, version, showPassed, showIgnored, sortAlphabetically, byDuration]); // eslint-disable-line react-hooks/exhaustive-deps
// Expand failed suites by default (IDEA reveals failures); the user's toggles win afterwards.
const effectiveExpanded = useMemo(() => {
@@ -280,6 +307,31 @@ export function TestTreeView({
pressed={showIgnored}
onClick={() => toggle("showIgnored")}
/>
<IconButton
ref={sortButton}
icon="test-sort"
size="small"
iconSize={16}
title="Sorting Options"
aria-haspopup="menu"
aria-expanded={sortMenu}
pressed={sortAlphabetically || sortByDuration}
onClick={() => setSortMenu((v) => !v)}
/>
<Menu
open={sortMenu}
anchor={sortButton}
items={[
{ label: "Sort Alphabetically", checked: sortAlphabetically, run: () => toggle("sortAlphabetically") },
{
label: "Sort by Duration",
checked: sortByDuration,
disabled: running,
run: () => toggle("sortByDuration"),
},
]}
onClose={() => setSortMenu(false)}
/>
<IconButton icon="expand-all" action="run.expandAllTests" size="small" iconSize={16} disabled={!hasSuites} />
<IconButton
icon="collapse-all"
+3 -1
View File
@@ -2718,5 +2718,7 @@
"Hover actions": "Hover actions",
"Hover action": "Hover action",
"{count} hover actions": "{count} hover actions",
"{description}; JVM options: {flags}": "{description}; JVM options: {flags}"
"{description}; JVM options: {flags}": "{description}; JVM options: {flags}",
"Sorting Options": "Sorting Options",
"Sort by Duration": "Sort by Duration"
}
+3 -1
View File
@@ -9091,5 +9091,7 @@
"Hover actions": "悬停操作",
"Hover action": "悬停操作",
"{count} hover actions": "{count} 个悬停操作",
"{description}; JVM options: {flags}": "{description};JVM 选项:{flags}"
"{description}; JVM options: {flags}": "{description};JVM 选项:{flags}",
"Sorting Options": "排序选项",
"Sort by Duration": "按持续时间排序"
}
@@ -0,0 +1,7 @@
<!-- Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7.5 3.5H14.5" stroke="#6C707E" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M9.5 8H14.5" stroke="#6C707E" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M11.5 12.5H14.5" stroke="#6C707E" stroke-linecap="round" stroke-linejoin="round"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.85355 9.85355C8.04881 9.65829 8.04881 9.34171 7.85355 9.14645C7.65829 8.95118 7.34171 8.95118 7.14645 9.14645L5 11.2929L5 3.5C5 3.22386 4.77615 3 4.5 3C4.22386 3 4 3.22386 4 3.5L4 11.2929L1.85355 9.14645C1.65829 8.95118 1.34171 8.95118 1.14645 9.14645C0.951184 9.34171 0.951184 9.65829 1.14645 9.85355L4.14644 12.8536C4.23693 12.944 4.36193 13 4.5 13C4.63885 13 4.76447 12.9434 4.85507 12.852L7.85355 9.85355Z" fill="#6C707E"/>
</svg>

After

Width:  |  Height:  |  Size: 980 B

@@ -0,0 +1,7 @@
<!-- Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7.5 3.5H14.5" stroke="#CED0D6" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M9.5 8H14.5" stroke="#CED0D6" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M11.5 12.5H14.5" stroke="#CED0D6" stroke-linecap="round" stroke-linejoin="round"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.85355 9.85355C8.04881 9.65829 8.04881 9.34171 7.85355 9.14645C7.65829 8.95118 7.34171 8.95118 7.14645 9.14645L5 11.2929L5 3.5C5 3.22386 4.77615 3 4.5 3C4.22386 3 4 3.22386 4 3.5L4 11.2929L1.85355 9.14645C1.65829 8.95118 1.34171 8.95118 1.14645 9.14645C0.951184 9.34171 0.951184 9.65829 1.14645 9.85355L4.14644 12.8536C4.23693 12.944 4.36193 13 4.5 13C4.63885 13 4.76447 12.9434 4.85507 12.852L7.85355 9.85355Z" fill="#CED0D6"/>
</svg>

After

Width:  |  Height:  |  Size: 980 B

+1
View File
@@ -448,6 +448,7 @@ const ICONS = {
"test-pending": "run/testNotRunYet",
"test-show-passed": "run/showPassed",
"test-show-ignored": "run/showIgnored",
"test-sort": "runConfigurations/sortByDuration",
debug: "run/debug",
"run-coverage": "run/runWithCoverage",
profile: "run/profile",