Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove snap suffix if use custom identifier #305

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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