Skip to content

Commit

Permalink
fix: remove new snapshot when existing snapshot matches (#76)
Browse files Browse the repository at this point in the history
* chore: add failing test

* fix: remove new snapshot when existing snapshot matches

* fix: enable tests

* chore: use fs/promises in compare-pdf-to-snapshot

* Revert "chore: use fs/promises in compare-pdf-to-snapshot"

This reverts commit 04beef3.
  • Loading branch information
moshensky authored Sep 26, 2024
1 parent 10373ed commit f3469d6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## UNRELEASED

### 🐛 Bug Fix

- [PR#76](https://github.com/moshensky/pdf-visual-diff/pull/76) fix: remove new snapshot when existing snapshot matches

## 0.12.0 / 2024-09-26

### :tada: Enhancements
Expand Down
Binary file added src/__snapshots__/should-remove-diff-and-new.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 29 additions & 4 deletions src/compare-pdf-to-snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { describe, it } from 'node:test'
import * as assert from 'node:assert/strict'
import { join } from 'node:path'
import { existsSync, unlinkSync } from 'node:fs'
import { unlink, readFile } from 'node:fs/promises'
import { Jimp, JimpInstance } from 'jimp'
import {
comparePdfToSnapshot,
snapshotsDirName,
CompareOptions,
RegionMask,
} from './compare-pdf-to-snapshot'
import { join } from 'path'
import { existsSync, unlinkSync } from 'fs'
import { compareImages } from './compare-images'
import * as fs from 'fs/promises'
import { Dpi } from './types'

const testDataDir = join(__dirname, './test-data')
Expand All @@ -20,6 +20,14 @@ const singlePageSmallPdfPath = join(pdfs, 'single-page-small.pdf')
const singlePagePdfPath = join(pdfs, 'single-page.pdf')
const twoPagePdfPath = join(pdfs, 'two-page.pdf')

async function removeIfExists(filePath: string): Promise<void> {
try {
await unlink(filePath)
} catch {
// File doesn't exist, no need to remove
}
}

describe('comparePdfToSnapshot()', () => {
it('should create new snapshot, when one does not exists', () => {
const snapshotName = 'single-page-small'
Expand All @@ -43,6 +51,23 @@ describe('comparePdfToSnapshot()', () => {
assert.strictEqual(existsSync(snapshotNewPath), true, 'new is not created')
}))

it('should remove diff and new snapshots when matches with reference snapshot', async () => {
const snapshotName = 'should-remove-diff-and-new'
const snapshotBase = join(__dirname, snapshotsDirName, snapshotName)
const snapshotPathDiff: `${string}.${string}` = `${snapshotBase}.diff.png`
const snapshotPathNew: `${string}.${string}` = `${snapshotBase}.new.png`

await removeIfExists(snapshotPathDiff)
await removeIfExists(snapshotPathNew)
await new Jimp({ width: 100, height: 100 }).write(snapshotPathDiff)
await new Jimp({ width: 100, height: 100 }).write(snapshotPathNew)

const isEqual = await comparePdfToSnapshot(singlePageSmallPdfPath, __dirname, snapshotName)
assert.strictEqual(isEqual, true)
assert.strictEqual(existsSync(snapshotPathDiff), false, 'Snapshot diff should not exists.')
assert.strictEqual(existsSync(snapshotPathNew), false, 'Snapshot new should not exists.')
})

describe('should pass', () => {
it('should pass', () =>
comparePdfToSnapshot(twoPagePdfPath, __dirname, 'two-page-success').then((x) =>
Expand Down Expand Up @@ -75,7 +100,7 @@ describe('comparePdfToSnapshot()', () => {
pdf2PngOptions: { dpi: Dpi.Low },
}))
it('two-page.pdf', () => testPdf2png(twoPage, 'two-page'))
it('two-page.pdf buffer', () => fs.readFile(twoPage).then((x) => testPdf2png(x, 'two-page')))
it('two-page.pdf buffer', () => readFile(twoPage).then((x) => testPdf2png(x, 'two-page')))
})

describe('maskRegions', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/compare-pdf-to-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,19 @@ export function comparePdfToSnapshot(
.then((images) =>
compareImages(snapshotPath, images, restOpts).then((result) => {
const diffSnapshotPath = path.join(dir, snapshotName + '.diff.png')
const newSnapshotPath = path.join(dir, snapshotName + '.new.png')
if (result.equal) {
if (existsSync(diffSnapshotPath)) {
unlinkSync(diffSnapshotPath)
}

if (existsSync(newSnapshotPath)) {
unlinkSync(newSnapshotPath)
}

return true
}

const newSnapshotPath = path.join(dir, snapshotName + '.new.png')
return writeImages(newSnapshotPath)(images)
.then(() => writeImages(diffSnapshotPath)(result.diffs.map((x) => x.diff)))
.then(() => false)
Expand Down

0 comments on commit f3469d6

Please sign in to comment.