Skip to content

Commit

Permalink
feat: remove snap suffix if use custom identifier (#305)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Kibala <ivankibala@Ivans-MacBook-Air.local>
  • Loading branch information
kibala145 and Ivan Kibala committed Aug 31, 2022
1 parent 1f4be75 commit 775ac0a
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/index.spec.js.snap
Expand Up @@ -49,7 +49,7 @@ Object {
"failureThresholdType": "pixel",
"receivedDir": undefined,
"receivedImageBuffer": "pretendthisisanimagebuffer",
"snapshotIdentifier": "test-spec-js-test-1",
"snapshotIdentifier": "test-spec-js-test-1-snap",
"snapshotsDir": "path/to/__image_snapshots__",
"storeReceivedOnFailure": false,
"updatePassedSnapshot": false,
Expand Down
8 changes: 4 additions & 4 deletions __tests__/diff-snapshot.spec.js
Expand Up @@ -114,7 +114,7 @@ describe('diff-snapshot', () => {

mockFs.existsSync.mockImplementation((p) => {
switch (p) {
case path.join(mockSnapshotsDir, `${mockSnapshotIdentifier}-snap.png`):
case path.join(mockSnapshotsDir, `${mockSnapshotIdentifier}.png`):
return snapshotExists;
case mockDiffDir:
return !!outputDirExists;
Expand All @@ -127,7 +127,7 @@ describe('diff-snapshot', () => {
mockFs.readFileSync.mockImplementation((p) => {
const bn = path.basename(p);

if (bn === 'id1-snap.png' && snapshotExists) {
if (bn === 'id1.png' && snapshotExists) {
return mockImageBuffer;
}

Expand Down Expand Up @@ -638,7 +638,7 @@ describe('diff-snapshot', () => {
});

expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
expect(mockWriteFileSync).toHaveBeenCalledWith(path.join(mockSnapshotsDir, `${mockSnapshotIdentifier}-snap.png`), mockImageBuffer);
expect(mockWriteFileSync).toHaveBeenCalledWith(path.join(mockSnapshotsDir, `${mockSnapshotIdentifier}.png`), mockImageBuffer);
});

it('should return updated flag if snapshot was updated', () => {
Expand Down Expand Up @@ -674,7 +674,7 @@ describe('diff-snapshot', () => {

expect(diffResult).toHaveProperty('added', true);
expect(mockWriteFileSync).toHaveBeenCalledWith(
path.join(mockSnapshotsDir, 'id1-snap.png'),
path.join(mockSnapshotsDir, 'id1.png'),
expect.any(Buffer)
);
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/index.spec.js
Expand Up @@ -450,7 +450,7 @@ describe('toMatchImageSnapshot', () => {
failureThreshold: 0,
failureThresholdType: 'pixel',
receivedImageBuffer: undefined,
snapshotIdentifier: 'test-spec-js-test-1-1',
snapshotIdentifier: 'test-spec-js-test-1-1-snap',
snapshotsDir: 'path/to/__image_snapshots__',
storeReceivedOnFailure: false,
updatePassedSnapshot: false,
Expand Down Expand Up @@ -575,7 +575,7 @@ describe('toMatchImageSnapshot', () => {
customDiffConfig: {
perceptual: true,
},
snapshotIdentifier: 'test-spec-js-test-1-1',
snapshotIdentifier: 'test-spec-js-test-1-1-snap',
snapshotsDir: path.join('path', 'to', 'my-custom-snapshots-dir'),
receivedDir: path.join('path', 'to', 'my-custom-received-dir'),
diffDir: path.join('path', 'to', 'my-custom-diff-dir'),
Expand Down
4 changes: 2 additions & 2 deletions __tests__/integration.spec.js
Expand Up @@ -27,7 +27,7 @@ describe('toMatchImageSnapshot', () => {
const customSnapshotsDir = path.resolve(__dirname, '__custom_snapshots_dir__');
const cleanupRequiredIndicator = 'cleanup-required-';
const getIdentifierIndicatingCleanupIsRequired = () => uniqueId(cleanupRequiredIndicator);
const getSnapshotFilename = identifier => `${identifier}-snap.png`;
const getSnapshotFilename = identifier => `${identifier}.png`;
const diffExists = identifier => fs.existsSync(path.join(__dirname, diffOutputDir(), `${identifier}-diff.png`));

beforeAll(() => {
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('toMatchImageSnapshot', () => {
describe('updates', () => {
const customSnapshotIdentifier = 'integration-update';
const updateImageData = fs.readFileSync(fromStubs('TestImageUpdate1pxOff.png'));
const updateImageSnapshotPath = path.join(__dirname, '__image_snapshots__', `${customSnapshotIdentifier}-snap.png`);
const updateImageSnapshotPath = path.join(__dirname, '__image_snapshots__', `${customSnapshotIdentifier}.png`);

beforeEach(() => {
fs.writeFileSync(updateImageSnapshotPath, imageData);
Expand Down
2 changes: 1 addition & 1 deletion src/diff-snapshot.js
Expand Up @@ -205,7 +205,7 @@ function diffImageToSnapshot(options) {

const comparisonFn = comparisonMethod === 'ssim' ? ssimMatch : pixelmatch;
let result = {};
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}-snap.png`);
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}.png`);
if (!fs.existsSync(baselineSnapshotPath)) {
mkdirp.sync(path.dirname(baselineSnapshotPath));
fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -108,7 +108,7 @@ function createSnapshotIdentifier({
const counter = snapshotState._counters.get(currentTestName);
const defaultIdentifier = kebabCase(`${path.basename(testPath)}-${currentTestName}-${counter}`);

let snapshotIdentifier = customSnapshotIdentifier || defaultIdentifier;
let snapshotIdentifier = customSnapshotIdentifier || `${defaultIdentifier}-snap`;

if (typeof customSnapshotIdentifier === 'function') {
const customRes = customSnapshotIdentifier({
Expand Down Expand Up @@ -195,7 +195,7 @@ function configureToMatchImageSnapshot({
const snapshotsDir = customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR);
const receivedDir = customReceivedDir;
const diffDir = customDiffDir;
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}-snap.png`);
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}.png`);
OutdatedSnapshotReporter.markTouchedFile(baselineSnapshotPath);

if (snapshotState._updateSnapshot === 'none' && !fs.existsSync(baselineSnapshotPath)) {
Expand Down

0 comments on commit 775ac0a

Please sign in to comment.