microsoft/TypeScript · error · Error
verifyTextAtCaretIs\n
Error message
verifyTextAtCaretIs\n
What it means
Thrown by FourSlashTestState.verifyTextAtCaretIs when the substring extracted from the active file starting at the current caret position does not match the expected text. The error message includes a quoted expected-vs-actual diff via displayExpectedAndActualString. This is an assertion inside the fourslash test harness that verifies editor content at a precise caret offset.
Source
Thrown at src/harness/fourslashImpl.ts:3063
private verifyFileContent(fileName: string, text: string) {
const actual = this.getFileContent(fileName);
if (actual !== text) {
throw new Error(`verifyFileContent in file '${fileName}' failed:\n${showTextDiff(text, actual)}`);
}
}
public verifyFormatDocumentChangesNothing(): void {
const { fileName } = this.activeFile;
const before = this.getFileContent(fileName);
this.formatDocument();
this.verifyFileContent(fileName, before);
}
public verifyTextAtCaretIs(text: string): void {
const actual = this.getFileContent(this.activeFile.fileName).substring(this.currentCaretPosition, this.currentCaretPosition + text.length);
if (actual !== text) {
throw new Error("verifyTextAtCaretIs\n" + displayExpectedAndActualString(text, actual, /*quoted*/ true));
}
}
public verifyCurrentNameOrDottedNameSpanText(text: string): undefined {
const span = this.languageService.getNameOrDottedNameSpan(this.activeFile.fileName, this.currentCaretPosition, this.currentCaretPosition);
if (!span) {
return this.raiseError("verifyCurrentNameOrDottedNameSpanText\n" + displayExpectedAndActualString('"' + text + '"', "undefined"));
}
const actual = this.getFileContent(this.activeFile.fileName).substring(span.start, ts.textSpanEnd(span));
if (actual !== text) {
this.raiseError("verifyCurrentNameOrDottedNameSpanText\n" + displayExpectedAndActualString(text, actual, /*quoted*/ true));
}
}
private getNameOrDottedNameSpan(pos: number) {
return this.languageService.getNameOrDottedNameSpan(this.activeFile.fileName, pos, pos);
}
View on GitHub (pinned to b465fdbfe1)
Solutions
- Inspect the quoted expected/actual diff in the message to see exactly which characters differ.
- Verify the caret is on the intended position: check the most recent goTo.marker / goTo.position call and confirm the marker name or offset is correct.
- If the caret is right but the expectation is stale, update the expected string argument to match actual file content.
- If a codefix/completion was just applied, confirm the applied edit produced the text you assume (use verify.fileContent or print getFileContent).
Example fix
// before
verify.textAtCaretIs("const x = 1;");
// after — caret was one column off, adjust expected to actual
verify.textAtCaretIs("const x = 10;"); Defensive patterns
Strategy: validation
Validate before calling
// Before calling verify.textAtCaretIs, read the actual substring and compare yourself
const expected = "const x = 10;";
const actual = state.getFileContent(state.activeFile.fileName)
.substring(state.currentCaretPosition, state.currentCaretPosition + expected.length);
if (actual === expected) {
verify.textAtCaretIs(expected);
} Prevention
- Always pair goTo.marker calls with the exact expected text at that marker to avoid caret/content drift.
- When updating test content, re-derive expected caret strings from the file rather than hardcoding.
When it happens
Trigger: Calling verify.textAtCaretIs("expected") (or the underlying state.verifyTextAtCaretIs) after moving the caret via goTo.marker/goTo.position, but the file content near that offset differs from the expected string. The substring taken is exactly text.length characters from currentCaretPosition.
Common situations: A test author edits the expected string but forgets to update the marker position, or a refactor/codefix was applied but the caret moved further than expected. Also happens when goTo.marker targets the wrong marker so the caret lands on different content.
Related errors
- verifyCurrentLineContent\n
- verifyFileContent in file '${fileName}' failed:\n${showTextD
- You need to change the source code of fourslash test to use
- Exactly one refactor range is allowed per test.
- File index (${index}) in openFile was out of range. There ar
AI-assisted analysis of microsoft/TypeScript@b465fdbfe1 (2026-08-12).
Data as JSON: /api/errors/674dcb77b83a863f.
Report an issue: GitHub.