Repository navigation
bUnit.Analyzers #1792
Description
Analyzer package
We could invest some time to tackle some common issues users might face (or antipatterns) with our own analyzer. If we have a look into the issue tracker, we see this example a few times:
The render handle is not yet assigned.
Users might define a razor file with bunit like this:
@code
{
[Fact]
public void Test()
{
using var ctx = new BunitContext();
Action<MouseEventArgs> onClickHandler = _ => { Assert.True(true); };
var cut = ctx.Render(@<EventCallbackParams
OnClick="onClickHandler" />);
cut.Find("button").Click();
}
}This fails with
"System.InvalidOperationException : The render handle is not yet assigned."
Because the razor component isn't part of the RenderTree of the Blazor Renderer. So everytime we have a callback or variable (also two-way bindings) in razor syntax it might fail.
Therefore the fix is to do
@inherits BunitContext
@code
{
[Fact]
public async Task Test()
{
var wasInvoked = false;
Action<MouseEventArgs> onClick = _ => { wasInvoked = true; };
var cut = Render(@<EventCallbackParams OnClick="onClick" />);
var button = cut.Find("button");
await button.ClickAsync(new MouseEventArgs());
cut.WaitForAssertion(() => Assert.True(wasInvoked));
}
}Here we can easily help the user that if he uses a .razor file and has some variables passed from the test bi-directional (or events) to the cut, the user should use @inherits BunitContext.
Another one might be (in any variation):
IHtmlAnchorElement elem = (IHtmlAnchorElement)cut.Find("cssSelector");
// Could be
var elem = cut.Find<IHtmlAnchorElement>("cssSelector");We probably could find more examples.
Risks
- The biggest risk I see is our time. Maintaining that might be an additional burden.
Additional
- We need to add also some documentation in our
docs/directory so people know what the Analyzer is and does. - We might want to promote this in the README.md (link to the nuget package - once it is available)
Activity
ScarletKuro commentedon Dec 16, 2025
I’m not sure if this helps, but recently I’ve been exploring the boundaries of Copilot. This started after learning that MSFT is using it to make PRs for runtime repo. I wanted to implement an analyzer long time ago, but reading documentation, reviewing real code, writing it and testing takes takes too much time for an free open source project.
Here’s an attempt I made: MudBlazor/MudBlazor#12197 and so far from my experience, Copilot can actually produce a reasonably solid analyzer with a code fixer, as long as it’s given good context and examples.
I’m not sure whether this repository is eligible for a Copilot+, but if it is, it might be worth trying here as well.
Having an analyzer for bUnit wouldn’t hurt and could be quite beneficial.
linkdotnet commentedon Dec 16, 2025
I think that is an approach we can do. It would have to live in a separate package no matter what. So we can keep it experimental for quite some time.
I do think we have Copilot+ - at least I can assign tasks to Copilot. If not, I have a personal subscription that can do the job for me.