From 54fa49092807ed36ede1fa7be1e791ab6b7e7f08 Mon Sep 17 00:00:00 2001 From: Jamie Ridley Date: Tue, 26 May 2020 17:10:43 +0100 Subject: [PATCH 1/2] Keep task options intact - When you call toMatchSnapshot or toMatchImageSnapshot, the options you pass in get merged with the plugin's entire collection of options. This complicates logic later on (and causes the bug the next commit fixes), where the original options need picking back out of the large object again. Instead, we just keep the originally passed in options as its own little item, while also keeping the merged version unscathed. --- commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.js b/commands.js index 88e8e7bb..3defb79a 100644 --- a/commands.js +++ b/commands.js @@ -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)); }); From 509b08bcd7a803bb6965b8611e82df2ce9817388 Mon Sep 17 00:00:00 2001 From: Jamie Ridley Date: Tue, 26 May 2020 17:14:22 +0100 Subject: [PATCH 2/2] Correctly apply all passed in screenshot options 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). --- __tests__/config.test.js | 15 +++++++++------ src/config.js | 34 +++++++++++++--------------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/__tests__/config.test.js b/__tests__/config.test.js index cc2f093e..6f416cd5 100644 --- a/__tests__/config.test.js +++ b/__tests__/config.test.js @@ -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, @@ -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'], @@ -40,6 +42,7 @@ describe('config', () => { }, disableTimersAndAnimations: true, log: true, + onBeforeScreenshot: 'some function', scale: false, timeout: 30000, }); diff --git a/src/config.js b/src/config.js index 21f6fa7b..881c2758 100644 --- a/src/config.js +++ b/src/config.js @@ -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() { @@ -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;