Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/sdk-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import postBuildEvents from './post-build-event.js';
import flushSnapshots from './flush-snapshots.js';
import captureAutomateScreenshot from './post-screenshot.js';
import getResponsiveWidths from './get-responsive-widths.js';
import mergeSnapshotOptions from './merge-snapshot-options.js';

export {
logger,
Expand All @@ -23,7 +24,8 @@ export {
flushSnapshots,
captureAutomateScreenshot,
postBuildEvents,
getResponsiveWidths
getResponsiveWidths,
mergeSnapshotOptions
};

// export the namespace by default
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk-utils/src/merge-snapshot-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import percy from './percy-info.js';

// Merges .percy.yml config snapshot options with per-snapshot options.
// Per-snapshot options take priority over config options.
export function mergeSnapshotOptions(options) {
const configOptions = percy?.config?.snapshot || {};
return { ...configOptions, ...options };
}

export default mergeSnapshotOptions;
45 changes: 45 additions & 0 deletions packages/sdk-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,4 +648,49 @@ describe('SDK Utils', () => {
]);
});
});

describe('mergeSnapshotOptions(options)', () => {
let { mergeSnapshotOptions } = utils;

beforeEach(async () => {
await helpers.setupTest();
await utils.isPercyEnabled();
});

it('merges config snapshot options with per-snapshot options', () => {
const result = mergeSnapshotOptions({ enableJavaScript: true });
expect(result.enableJavaScript).toBe(true);
expect(result.widths).toEqual([375, 1280]);
});

it('gives per-snapshot options priority over config', () => {
const result = mergeSnapshotOptions({ widths: [768] });
expect(result.widths).toEqual([768]);
});

it('returns config options when no per-snapshot options are provided', () => {
const result = mergeSnapshotOptions();
expect(result.widths).toEqual([375, 1280]);
});

it('returns empty object when config.snapshot is undefined and no options given', () => {
const savedConfig = utils.percy.config;
utils.percy.config = { ...savedConfig, snapshot: undefined };

const result = mergeSnapshotOptions();
expect(result).toEqual({});

utils.percy.config = savedConfig;
});

it('returns only per-snapshot options when config.snapshot is undefined', () => {
const savedConfig = utils.percy.config;
utils.percy.config = { ...savedConfig, snapshot: undefined };

const result = mergeSnapshotOptions({ enableJavaScript: true });
expect(result).toEqual({ enableJavaScript: true });

utils.percy.config = savedConfig;
});
});
});
Loading