From d88c4c87b68a27e8fc4c36e1ba17044e8a06d3bb Mon Sep 17 00:00:00 2001 From: Mansur Akhmadi Date: Thu, 26 Nov 2026 04:21:18 +0700 Subject: [PATCH] feat(run): Select First Failed Test When Finished in Test Runner Options (#5972) Adds the Select First Failed Test When Finished toggle under Test Runner Options and in Find Action (off by default, stored with the other test tree options). When a run finishes with it on, the test tree selects the first failure in tree order, honouring filters and sorting, opens its collapsed suites and shows its output without moving focus. Co-authored-by: mansur-akhmadi <10739460+mansur-akhmadi@users.noreply.github.com> --- docs/en/guide/testing.mdx | 1 + docs/zh/guide/testing.mdx | 1 + src/features/run/TestTreeView.test.tsx | 134 +++++++++++++++++++++++++ src/features/run/TestTreeView.tsx | 31 +++++- src/features/run/index.ts | 9 ++ src/features/run/testTree.ts | 4 + src/platform/locales/text/ru.json | 1 + src/platform/locales/text/zh-TW.json | 1 + src/platform/locales/text/zh.json | 1 + 9 files changed, 181 insertions(+), 2 deletions(-) diff --git a/docs/en/guide/testing.mdx b/docs/en/guide/testing.mdx index 65af9ef70..36cbadce3 100644 --- a/docs/en/guide/testing.mdx +++ b/docs/en/guide/testing.mdx @@ -54,6 +54,7 @@ A test run opens a tab in the **Run** tool window with the test tree next to the - Click a test to see its own output in the console, or a class or suite to see its setup and teardown output followed by its tests'. **Show all output** above the console goes back to the whole run. - Double-click, `Enter` or `F4` jumps to the test's source (**Jump to Source**). - **Show Passed** and **Show Ignored** filter the tree. Passed tests are hidden by default; the filters are remembered. +- **⋯ › Test Runner Options › Select First Failed Test When Finished** selects the first failed test, with its output, when a run ends. It is off by default. - Right-click a test or a selection for **Run '…'**, **Debug '…'** and **Jump to Source**. - For an assertion failure with expected and actual values, **Show Diff** opens them side by side. diff --git a/docs/zh/guide/testing.mdx b/docs/zh/guide/testing.mdx index 5fec744ff..1bbdb18bf 100644 --- a/docs/zh/guide/testing.mdx +++ b/docs/zh/guide/testing.mdx @@ -50,6 +50,7 @@ RSpec 有自己的配置类型(**RSpec 测试**)和检测到的 `rspec` 配 - 单击某个测试,控制台只显示它自己的输出;单击类或套件,先显示它自己的准备和清理输出,再显示其下各个测试的输出。控制台上方的 **显示所有输出** 可回到整个运行的输出。 - 双击、按 `Enter` 或 `F4` 跳到测试源码(**跳转到源代码**)。 - **显示已通过** 和 **显示已忽略** 用于筛选测试树。默认隐藏已通过的测试,筛选状态会被记住。 +- **⋯ › 测试运行器选项 › 完成后选中第一个失败的测试** 会在运行结束时选中第一个失败的测试并显示它的输出。默认关闭。 - 右键单个测试或多选后,可以 **运行 '…'**、**调试 '…'** 或 **跳转到源代码**。 - 对于带有期望值和实际值的断言失败,**显示差异** 会把两者并排对比。 diff --git a/src/features/run/TestTreeView.test.tsx b/src/features/run/TestTreeView.test.tsx index 4690cae52..ae394b70d 100644 --- a/src/features/run/TestTreeView.test.tsx +++ b/src/features/run/TestTreeView.test.tsx @@ -507,3 +507,137 @@ describe("Sorting Options", () => { expect(screen.getByRole("treeitem", { selected: true }).textContent).toBe("a()"); }); }); + +describe("Select First Failed Test When Finished", () => { + beforeEach(() => { + localStorage.clear(); + onSelect.mockClear(); + }); + afterEach(() => localStorage.clear()); + + /** Run order: a passing suite, then a failure nested two suites deep, then a later one. */ + function run() { + const r = new TestResults(); + r.apply([ + { type: "result", path: ["ApiTest", "lists()"], status: "passed" }, + { type: "result", path: ["ApiTest", "Auth", "Tokens", "refreshes()"], status: "failed" }, + { type: "result", path: ["DbTest", "reads()"], status: "failed" }, + ]); + return r; + } + const onSelect = vi.fn(); + function Host({ results, running }: { results: TestResults; running: boolean }) { + const [selected, setSelected] = useState(null); + return ( + { + onSelect(id); + setSelected(id); + }} + running={running} + /> + ); + } + const selectedRows = () => + screen.queryAllByRole("treeitem", { selected: true }).map((r) => r.textContent!.replace(/[\d.]+ m?s$/, "")); + const enable = (on = true) => + localStorage.setItem("openidea.run.testFilters", JSON.stringify({ showPassed: true, selectFirstFailed: on })); + + it("selects the first failure in tree order when the run finishes, opening its collapsed suites", () => { + enable(); + const results = run(); + const { rerender } = render(); + act(() => testTree.collapseAll!()); + expect(screen.queryByText("refreshes()")).toBeNull(); + expect(selectedRows()).toEqual([]); + results.finish(1); + rerender(); + expect(selectedRows()).toEqual(["refreshes()"]); + expect(results.get(onSelect.mock.lastCall![0])!.name).toBe("refreshes()"); + }); + + it("follows the sort order", () => { + localStorage.setItem( + "openidea.run.testFilters", + JSON.stringify({ showPassed: true, selectFirstFailed: true, sortAlphabetically: true }), + ); + const results = new TestResults(); + results.apply([ + { type: "result", path: ["beta", "zeta()"], status: "failed" }, + { type: "result", path: ["Alpha", "omega()"], status: "failed" }, + ]); + const { rerender } = render(); + results.finish(1); + rerender(); + expect(selectedRows()).toEqual(["omega()"]); + }); + + it("follows Sort by Duration, which applies in the render where the run finishes", () => { + localStorage.setItem( + "openidea.run.testFilters", + JSON.stringify({ showPassed: true, selectFirstFailed: true, sortByDuration: true }), + ); + const results = new TestResults(); + results.apply([ + { type: "result", path: ["fast()"], status: "failed", durationMs: 5 }, + { type: "result", path: ["slow()"], status: "failed", durationMs: 50 }, + ]); + const { rerender } = render(); + results.finish(1); + rerender(); + expect(selectedRows()).toEqual(["slow()"]); + }); + + it("keeps opening failed suites by default on the next run when the failure was already in view", () => { + enable(); + const first = run(); + const { rerender } = render(); + first.finish(1); + rerender(); + expect(selectedRows()).toEqual(["refreshes()"]); + const second = new TestResults(); + second.apply([{ type: "result", path: ["DbTest", "Pool", "reuses()"], status: "failed" }]); + rerender(); + expect(screen.getByText("reuses()")).toBeVisible(); + }); + + it("is off by default, and selects nothing for a passing run or results that were already finished", () => { + let results = run(); + const { rerender, unmount } = render(); + results.finish(1); + rerender(); + expect(selectedRows()).toEqual([]); + unmount(); + + enable(); + results = new TestResults(); + results.apply([{ type: "result", path: ["ApiTest", "lists()"], status: "passed" }]); + const passing = render(); + results.finish(0); + passing.rerender(); + expect(selectedRows()).toEqual([]); + passing.unmount(); + + results = run(); + results.finish(1); + render(); + expect(selectedRows()).toEqual([]); + expect(onSelect).not.toHaveBeenCalled(); + }); + + it("the Test Runner Options toggle is remembered across sessions", () => { + const view = ; + const { unmount } = render(view); + expect(testTree.selectFirstFailed).toBe(false); + act(() => testTree.toggleSelectFirstFailed!()); + expect(testTree.selectFirstFailed).toBe(true); + unmount(); + expect(testTree.toggleSelectFirstFailed).toBeNull(); + render(view); + expect(testTree.selectFirstFailed).toBe(true); + }); +}); diff --git a/src/features/run/TestTreeView.tsx b/src/features/run/TestTreeView.tsx index 74c50e2d4..dfe9b85c6 100644 --- a/src/features/run/TestTreeView.tsx +++ b/src/features/run/TestTreeView.tsx @@ -40,6 +40,7 @@ interface TestFilters { showPassed: boolean; showIgnored: boolean; showDurations: boolean; + selectFirstFailed: boolean; sortAlphabetically: boolean; sortByDuration: boolean; } @@ -52,6 +53,7 @@ export function loadTestFilters(): TestFilters { showPassed: v.showPassed === true, showIgnored: v.showIgnored !== false, showDurations: v.showDurations !== false, + selectFirstFailed: v.selectFirstFailed === true, sortAlphabetically: v.sortAlphabetically === true && v.sortByDuration !== true, sortByDuration: v.sortByDuration === true, }; @@ -60,6 +62,7 @@ export function loadTestFilters(): TestFilters { showPassed: false, showIgnored: true, showDurations: true, + selectFirstFailed: false, sortAlphabetically: false, sortByDuration: false, }; @@ -202,7 +205,7 @@ export function TestTreeView({ canRunTests, }: TestTreeViewProps) { const [filters, setFilters] = useState(loadTestFilters); - const { showPassed, showIgnored, showDurations, sortAlphabetically, sortByDuration } = filters; + const { showPassed, showIgnored, showDurations, selectFirstFailed, sortAlphabetically, sortByDuration } = filters; const toggle = (key: keyof TestFilters) => setFilters((f) => { const next = { ...f, [key]: !f[key] }; @@ -330,14 +333,35 @@ export function TestTreeView({ testTree.exportResults = canExport ? () => setExporting(true) : null; testTree.showDurations = showDurations; testTree.toggleDurations = () => toggle("showDurations"); + testTree.selectFirstFailed = selectFirstFailed; + testTree.toggleSelectFirstFailed = () => toggle("selectFirstFailed"); return () => { testTree.expandAll = null; testTree.collapseAll = null; testTree.exportResults = null; testTree.showDurations = null; testTree.toggleDurations = null; + testTree.selectFirstFailed = null; + testTree.toggleSelectFirstFailed = null; }; }); + + // Select First Failed Test When Finished: when a run ends, select its first failure in tree order, as IDEA does. + const [wasRunning, setWasRunning] = useState(running); + const [firstFailed, setFirstFailed] = useState<{ key: string } | null>(null); + if (wasRunning !== running) { + setWasRunning(running); + const first = !running && selectFirstFailed ? findOccurrence(roots, null, 1, treeModel) : null; + if (first) { + // Failed suites are open by default; pin the expansion only when the failure is under a collapsed one. + if (first.ancestors.some((a) => !effectiveExpanded.has(a.id))) + setExpanded(new Set([...effectiveExpanded, ...first.ancestors.map((a) => a.id)])); + setFirstFailed({ key: first.node.id }); + } + } + useEffect(() => { + if (firstFailed) tree.current?.reveal(firstFailed.key); + }, [firstFailed]); const hasSuites = roots.some((n) => n.children.length > 0); // Previous / Next Failed Test (⌘⌥↑ / ⌘⌥↓): like IDEA, stops at the first / last failure instead of wrapping. @@ -448,7 +472,10 @@ export function TestTreeView({ { label: "Test Runner Options", icon: "settings", - submenu: [{ kind: "action", action: "run.toggleTestDurations" }], + submenu: [ + { kind: "action", action: "run.toggleTestDurations" }, + { kind: "action", action: "run.toggleSelectFirstFailedTest" }, + ], }, ]} /> diff --git a/src/features/run/index.ts b/src/features/run/index.ts index 4f5d3f698..0fd9355ef 100644 --- a/src/features/run/index.ts +++ b/src/features/run/index.ts @@ -818,6 +818,15 @@ export function activate(): () => void { toggled: () => testTree.showDurations === true, run: () => testTree.toggleDurations?.(), }, + { + id: "run.toggleSelectFirstFailedTest", + title: "Select First Failed Test When Finished", + category: "Run", + when: "projectOpen", + enabled: () => testTree.toggleSelectFirstFailed !== null, + toggled: () => testTree.selectFirstFailed === true, + run: () => testTree.toggleSelectFirstFailed?.(), + }, { id: "run.reloadConfigs", title: "Reload Run Configurations", diff --git a/src/features/run/testTree.ts b/src/features/run/testTree.ts index 1569f50c3..9a785268e 100644 --- a/src/features/run/testTree.ts +++ b/src/features/run/testTree.ts @@ -9,10 +9,14 @@ export const testTree: { exportResults: (() => void) | null; showDurations: boolean | null; toggleDurations: (() => void) | null; + selectFirstFailed: boolean | null; + toggleSelectFirstFailed: (() => void) | null; } = { expandAll: null, collapseAll: null, exportResults: null, showDurations: null, toggleDurations: null, + selectFirstFailed: null, + toggleSelectFirstFailed: null, }; diff --git a/src/platform/locales/text/ru.json b/src/platform/locales/text/ru.json index 94fb618b1..0d1103283 100644 --- a/src/platform/locales/text/ru.json +++ b/src/platform/locales/text/ru.json @@ -9052,6 +9052,7 @@ "Previous Failed Test": "Предыдущий непройденный тест", "Next Failed Test": "Следующий непройденный тест", "Show Test Durations": "Показать длительность тестов", + "Select First Failed Test When Finished": "Выбирать первый непройденный тест по завершении", "Test Runner Options": "Параметры запуска тестов", "Run '{name}' with Fuzzing": "Запустить '{name}' с фаззингом", "Reading…": "Чтение…", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index b29f20408..2456a1cb2 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -8511,6 +8511,7 @@ "Show Changes": "顯示更改", "Previous Failed Test": "上一個失敗的測試", "Next Failed Test": "下一個失敗的測試", + "Select First Failed Test When Finished": "完成後選取第一個失敗的測試", "Run '{name}' with Fuzzing": "使用模糊測試執行 '{name}'", "Reading…": "正在讀取…", "Editing…": "正在編輯…", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index d2025185f..4273cb5a4 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -9168,6 +9168,7 @@ "Previous Failed Test": "上一个失败的测试", "Next Failed Test": "下一个失败的测试", "Show Test Durations": "显示测试耗时", + "Select First Failed Test When Finished": "完成后选中第一个失败的测试", "Test Runner Options": "测试运行器选项", "Run '{name}' with Fuzzing": "使用模糊测试运行 '{name}'", "Reading…": "正在读取…",