Function comparePdfToSnapshot

  • Compares a PDF to a persisted snapshot, with behavior for handling missing snapshots controlled by the failOnMissingSnapshot option.

    Parameters

    • pdf: string | Buffer

      Path to the PDF file or a Buffer containing the PDF.

    • snapshotDir: string

      Path to the directory where the __snapshots__ folder will be created.

    • snapshotName: string

      Unique name for the snapshot within the specified path.

    • Optionaloptions: CompareOptions

      Options for comparison, including tolerance, mask regions, and behavior regarding missing snapshots. See CompareOptions for more details.

    Returns Promise<boolean>

    A promise that resolves to true if the PDF matches the snapshot or if the behavior allows for missing snapshots. Resolves to false if the PDF differs from the snapshot or if failOnMissingSnapshot is true and no snapshot exists.

    The function has the following side effects:

    • If no snapshot exists:
      • If failOnMissingSnapshot is false (default), the PDF is converted to an image, saved as a new snapshot, and the function returns true.
      • If failOnMissingSnapshot is true, the function returns false without creating a new snapshot.
    • If a snapshot exists, the PDF is converted to an image and compared to the snapshot:
      • If they differ, the function returns false and creates two additional images next to the snapshot: one with the suffix new (the current view of the PDF as an image) and one with the suffix diff (showing the difference between the snapshot and the new image).
      • If they are equal, the function returns true. If new and diff versions are present, they are deleted.