From 57094bc28596d0d6607b9fb35817609dc38e094d Mon Sep 17 00:00:00 2001 From: Mansur Akhmadi Date: Fri, 27 Nov 2026 07:18:37 +0700 Subject: [PATCH] feat(run): Track Running Test in Test Runner Options (#6285) Test Runner Options gets Track Running Test (also in Find Action as run.toggleTrackRunningTest), remembered with the other test tree options and on by default. While a run is in progress, each test that starts is selected, its collapsed suites opened and its output shown; when several start together the last in tree order wins. A test finishing doesn't move the selection, so a test picked by hand stays selected until the next one starts, and focus doesn't move. 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 | 122 +++++++++++++++++++++++++ src/features/run/TestTreeView.tsx | 48 +++++++++- 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, 187 insertions(+), 1 deletion(-) diff --git a/docs/en/guide/testing.mdx b/docs/en/guide/testing.mdx index 9a50764f69..6ec3b26f28 100644 --- a/docs/en/guide/testing.mdx +++ b/docs/en/guide/testing.mdx @@ -55,6 +55,7 @@ A test run opens a tab in the **Run** tool window with the test tree next to the - 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. +- **⋯ › Test Runner Options › Track Running Test** selects each test, with its output, as it starts while the run is in progress. It is on 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. - For a Jest or Vitest snapshot mismatch, **Update Snapshot** (on the test or its file or suite, above the failure output, and in the gutter run menu) reruns just those tests with `-u`, so only their snapshots are rewritten. diff --git a/docs/zh/guide/testing.mdx b/docs/zh/guide/testing.mdx index a422534229..647beb508c 100644 --- a/docs/zh/guide/testing.mdx +++ b/docs/zh/guide/testing.mdx @@ -51,6 +51,7 @@ RSpec 有自己的配置类型(**RSpec 测试**)和检测到的 `rspec` 配 - 双击、按 `Enter` 或 `F4` 跳到测试源码(**跳转到源代码**)。 - **显示已通过** 和 **显示已忽略** 用于筛选测试树。默认隐藏已通过的测试,筛选状态会被记住。 - **⋯ › 测试运行器选项 › 完成后选中第一个失败的测试** 会在运行结束时选中第一个失败的测试并显示它的输出。默认关闭。 +- **⋯ › 测试运行器选项 › 跟踪正在运行的测试** 会在运行过程中,每个测试开始时选中它并显示它的输出。默认开启。 - 右键单个测试或多选后,可以 **运行 '…'**、**调试 '…'** 或 **跳转到源代码**。 - 对于带有期望值和实际值的断言失败,**显示差异** 会把两者并排对比。 - 对于 Jest 或 Vitest 的快照不匹配,**更新快照**(在测试或其文件、套件上,失败输出上方,以及边栏运行菜单中)只用 `-u` 重新运行这些测试,只改写它们的快照。 diff --git a/src/features/run/TestTreeView.test.tsx b/src/features/run/TestTreeView.test.tsx index a891a6eb7e..800aa0a132 100644 --- a/src/features/run/TestTreeView.test.tsx +++ b/src/features/run/TestTreeView.test.tsx @@ -680,3 +680,125 @@ describe("Select First Failed Test When Finished", () => { expect(testTree.selectFirstFailed).toBe(true); }); }); + +describe("Track Running Test", () => { + beforeEach(() => { + localStorage.clear(); + onSelect.mockClear(); + }); + afterEach(() => localStorage.clear()); + + const onSelect = vi.fn(); + function Host({ results, version, running }: { results: TestResults; version: number; 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); + const enable = (on = true) => + localStorage.setItem("openidea.run.testFilters", JSON.stringify({ showPassed: true, trackRunning: on })); + + it("selects each test as it starts, opening its collapsed suites", () => { + enable(); + const results = new TestResults(); + results.apply([{ type: "start", path: ["ApiTest", "lists()"] }]); + const { rerender } = render(); + expect(selectedRows()).toEqual(["lists()"]); + expect(results.get(onSelect.mock.lastCall![0])!.name).toBe("lists()"); + + act(() => testTree.collapseAll!()); + results.apply([ + { type: "result", path: ["ApiTest", "lists()"], status: "passed" }, + { type: "start", path: ["ApiTest", "Auth", "Tokens", "refreshes()"] }, + ]); + rerender(); + expect(selectedRows()).toEqual(["refreshes()"]); + }); + + it("selects the latest of several tests that start together and keeps the selection while they finish", () => { + enable(); + const results = new TestResults(); + results.apply([{ type: "start", path: ["pkg", "TestA"] }]); + const { rerender } = render(); + results.apply([ + { type: "start", path: ["pkg", "TestB"] }, + { type: "start", path: ["pkg", "TestC"] }, + ]); + rerender(); + expect(selectedRows()).toEqual(["TestC"]); + + // A test finishing, or one that was already running, doesn't move the selection. + results.apply([{ type: "result", path: ["pkg", "TestC"], status: "passed" }]); + rerender(); + expect(selectedRows()).toEqual(["TestC"]); + fireEvent.mouseDown(screen.getByText("TestA")); + results.apply([{ type: "result", path: ["pkg", "TestB"], status: "passed" }]); + rerender(); + expect(selectedRows()).toEqual(["TestA"]); + + // The next test to start takes the selection again. + results.apply([{ type: "start", path: ["pkg", "TestD"] }]); + rerender(); + expect(selectedRows()).toEqual(["TestD"]); + }); + + it("switched on mid-run, waits for the next test to start", () => { + enable(false); + const results = new TestResults(); + results.apply([{ type: "start", path: ["pkg", "TestA"] }]); + const { rerender } = render(); + act(() => testTree.toggleTrackRunning!()); + rerender(); + expect(selectedRows()).toEqual([]); + results.apply([{ type: "start", path: ["pkg", "TestB"] }]); + rerender(); + expect(selectedRows()).toEqual(["TestB"]); + }); + + it("is on by default, as in IDEA, and follows nothing once the run has finished", () => { + let results = new TestResults(); + results.apply([{ type: "start", path: ["ApiTest", "lists()"] }]); + const { unmount } = render(); + expect(selectedRows()).toEqual(["lists()"]); + unmount(); + onSelect.mockClear(); + + enable(false); + results = new TestResults(); + results.apply([{ type: "start", path: ["ApiTest", "lists()"] }]); + const off = render(); + expect(selectedRows()).toEqual([]); + off.unmount(); + + enable(); + results = new TestResults(); + results.apply([{ type: "start", path: ["ApiTest", "lists()"] }]); + render(); + expect(selectedRows()).toEqual([]); + expect(onSelect).not.toHaveBeenCalled(); + }); + + it("the Test Runner Options toggle is remembered across sessions", () => { + const results = new TestResults(); + const view = ; + const { unmount } = render(view); + expect(testTree.trackRunning).toBe(true); + act(() => testTree.toggleTrackRunning!()); + expect(testTree.trackRunning).toBe(false); + unmount(); + expect(testTree.toggleTrackRunning).toBeNull(); + render(view); + expect(testTree.trackRunning).toBe(false); + }); +}); diff --git a/src/features/run/TestTreeView.tsx b/src/features/run/TestTreeView.tsx index fcd420ef32..d75e85f500 100644 --- a/src/features/run/TestTreeView.tsx +++ b/src/features/run/TestTreeView.tsx @@ -41,6 +41,7 @@ interface TestFilters { showIgnored: boolean; showDurations: boolean; selectFirstFailed: boolean; + trackRunning: boolean; sortAlphabetically: boolean; sortByDuration: boolean; } @@ -54,6 +55,7 @@ export function loadTestFilters(): TestFilters { showIgnored: v.showIgnored !== false, showDurations: v.showDurations !== false, selectFirstFailed: v.selectFirstFailed === true, + trackRunning: v.trackRunning !== false, sortAlphabetically: v.sortAlphabetically === true && v.sortByDuration !== true, sortByDuration: v.sortByDuration === true, }; @@ -63,6 +65,7 @@ export function loadTestFilters(): TestFilters { showIgnored: true, showDurations: true, selectFirstFailed: false, + trackRunning: true, sortAlphabetically: false, sortByDuration: false, }; @@ -155,6 +158,19 @@ function suiteIds(nodes: readonly TestNode[], out: Set = new Set()): Set return out; } +/** Tests still running, in tree order, with the suites above them. */ +function runningTests( + nodes: readonly TestNode[], + ancestors: TestNode[] = [], + out: Array<{ node: TestNode; ancestors: TestNode[] }> = [], +) { + for (const n of nodes) { + if (n.children.length > 0) runningTests(n.children, [...ancestors, n], out); + else if (nodeStatus(n) === "running") out.push({ node: n, ancestors }); + } + return out; +} + function filterTree(nodes: readonly TestNode[], keep: (n: TestNode) => boolean): TestNode[] { const out: TestNode[] = []; for (const n of nodes) { @@ -213,7 +229,15 @@ export function TestTreeView({ rerunAutomatically, }: TestTreeViewProps) { const [filters, setFilters] = useState(loadTestFilters); - const { showPassed, showIgnored, showDurations, selectFirstFailed, sortAlphabetically, sortByDuration } = filters; + const { + showPassed, + showIgnored, + showDurations, + selectFirstFailed, + trackRunning, + sortAlphabetically, + sortByDuration, + } = filters; const toggle = (key: keyof TestFilters) => setFilters((f) => { const next = { ...f, [key]: !f[key] }; @@ -345,6 +369,8 @@ export function TestTreeView({ testTree.toggleDurations = () => toggle("showDurations"); testTree.selectFirstFailed = selectFirstFailed; testTree.toggleSelectFirstFailed = () => toggle("selectFirstFailed"); + testTree.trackRunning = trackRunning; + testTree.toggleTrackRunning = () => toggle("trackRunning"); return () => { testTree.expandAll = null; testTree.collapseAll = null; @@ -353,6 +379,8 @@ export function TestTreeView({ testTree.toggleDurations = null; testTree.selectFirstFailed = null; testTree.toggleSelectFirstFailed = null; + testTree.trackRunning = null; + testTree.toggleTrackRunning = null; }; }); @@ -372,6 +400,23 @@ export function TestTreeView({ useEffect(() => { if (firstFailed) tree.current?.reveal(firstFailed.key); }, [firstFailed]); + + // Track Running Test: while the run is in progress, select each test as it starts, as IDEA does. + const started = useMemo(() => (running ? runningTests(roots) : []), [roots, running]); + const [startedIds, setStartedIds] = useState>(new Set()); + const [tracked, setTracked] = useState<{ key: string } | null>(null); + if (started.length !== startedIds.size || started.some((t) => !startedIds.has(t.node.id))) { + setStartedIds(new Set(started.map((t) => t.node.id))); + const latest = trackRunning ? started.findLast((t) => !startedIds.has(t.node.id)) : undefined; + if (latest) { + if (latest.ancestors.some((a) => !effectiveExpanded.has(a.id))) + setExpanded(new Set([...effectiveExpanded, ...latest.ancestors.map((a) => a.id)])); + setTracked({ key: latest.node.id }); + } + } + useEffect(() => { + if (tracked) tree.current?.reveal(tracked.key); + }, [tracked]); const hasSuites = roots.some((n) => n.children.length > 0); // Previous / Next Failed Test (⌘⌥↑ / ⌘⌥↓): like IDEA, stops at the first / last failure instead of wrapping. @@ -494,6 +539,7 @@ export function TestTreeView({ icon: "settings", submenu: [ { kind: "action", action: "run.toggleTestDurations" }, + { kind: "action", action: "run.toggleTrackRunningTest" }, { kind: "action", action: "run.toggleSelectFirstFailedTest" }, ], }, diff --git a/src/features/run/index.ts b/src/features/run/index.ts index 1f0a784aa2..6ed98be16b 100644 --- a/src/features/run/index.ts +++ b/src/features/run/index.ts @@ -823,6 +823,15 @@ export function activate(): () => void { toggled: () => testTree.showDurations === true, run: () => testTree.toggleDurations?.(), }, + { + id: "run.toggleTrackRunningTest", + title: "Track Running Test", + category: "Run", + when: "projectOpen", + enabled: () => testTree.toggleTrackRunning !== null, + toggled: () => testTree.trackRunning === true, + run: () => testTree.toggleTrackRunning?.(), + }, { id: "run.toggleSelectFirstFailedTest", title: "Select First Failed Test When Finished", diff --git a/src/features/run/testTree.ts b/src/features/run/testTree.ts index 9a785268e4..4ae84e007b 100644 --- a/src/features/run/testTree.ts +++ b/src/features/run/testTree.ts @@ -11,6 +11,8 @@ export const testTree: { toggleDurations: (() => void) | null; selectFirstFailed: boolean | null; toggleSelectFirstFailed: (() => void) | null; + trackRunning: boolean | null; + toggleTrackRunning: (() => void) | null; } = { expandAll: null, collapseAll: null, @@ -19,4 +21,6 @@ export const testTree: { toggleDurations: null, selectFirstFailed: null, toggleSelectFirstFailed: null, + trackRunning: null, + toggleTrackRunning: null, }; diff --git a/src/platform/locales/text/ru.json b/src/platform/locales/text/ru.json index c1931ca242..d8d4b38214 100644 --- a/src/platform/locales/text/ru.json +++ b/src/platform/locales/text/ru.json @@ -9041,6 +9041,7 @@ "Next Failed Test": "Следующий непройденный тест", "Show Test Durations": "Показать длительность тестов", "Select First Failed Test When Finished": "Выбирать первый непройденный тест по завершении", + "Track Running Test": "Отслеживать выполняемый тест", "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 0b16777aa5..295c936649 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -8511,6 +8511,7 @@ "Previous Failed Test": "上一個失敗的測試", "Next Failed Test": "下一個失敗的測試", "Select First Failed Test When Finished": "完成後選取第一個失敗的測試", + "Track Running Test": "追蹤正在執行的測試", "Run '{name}' with Fuzzing": "使用模糊測試執行 '{name}'", "Reading…": "正在讀取…", "Editing…": "正在編輯…", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 5f25cb9abb..44c793749a 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -9263,6 +9263,7 @@ "Next Failed Test": "下一个失败的测试", "Show Test Durations": "显示测试耗时", "Select First Failed Test When Finished": "完成后选中第一个失败的测试", + "Track Running Test": "跟踪正在运行的测试", "Test Runner Options": "测试运行器选项", "Run '{name}' with Fuzzing": "使用模糊测试运行 '{name}'", "Reading…": "正在读取…",