Skip to content

toMatchTextFile ​

Asserts that a string value matches a text file snapshot. The string is trimmed and compared to a stored validation file. Supports retries when a callback function is provided as the actual value.

Usage ​

Static Snapshot ​

ts
test("matches page title", async () => {
  await page.setContent("<title>Page Title</title>");

  await expect(page.title()).toMatchTextFile();
});

Output:

matches_page_title.txt
Page Title

Repeatable Snapshot ​

When passing a callback function as actual value, the snapshot will be retried until it matches the stored validation file:

ts
test("matches page title eventually", async () => {
  await page.setContent("<title>Page Title</title>");

  await expect(() => page.title()).toMatchTextFile();
});

Custom File Extension ​

Use fileExtension to store snapshots with a different file extension:

ts
test("matches HTML snapshot", async ({ page }) => {
  await page.setContent("<main>Hello World</main>");

  await expect(() => page.getByRole("main").innerHTML()).toMatchTextFile({
    fileExtension: "html",
  });
});

Output:

matches_HTML_snapshot.html
html
<main>Hello World</main>

Options ​

OptionDefault ValueDescription
fileExtensiontxtFile extension used for storing the snapshot file.
nameundefinedUnique name of the file snapshot. Used to distinguish multiple file snapshots within the same test. See Named Snapshots.
normalizers[]Custom normalizers to apply before serialization. See Normalization of Snapshots.
timeoutexpect timeoutRetries the snapshot until it passes or the timeout is reached. Only applies when a callback is passed. See Snapshot Retries.
updateDelay250Delay in ms before a repeatable snapshot is created in update mode. See Snapshot Retries.
resolveFilePathresolveNameAsFileCustom resolver for the file path used to store snapshots. See File Path Resolvers.