Skip to content

Commit

Permalink
Correctly apply all passed in screenshot options
Browse files Browse the repository at this point in the history
What originally happened was any options you pass
in to the command had to match any of the defaults
that the plugin sets, otherwise they never see the light of
day. I'm assuming this was done to stop the entire plugin
config from being passed into cy.screenshot() (et al.)
later on.

Now that all the original options passed into the
command are easily retrievable as options.taskOptions,
the merge with both the plugin defaults, the options
defined in cypress.json is easy (and right).
  • Loading branch information
BoboRett committed May 29, 2020
1 parent 54fa490 commit 509b08b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
15 changes: 9 additions & 6 deletions __tests__/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ const { getImageConfig, getScreenshotConfig } = require('../src/config');

describe('config', () => {
it('getImageConfig', () => {
const config = {
const config = { taskOptions: {
imageConfig: {
threshold: 0.5,
bar: 'should be ignored',
bar: 'should be included',
},
foo: 'should be ignored',
};
}};

expect(getImageConfig(config)).toEqual({
bar: 'should be included',
createDiffImage: true,
resizeDevicePixelRatio: true,
threshold: 0.5,
Expand All @@ -19,15 +20,16 @@ describe('config', () => {
});

it('getScreenshotConfig', () => {
const config = {
const config = { taskOptions: {
log: true,
clip: {
x: 0,
y: 0,
height: 100,
width: 100,
}
};
},
onBeforeScreenshot: 'some function'
}};

expect(getScreenshotConfig(config)).toEqual({
blackout: ['.snapshot-diff'],
Expand All @@ -40,6 +42,7 @@ describe('config', () => {
},
disableTimersAndAnimations: true,
log: true,
onBeforeScreenshot: 'some function',
scale: false,
timeout: 30000,
});
Expand Down
34 changes: 13 additions & 21 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const crypto = require('crypto');
const { merge, cloneDeep, clone } = require('lodash');
const { merge, cloneDeep, clone, get } = require('lodash');
const { TYPE_JSON } = require('./dataTypes');

function createToken() {
Expand Down Expand Up @@ -67,29 +67,21 @@ function getConfig() {
}

function getImageConfig(options = {}) {
return Object.keys(DEFAULT_IMAGE_CONFIG)
.filter((key) => options.imageConfig && options.imageConfig[key] !== undefined)
.reduce(
(imageConfig, key) => {
imageConfig[key] = options.imageConfig[key];
return imageConfig;
},
merge({}, DEFAULT_IMAGE_CONFIG, getConfig().imageConfig)
);
return merge(
{},
DEFAULT_IMAGE_CONFIG, // plugin defaults
options.imageConfig, // external config (cypress.json, command line args)
get(options.taskOptions, 'imageConfig') // options passed into the command
);
}


function getScreenshotConfig(options = {}) {
const screenshotConfig = Object.keys(DEFAULT_SCREENSHOT_CONFIG)
.filter((key) => options && options[key] !== undefined)
.reduce(
(imageConfig, key) => {
imageConfig[key] = options[key];
return imageConfig;
},
merge({}, DEFAULT_SCREENSHOT_CONFIG, getConfig().screenshotConfig)
);

const screenshotConfig = merge(
{},
DEFAULT_SCREENSHOT_CONFIG, // plugin defaults
options.screenshotConfig, // external config (cypress.json, command line args)
options.taskOptions // options passed into the command
);
screenshotConfig.blackout = (screenshotConfig.blackout || []);
screenshotConfig.blackout.push('.snapshot-diff');
return screenshotConfig;
Expand Down

0 comments on commit 509b08b

Please sign in to comment.