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.

Usage

ts
test("value is expected value", () => {
  expect("expected value").toMatchTextFile();
});

Output (value_is_expected_value.txt):

expected value

Custom File Extension

Use fileExtension to store snapshots with a different file extension:

ts
test("html snapshot", () => {
  expect("<main>Hello World</main>").toMatchTextFile({ fileExtension: "html" });
});

Output (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.
resolveFilePathresolveNameAsFileCustom resolver for the file path used to store snapshots.