UNPKG

3.31 kBJavaScriptView Raw
1const la = require('lazy-ass')
2const is = require('check-more-types')
3const debug = require('debug')('snap-shot-store')
4const Result = require('folktale/result')
5const R = require('ramda')
6
7const isName = R.anyPass([is.unemptyString, is.strings])
8const nameLens = name =>
9 is.unemptyString(name) ? R.lensProp(name) : R.lensPath(name)
10const fullName = name => (is.unemptyString(name) ? name : name.join(' - '))
11
12// TODO allow list of strings for name
13function findValue ({ snapshots, name, opts = {} }) {
14 la(isName(name), 'invalid name to find spec for', name)
15
16 if (opts.update) {
17 // let the new value replace the current value
18 return
19 }
20 if (!snapshots) {
21 debug('there are no snapshots to find "%s"', name)
22 return
23 }
24
25 const lens = nameLens(name)
26 return R.view(lens, snapshots)
27 // const key = name
28 // debug('key "%s"', name)
29 // if (!(key in snapshots)) {
30 // return
31 // }
32
33 // return snapshots[key]
34}
35
36// given an object, and a key (or keys), and a value
37// stores the value in the object
38// returns a new object
39function storeValue ({ snapshots, name, value, opts = {} }) {
40 la(value !== undefined, 'cannot store undefined value')
41 la(is.object(snapshots), 'missing snapshots object', snapshots)
42 la(is.unemptyString(name) || is.strings(name), 'missing name', name)
43
44 const lens = nameLens(name)
45
46 if (opts.show || opts.dryRun) {
47 console.log('updated snapshot "%s"', name)
48 console.log(value)
49 }
50
51 if (!opts.dryRun) {
52 debug('setting snapshot for name "%s"', name)
53 return R.set(lens, value, snapshots)
54 }
55
56 return snapshots
57}
58
59const isValidCompareResult = is.schema({
60 orElse: is.fn
61})
62
63// expected = schema we expect value to adhere to
64// value - what the test computed right now
65// expected - existing value loaded from snapshot
66function raiseIfDifferent ({ value, expected, specName, compare }) {
67 la(value, 'missing value to compare', value)
68 la(expected, 'missing expected value', expected)
69 la(isName(specName), 'missing spec name', specName)
70
71 const result = compare({ expected, value })
72 la(
73 isValidCompareResult(result),
74 'invalid compare result',
75 result,
76 'when comparing value\n',
77 value,
78 'with expected\n',
79 expected
80 )
81
82 result.orElse(message => {
83 debug('Test "%s" snapshot difference', fullName(specName))
84 la(is.unemptyString(message), 'missing err string', message)
85 console.log(message)
86 throw new Error(message)
87 })
88}
89
90function compare ({ expected, value }) {
91 const e = JSON.stringify(expected)
92 const v = JSON.stringify(value)
93 if (e === v) {
94 return Result.Ok()
95 }
96 return Result.Error(`${e} !== ${v}`)
97}
98
99// make sure values in the object are "safe" to be serialized
100// and compared from loaded value
101function strip (o) {
102 if (is.fn(o)) {
103 return o
104 }
105 let stringified
106 try {
107 stringified = JSON.stringify(o)
108 } catch (e) {
109 console.error('Could not stringify value')
110 console.error('%j', o)
111 throw e
112 }
113
114 try {
115 const parsed = JSON.parse(stringified)
116 return parsed
117 } catch (e) {
118 console.error('Could not parse value from string')
119 console.error('%s', stringified)
120 console.error('from value %j', o)
121 throw e
122 }
123}
124
125module.exports = {
126 findValue,
127 storeValue,
128 raiseIfDifferent,
129 compare,
130 strip,
131 isName,
132 fullName
133}