diff --git a/CHANGELOG.md b/CHANGELOG.md
index 476512176..7e1dc956f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad
## [Unreleased]
+### Changed
+
+- The markup of a rendered component is generated when it is read (`Markup`, `Nodes`, `Find`) instead of after every render batch. A component returned to a test was notified once per changed child in a batch and regenerated its full markup each time; on large components this dominated test run time. `OnMarkupUpdated` is still raised after each render that changes the markup.
+
## [2.15.2] - 2026-11-10
### Added
diff --git a/Directory.Packages.props b/Directory.Packages.props
index ca5ce9937..6caf2ebfb 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -18,7 +18,7 @@
-
+
diff --git a/src/bunit/Rendering/BunitRenderer.cs b/src/bunit/Rendering/BunitRenderer.cs
index 46fedfb45..e82b6fc5d 100644
--- a/src/bunit/Rendering/BunitRenderer.cs
+++ b/src/bunit/Rendering/BunitRenderer.cs
@@ -32,6 +32,20 @@ public sealed class BunitRenderer : Renderer
private TaskCompletionSource unhandledExceptionTsc = new(TaskCreationOptions.RunContinuationsAsynchronously);
private Exception? capturedUnhandledException;
+ ///
+ /// Runs while no render batch can change the render tree, e.g. to generate
+ /// the markup of a rendered component on demand. Re-entrant: a caller that already holds the lock,
+ /// such as an OnMarkupUpdated handler invoked from , runs directly.
+ /// Returns once the renderer is disposed.
+ ///
+ internal T ReadRenderTree(Func read, T fallback)
+ {
+ lock (renderTreeUpdateLock)
+ {
+ return disposed ? fallback : read();
+ }
+ }
+
private bool IsBatchInProgress
{
#pragma warning disable S1144 // Unused private types or members should be removed
diff --git a/src/bunit/Rendering/RenderedComponent.cs b/src/bunit/Rendering/RenderedComponent.cs
index 22e98e1d2..834cb883f 100644
--- a/src/bunit/Rendering/RenderedComponent.cs
+++ b/src/bunit/Rendering/RenderedComponent.cs
@@ -20,6 +20,12 @@ internal sealed class RenderedComponent : ComponentState, IRenderedC
private string markup = string.Empty;
private MarkupNodes? latestRenderNodes;
+ // Set when a render changed this component's subtree; the markup is generated on the next read of
+ // Markup instead of after every render batch. A component returned to a test (cut, FindComponent)
+ // is notified once per changed child in a batch, so eager generation rebuilt the same markup many
+ // times per batch (measured: a 100 KB page regenerated 44-284 times per state change).
+ private volatile bool markupStale;
+
///
/// Gets the component under test.
///
@@ -45,6 +51,25 @@ public string Markup
get
{
EnsureComponentNotDisposed();
+
+ if (markupStale)
+ {
+ // Generated under the renderer's render tree lock, like FindComponents, so no batch can
+ // change the tree while it is read. The flag is re-checked inside the lock: another
+ // thread may have generated the markup meanwhile.
+ return renderer.ReadRenderTree(
+ () =>
+ {
+ if (markupStale)
+ {
+ UpdateMarkup();
+ }
+
+ return Volatile.Read(ref markup);
+ },
+ Volatile.Read(ref markup));
+ }
+
// Volatile read is necessary to ensure the updated markup
// is available across CPU cores. Without it, the pointer to the
// markup string can be stored in a CPUs register and not
@@ -126,7 +151,9 @@ public void UpdateState(bool hasRendered, bool isMarkupGenerationRequired)
if (isMarkupGenerationRequired)
{
- UpdateMarkup();
+ // Only mark the markup as stale; Markup generates it on the next read. Subscribers of
+ // OnMarkupUpdated (e.g. the WaitFor helpers) read it and so still see the new markup.
+ markupStale = true;
OnMarkupUpdated?.Invoke(this, EventArgs.Empty);
}
@@ -142,6 +169,7 @@ public void UpdateState(bool hasRendered, bool isMarkupGenerationRequired)
///
private void UpdateMarkup()
{
+ markupStale = false;
latestRenderNodes = null;
var newMarkup = Htmlizer.GetHtml(ComponentId, renderer);
@@ -169,6 +197,7 @@ public void Dispose()
return;
IsDisposed = true;
+ markupStale = false;
markup = string.Empty;
OnAfterRender = null;
OnMarkupUpdated = null;
diff --git a/tests/bunit.tests/Rendering/RenderedComponentTest.cs b/tests/bunit.tests/Rendering/RenderedComponentTest.cs
index f2ab250f4..f28656df4 100644
--- a/tests/bunit.tests/Rendering/RenderedComponentTest.cs
+++ b/tests/bunit.tests/Rendering/RenderedComponentTest.cs
@@ -320,6 +320,56 @@ public void Test030()
cut.Find("p").TextContent.ShouldBe("2");
}
+ [Fact(DisplayName = "Markup returns the same instance on repeated reads when nothing rendered in between")]
+ public void Test031()
+ {
+ var cut = Render();
+
+ var first = cut.Markup;
+ var second = cut.Markup;
+
+ second.ShouldBeSameAs(first);
+ }
+
+ [Fact(DisplayName = "Markup reflects the last of several renders that were not read in between")]
+ public void Test032()
+ {
+ var cut = Render();
+ var button = cut.Find("button");
+
+ button.Click();
+ button.Click();
+ button.Click();
+
+ cut.Find("p").TextContent.ShouldBe("3");
+ }
+
+ [Fact(DisplayName = "OnMarkupUpdated subscribers read the updated markup")]
+ public void Test033()
+ {
+ var cut = Render();
+ var markupSeenBySubscriber = new List();
+ cut.OnMarkupUpdated += (_, _) => markupSeenBySubscriber.Add(cut.Markup);
+
+ cut.Find("button").Click();
+
+ markupSeenBySubscriber.ShouldNotBeEmpty();
+ markupSeenBySubscriber[^1].ShouldContain("1
");
+ }
+
+ [Fact(DisplayName = "Markup of a parent reflects a render of a child component found via FindComponent")]
+ public void Test034()
+ {
+ var cut = Render(ps => ps.AddChildContent());
+ _ = cut.Markup;
+ var child = cut.FindComponent();
+
+ child.Find("button").Click();
+
+ child.Find("p").TextContent.ShouldBe("1");
+ cut.Find("p").TextContent.ShouldBe("1");
+ }
+
private class BaseComponent : ComponentBase
{
protected override void BuildRenderTree(RenderTreeBuilder builder)
diff --git a/version.json b/version.json
index bfaebb016..10f89b27a 100644
--- a/version.json
+++ b/version.json
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
- "version": "2.15",
+ "version": "2.16-preview",
"assemblyVersion": {
"precision": "revision"
},