-
Notifications
You must be signed in to change notification settings - Fork 16
/
snap-shot-it.javascript.txt
110 lines (84 loc) · 7.28 KB
/
snap-shot-it.javascript.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
SNAP-SHOT-IT
ALTERNATIVES ==> #See jest snapshots doc
SNAP-SHOT-CORE ==> #Version 5.0.4
/=+===============================+=\
/ : : \
)==: ASSERTIONS :==(
\ :_______________________________: /
\=+===============================+=/
SNAP-SHOT-CORE(OPTS) #Compare OPTS.what against snapshot value:
# - if snapshot not there, create it
# - otherwise, throw error if different
#OPTS:
# - what [PROMISE_]VAL: value to compare
# - file|__filename 'TEST_PATH'
# - ext '.EXT' (def: '.snapshot.js')
# - [exact]specName 'NAME'
# - if 'exact', does not append 'OFFSET'
# - opts:
# - update BOOL (def: false): forces updating snapshot
# - ci BOOL (def: IS-CI, see its doc): throw if snapshot is missing
# - show BOOL (def: false): console.log() on snapshot save
# - dryRun BOOL (def: false): do not save snapshots
# - compare({ value, expected }):
# - customize value comparison (def: JSON comparison)
# - can use SNAP-SHOT-COMPARE package, which throws a prettified diff
# (using disparity, not maintained module)
# - must reject with 'ERROR' if different
# - must return a forktale/result object (see its doc)
# - raiser(OPTS):
# - customize what to do on difference (def: throw error message)
# - OPTS: value VAL, expected VAL2, compare({ value, expected }), specName 'NAME'
# - store(VAL)->VAL:
# - process value before being JSON serialized before saving
SNAP-SHOT-CORE.restore([OPTS]) #Resets OFFSET (from 'NAME OFFSET') to 0
#OPTS: specName 'NAME': target a specific one
SNAP-SHOT-CORE.prune(OPTS) #Only keep specified 'NAME' in specified snapshots
#OPTS:
# - tests OBJ_ARR: file 'TEST_PATH', specName 'NAME'
# - ext '.EXT'
/=+===============================+=\
/ : : \
)==: FILES :==(
\ :_______________________________: /
\=+===============================+=/
TEST_DIR/__snapshots__/ #Where snapshot is persisted
TEST_FILENAME.EXT #Is module.exports = { 'NAME OFFSET': VAL }
#OFFSET:
# - automatically incremented by SNAP-SHOT-CORE()
# - is relative to NAME, not to TEST_PATH + NAME
STORAGE ==> #Is according to environment:
# - Node: stores on filesystem
# - Browser:
# - stores on localStorage
# - only one snapshot (no 'TEST_PATH' nor 'EXT')
# - Cypress:
# - stores on filesystem
# - only one snapshot (no 'TEST_PATH' nor 'EXT')
#Only Node is feature-complete, i.e. documented here
/=+===============================+=\
/ : : \
)==: SERIALIZER :==(
\ :_______________________________: /
\=+===============================+=/
SERIALIZATION ==> #By default SNAP-SHOT-CORE() and OPTS.compare uses JSON.
#To make it support non-JSON types (using jsesc, see its doc):
# - OPTS.what should be a FUNC()->VAL
# - OPTS.compare and OPTS.store must be customized
/=+===============================+=\
/ : : \
)==: MOCHA :==(
\ :_______________________________: /
\=+===============================+=/
SNAP-SHOT-IT ==> #Version 4.1.5
SNAP-SHOT-IT #Same as calling SNAP-SHOT-CORE with OPTS.what VAL except:
(['NAME', ][PROMISE_]VAL) # - uses Mocha-specific information and hooks to automatically set 'TEST_PATH', 'NAME' and 'OFFSET'
# - automatically remove snapshots from deleted tests
# - i.e. calls SNAP-SHOT-CORE.prune() with only the current assertions, providing they all passed
# - uses OPTS.compare SNAP-SHOT-COMPARE
#Also uses OPTS:
# - ext '.js'
# - opts.update|ci|show|dryRun: uses ENV.SNAPSHOT_UPDATE|CI|SNAPSHOT_SHOW|SNAPSHOT_DRY
SNAP-SHOT-IT(FUNC, VAL|ARR...) #Same but using [FUNC(VAL,...), FUNC(VAL2,...), ...]