diff --git a/packages/sdk-utils/src/index.js b/packages/sdk-utils/src/index.js index 61b0b4c0e..4c4425ae7 100644 --- a/packages/sdk-utils/src/index.js +++ b/packages/sdk-utils/src/index.js @@ -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, @@ -23,7 +24,8 @@ export { flushSnapshots, captureAutomateScreenshot, postBuildEvents, - getResponsiveWidths + getResponsiveWidths, + mergeSnapshotOptions }; // export the namespace by default diff --git a/packages/sdk-utils/src/merge-snapshot-options.js b/packages/sdk-utils/src/merge-snapshot-options.js new file mode 100644 index 000000000..14339eabe --- /dev/null +++ b/packages/sdk-utils/src/merge-snapshot-options.js @@ -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; diff --git a/packages/sdk-utils/test/index.test.js b/packages/sdk-utils/test/index.test.js index 5a80350ce..59f910655 100644 --- a/packages/sdk-utils/test/index.test.js +++ b/packages/sdk-utils/test/index.test.js @@ -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; + }); + }); });