feat: better comparator function, close #3

This commit is contained in:
Gleb Bahmutov 2017-12-10 22:34:31 -05:00
parent f5f0b20492
commit 6af1b2cdcb
4 changed files with 48 additions and 16 deletions

View file

@ -1,6 +1,7 @@
{ {
"standard.enable": false, "standard.enable": false,
"eslint.enable": true, "eslint.enable": true,
"eslint.autoFixOnSave": true, "eslint.autoFixOnSave": false,
"git.ignoreLimitWarning": true "git.ignoreLimitWarning": true,
"standard.autoFixOnSave": false
} }

View file

@ -88,6 +88,7 @@
"its-name": "1.0.0", "its-name": "1.0.0",
"js-beautify": "1.7.5", "js-beautify": "1.7.5",
"lazy-ass": "1.6.0", "lazy-ass": "1.6.0",
"snap-shot-compare": "2.7.1",
"snap-shot-store": "1.2.3", "snap-shot-store": "1.2.3",
"switchcase": "0.0.3" "switchcase": "0.0.3"
} }

View file

@ -5,15 +5,24 @@ const itsName = require('its-name')
const { initStore } = require('snap-shot-store') const { initStore } = require('snap-shot-store')
const la = require('lazy-ass') const la = require('lazy-ass')
const is = require('check-more-types') const is = require('check-more-types')
const switchcase = require('switchcase')
const compare = require('snap-shot-compare')
const { const {
isJqueryElement, isJqueryElement,
serializeReactToHTML, serializeReactToHTML,
identity, identity,
countSnapshots countSnapshots
} = require('./utils') } = require('./utils')
const switchcase = require('switchcase')
/* eslint-disable no-console */ /* eslint-disable no-console */
function compareValues ({ expected, value }) {
const noColor = true
const json = true
return compare({ expected, value, noColor, json })
}
function registerCypressSnapshot () { function registerCypressSnapshot () {
la(is.fn(global.before), 'missing global before function') la(is.fn(global.before), 'missing global before function')
la(is.fn(global.after), 'missing global after function') la(is.fn(global.after), 'missing global after function')
@ -82,29 +91,47 @@ function registerCypressSnapshot () {
const message = Cypress._.last(name) const message = Cypress._.last(name)
console.log('current snapshot name', name) console.log('current snapshot name', name)
const devToolsLog = {
value
}
if (isJqueryElement($el)) {
// only add DOM elements, otherwise "expected" value is enough
devToolsLog.$el = $el
}
const options = { const options = {
name: 'snapshot', name: 'snapshot',
message, message,
consoleProps: () => { consoleProps: () => devToolsLog
const devToolsLog = {
value,
snapshot: message
}
if ($el) {
devToolsLog.$el = $el
}
return devToolsLog
}
} }
if ($el) { if ($el) {
options.$el = $el options.$el = $el
} }
const cyRaiser = ({ value, expected }) => {
const result = compareValues({ expected, value })
result.orElse((json) => {
// by deleting property and adding it at the last position
// we reorder how the object is displayed
// We want convenient:
// - message
// - expected
// - value
devToolsLog.message = json.message
devToolsLog.expected = expected
delete devToolsLog.value
devToolsLog.value = value
throw new Error(`Snapshot difference\n${json.message}`)
})
}
Cypress.log(options) Cypress.log(options)
storeSnapshot({ storeSnapshot({
value, value,
name name,
// compare: compareValues,
raiser: cyRaiser
}) })
} }

View file

@ -1,9 +1,12 @@
/* global Cypress */
const sd = require('@wildpeaks/snapshot-dom') const sd = require('@wildpeaks/snapshot-dom')
const beautify = require('js-beautify').html const beautify = require('js-beautify').html
function isJqueryElement (x) { function isJqueryElement (x) {
return 'wrap' in x // had to work around "switchcase" bug
function isObject (x) {
return x instanceof Object
}
return x && isObject(x) && 'wrap' in x
} }
// converts DOM element to a JSON object // converts DOM element to a JSON object