Skip to content

Commit

Permalink
Merge pull request #1 from CityScience/fix/image-task-options
Browse files Browse the repository at this point in the history
Properly handle options passed into commands
  • Loading branch information
BoboRett authored May 29, 2020
2 parents 1854f7e + 509b08b commit 5d0e54e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 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
2 changes: 1 addition & 1 deletion commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function addCommand(commandName, method) {
return commandSubject;
}

const options = merge({}, cloneDeep(getConfig()), taskOptions);
const options = merge({}, cloneDeep(getConfig()), taskOptions, { taskOptions });
return cy.wrap(commandSubject, NO_LOG)
.then((subject) => method(subject, options));
});
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 5d0e54e

Please sign in to comment.