diff --git a/core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php b/core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php
index 0378df8fa2..ba56d45a01 100644
--- a/core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php
+++ b/core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php
@@ -27,6 +27,13 @@ class AjaxRendererTest extends UnitTestCase {
    */
   protected $renderer;
 
+  /**
+   * TRUE to simulate empty messages.
+   *
+   * @var bool
+   */
+  protected $emptyMessages = FALSE;
+
   /**
    * {@inheritdoc}
    */
@@ -49,6 +56,9 @@ protected function setUp(): void {
           return $elements['#markup'];
         }
         elseif (isset($elements['#type'])) {
+          if ($this->emptyMessages) {
+            return "\n";
+          }
           return $elements['#type'];
         }
         else {
@@ -81,4 +91,31 @@ public function testRenderWithFragmentObject() {
     $this->assertEquals('status_messages', $commands[1]['data']);
   }
 
+  /**
+   * Tests that messages are not added to the commands array when we have an
+   * empty message.
+   *
+   * @covers ::renderResponse
+   */
+  public function testRenderWithFragmentObjectAndMessage() {
+    $main_content = ['#markup' => 'example content'];
+    $request = new Request();
+    $route_match = $this->createMock('Drupal\Core\Routing\RouteMatchInterface');
+
+    // This makes ['#type' => 'status_messages'] return just a new line.
+    $this->emptyMessages = TRUE;
+    /** @var \Drupal\Core\Ajax\AjaxResponse $result */
+    $result = $this->ajaxRenderer->renderResponse($main_content, $request, $route_match);
+
+    $this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $result);
+
+    $commands = $result->getCommands();
+    $this->assertEquals('insert', $commands[0]['command']);
+    $this->assertEquals('example content', $commands[0]['data']);
+
+    // Make sure that status messages are not prepended if there are no
+    // messages.
+    $this->assertEquals(1, count($commands));
+  }
+
 }
