Skip to content

Commit

Permalink
Merge pull request #29 from llonchj/master
Browse files Browse the repository at this point in the history
generate_screenshot_path setting not honored in globals.visual_regression_settings
  • Loading branch information
msteitle authored Apr 2, 2019
2 parents 65b2776 + d66cc32 commit 1663e1e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ yarn.lock
reports
package-lock.json
selenium-debug.log
.DS_Store
9 changes: 3 additions & 6 deletions lib/generate-screenshot-file-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const kebabCase = require('lodash/kebabCase'),
path = require('path'),
get = require('lodash/get')
get = require('lodash/get'),
getVrtSettings = require('./get-vrt-settings')


function appendExtensionToFilename(fileName) {
Expand Down Expand Up @@ -41,11 +42,7 @@ function defaultScreenshotFilePath(nightwatchClient, basePath, fileName) {
* @param {String} [fileName] A custom filename for the screenshot
*/
module.exports = function generateScreenshotFilePath(nightwatchClient, basePath, fileName) {
const globalSettings = get(
nightwatchClient,
'globals.test_settings.visual_regression_settings',
null
)
const globalSettings = getVrtSettings(nightwatchClient)

if (globalSettings && globalSettings.generate_screenshot_path) {
return globalSettings.generate_screenshot_path(
Expand Down
3 changes: 2 additions & 1 deletion lib/get-vrt-settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const get = require('lodash/get')
const isEmpty = require('lodash/isEmpty')

/**
* Get the visual regression tests settings from the nightwatch configuration file.
Expand All @@ -17,7 +18,7 @@ module.exports = function getVrtSettings(nightwatchClient, overrideSettings) {

const settings = visualRegressionSettingsPaths.map((settingPath) =>
get(nightwatchClient, settingPath, null)
).find((vrtSettings) => vrtSettings)
).filter((result) => !isEmpty(result))[0]

return Object.assign(
{
Expand Down
43 changes: 31 additions & 12 deletions tests/lib/generate-screenshot-file-path-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
'use strict'

const generateScreenshotFilePath = require('../../lib/generate-screenshot-file-path')
const path = require('path')
const set = require('lodash/set')

describe('generateScreenshotFilePath', () => {
let customNameFn

function getNightwatchClient() {
return {
currentTest: {
module: 'visualizations',
name: 'barPlots'
},
globals: {
test_settings: {
visual_regression_settings: {
generate_screenshot_path: customNameFn
}
}
visual_regression_settings: {},
test_settings: { visual_regression_settings: {} }
}
}
}
Expand All @@ -33,10 +27,35 @@ describe('generateScreenshotFilePath', () => {
})

it('should pass the correct filename to the custom naming function', () => {
customNameFn = (client, basePath, fileName) =>
path.join(process.cwd(), basePath, client.currentTest.module, `custom-${fileName}`)
const nightwatchClient = getNightwatchClient()
const testFn = jest.fn((client, basePath, fileName) =>
path.join(process.cwd(), basePath, client.currentTest.module, `test-setting-custom-${fileName}`))

expect(generateScreenshotFilePath(getNightwatchClient(), 'baseline', 'foo-test'))
set(nightwatchClient, 'globals.test_settings.visual_regression_settings.generate_screenshot_path', testFn)

const result = generateScreenshotFilePath(nightwatchClient, 'baseline', 'foo-test')

expect(testFn).toHaveBeenCalled()
expect(result)
.toEqual(`${process.cwd()}/baseline/visualizations/test-setting-custom-foo-test.png`)
})

it('should use the base global setting for generate_screenshot_path if a test_setting exists', () => {
const nightwatchClient = getNightwatchClient()
const testFn = jest.fn((client, basePath, fileName) =>
path.join(process.cwd(), basePath, client.currentTest.module, `test-setting-custom-${fileName}`))

set(nightwatchClient, 'globals.test_settings.visual_regression_settings.generate_screenshot_path', testFn)

const customFn = jest.fn((client, basePath, fileName) =>
path.join(process.cwd(), basePath, client.currentTest.module, `custom-${fileName}`))

set(nightwatchClient, 'globals.visual_regression_settings.generate_screenshot_path', customFn)
set(nightwatchClient, 'globals.test_settings.visual_regression_settings.generate_screenshot_path', testFn)

const result = generateScreenshotFilePath(nightwatchClient, 'baseline', 'foo-test')

expect(result, 'baseline', 'foo-test')
.toEqual(`${process.cwd()}/baseline/visualizations/custom-foo-test.png`)
})
})

0 comments on commit 1663e1e

Please sign in to comment.