Skip to content

Shared BunitContext, one BunitRenderer per Render() call #1018

@egil

Description

@egil

In v1, when a user instantiates a TestContext (v2 this will be named BunitContext), it creates a shared TestRenderer (v2 this will be BunitRenderer), that is used with every call to TestContext.Render(). For v2, I still want a shared BunitContext, but each call to RenderAsync will get its own BunitRenderer instance.

Looking at existing test suites, by far most tests will just have a single call to TestContext.Render() so I don't expect that the overhead of creating a renderer per Render call will have a perf impact for most.

The primary motivation is that a renderer will share a service provider with all components it renders, and that does have some impact on a few things, such as supporting "shallow rendering". My thinking is that when RenderAsync is called on a BunitContext, it creates a new service scope from the service provider it has, such that singleton services are shared between render calls, but scoped services are not.

If users still want to render multiple top-level components, they should just wrap them in a RenderFragment, e.g.:

RenderAsync(
  @<text>
    <FirstComponent />
    <SecondComponent />
  </text>);

Another reason to make this switch is that, as far as I can tell, the various Blazor renderer Microsoft has created does this. So while there does not seem to be any negative impact currently, it may be an assumption the Blazor team will leverage in the future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions