vitest-5-0-0-7-b
Claude Code's read-only tools, web search on, ludion sync on
Question
How do I check that an element's text contains a phrase in Vitest browser mode?
Answer key
- A correct answer says
- toHaveTextContent now matches strictly; use the new toMatchTextContent for partial matching.
- The stale answer
- toHaveTextContent('foo') passes when the text merely contains 'foo'.
Answer
Use `toMatchTextContent`: `await expect.element(el).toMatchTextContent('phrase')`. It also accepts a RegExp, such as `/phrase/i` for a case-insensitive match.
Don't use `toHaveTextContent` for this. In the installed vitest 5.x it only passes when the text matches exactly, so a partial phrase fails. Older code written for the earlier partial-match behaviour won't throw an error; those assertions will just start failing. The installed types say the same thing (`node_modules/@vitest/browser/jest-dom.d.ts:408`).Grade
correct
The judge's reason: It recommends toMatchTextContent for partial matching and states toHaveTextContent now requires an exact match, with no stale claim.
Session log
Tool call: Grep
{ "pattern": "toMatchTextContent|toHaveTextContent", "path": "C:\\Users\\haya0\\AppData\\Local\\Temp\\ludion-bench-zKtsex\\work\\node_modules", "glob": "*.d.ts", "output_mode": "content", "head_limit": 20 }Tool output
node_modules\@vitest\browser\jest-dom.d.ts:408: * If you wish to perform a partial check or use a RegExp, use `toMatchTextContent` instead. node_modules\@vitest\browser\jest-dom.d.ts:413: * await expect.element(element).toHaveTextContent('Text Content') node_modules\@vitest\browser\jest-dom.d.ts:414: * await expect.element(element).not.toHaveTextContent('Content') node_modules\@vitest\browser\jest-dom.d.ts:417: toHaveTextContent(text: string | number, options?: { normalizeWhitespace: boolean }): R node_modules\@vitest\browser\jest-dom.d.ts:432: * await expect.element(element).toMatchTextContent('Content') node_modules\@vitest\browser\jest-dom.d.ts:434: * await expect.element(element).toMatchTextContent(/^Text Content$/) node_modules\@vitest\browser\jest-dom.d.ts:436: * await expect.element(element).toMatchTextContent(/content$/i) node_modules\@vitest\browser\jest-dom.d.ts:437: * await expect.element(element).not.toMatchTextContent('content') node_modules\@vitest\browser\jest-dom.d.ts:440: toMatchTextContent(text: string | number | RegExp, options?: { normalizeWhitespace: boolean }): RAgent
Use `toMatchTextContent`: `await expect.element(el).toMatchTextContent('phrase')`. It also accepts a RegExp, such as `/phrase/i` for a case-insensitive match. Don't use `toHaveTextContent` for this. In the installed vitest 5.x it only passes when the text matches exactly, so a partial phrase fails. Older code written for the earlier partial-match behaviour won't throw an error; those assertions will just start failing. The installed types say the same thing (`node_modules/@vitest/browser/jest-dom.d.ts:408`).