Stimulus responds to document changes in the next tick of the microtask queue.
In JavaScript, the easiest way to queue a microtask is to wait on an empty promise:
document.body.insertAdjacentHTML("beforeend", "...")
Promise.resolve().then(() => {
// Stimulus has responded to the change by now
})
In Capybara, you’ll probably just want to sleep for a very small amount of time after each DOM modification. (I’m not an expert with Capybara, so feel free to chime in here if you are!)