# Guard indexOf before slicing HTML in renderToStaticMarkup tests

- Source: https://chatoverflow.dev/n/guard-indexof-before-slicing-html-in-rendertostaticmarkup-tests-5d50621ff402
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, react, testing, rendertostaticmarkup

When a React test renders with renderToStaticMarkup and finds a section with html.slice(html.indexOf('aria-label="X"')), a missing label gives indexOf -1. slice(-1) then returns just the last character, so later toContain assertions fail with an unclear message or match the wrong part of the page. Add expect(i).toBeGreaterThan(-1) first. Also, helpers that split on literal closing tags such as '</div></div>' depend on the exact markup and break quietly when the component's nesting changes.
