Skip to content

Commit 394445f

Browse files
authored
[Blazor] Improve the reliability of 'JSInteropThrowsInUserCode' (#15219)
* Moves the Dispose logic into `DisposeAsync` instead of `IAsyncLifetime.DisposeAsync`. * Adds a `try{...}catch{...}` around output.WriteLine to prevent situations where writing a log into the ITestOutput outside of the context of the test causes an exception that makes the test fail.
1 parent fe62ce7 commit 394445f

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/Components/test/E2ETest/ServerExecutionTests/IgnitorTest.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ async Task IAsyncLifetime.InitializeAsync()
7575

7676
async Task IAsyncLifetime.DisposeAsync()
7777
{
78-
if (TestSink != null)
79-
{
80-
TestSink.MessageLogged -= TestSink_MessageLogged;
81-
}
82-
8378
await DisposeAsync();
8479
}
8580

@@ -90,14 +85,30 @@ protected virtual Task InitializeAsync()
9085

9186
protected virtual Task DisposeAsync()
9287
{
88+
if (TestSink != null)
89+
{
90+
TestSink.MessageLogged -= TestSink_MessageLogged;
91+
}
92+
9393
return Task.CompletedTask;
9494
}
9595

9696
private void TestSink_MessageLogged(WriteContext context)
9797
{
9898
var log = new LogMessage(context.LogLevel, context.EventId, context.Message, context.Exception);
9999
Logs.Enqueue(log);
100-
Output.WriteLine(log.ToString());
100+
try
101+
{
102+
// This might produce an InvalidOperationException when the logger tries to log a message after
103+
// the test has completed but before the handler has been removed.
104+
// ---> System.InvalidOperationException: There is no currently active test.
105+
// For that reason, we capture the exception here and silence it, as the message is captured inside the Logs
106+
// variable anyway.
107+
Output.WriteLine(log.ToString());
108+
}
109+
catch (Exception)
110+
{
111+
}
101112
}
102113

103114
[DebuggerDisplay("{LogLevel.ToString(),nq} - {Message ?? \"null\",nq} - {Exception?.Message,nq}")]

0 commit comments

Comments
 (0)