-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjest_snapshots.javascript.txt
132 lines (107 loc) · 7.9 KB
/
jest_snapshots.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
┏━━━━━━━━━━━━━━━━━━━━┓
┃ JEST_SNAPSHOTS ┃
┗━━━━━━━━━━━━━━━━━━━━┛
ALTERNATIVES ==> # - jest-snapshots (preferred if not Mocha):
# - more high-profile
# - better with Jest|Mocha, but can be used without it
# - better serialization
# - prettier diff
# - snap-shot-it (preferred with Mocha):
# - better with Mocha, but can be used without it
# - ava snapshots (preferred with ava)
# - ava-specific
# - node-tap snapshots (preferred with node-tap)
# - node-tap-specific
# - minimalistic
# - serialization with UTIL-INSPECT()
# - Node.js snapshot():
# - core Node.js
# - minimalistic
# - serialization with UTIL-INSPECT()
# - Deno test snapshot (preferred with Deno):
# - core Deno
# - minimalistic
# - serialization with Deno.inspect()
VERSION ==> #Package within Jest monorepo (see its doc)
#Can be used outside Jest (see chai-jest-snapshot below)
SNAPSHOTS ==> #Diff testing on serialized values
┌────────────────┐
│ ASSERTIONS │
└────────────────┘
EXPECT.toMatchSnapshot #== to last time EXPECT.toMatchSnapshot() was run
([VAL, ]['NAME']) #Def NAME: 'TEST_NAME [NUM]'
# - NUM is when toMatchSnapshot() is called several times in current test
#If VAL, asserts EXPECT.toEqual(VAL)
# - useful with deep assertions, e.g. toMatchSnapshot({ VAR: expect.any(TYPE), ... })
# in which case only other properties will be checked against snapshot
EXPECT_FUNC.
toThrowErrorMatchingSnapshot() #Throw an error, and error == to last time EXPECT_FUNC.toThrowErrorMatchingSnapshot() was run
EXPECT.toMatchSnapshot([VAL])
EXPECT_FUNC. #Same but instead of storing snapshot in a separate file, re-write current test file
toThrowErrorMatchingSnapshot #and replace assertion argument.
(['ERROR']) #Prettier must be installed
┌───────────┐
│ FILES │
└───────────┘
./__snapshots__/FILE.snap #Contains serialized value as module.exports = { 'NAME [NUM]': SERIALIZED_VALUE }
#Should be committed to repository
#Created on first EXPECT.toMatchSnapshot()
# - unless jest --ci (should be done in CI)
#If during a test run, there is a difference, either:
# - if diff is not intended, fix bug
# - if diff is intended (e.g. new feature), update with jest --updateSnapshot
jest -u|--updateSnapshot #
jest --ci #
CONF.snapshotResolver #'MODULE' with:
# - resolveSnapshotPath('TEST_PATH', 'snap')->'SNAPSHOT_PATH'
# - resolveTestPath('SNAPSHOT_PATH', 'snap')->'TEST_PATH'
┌────────────────┐
│ SERIALIZER │
└────────────────┘
SERIALIZER #Adapter to serialize specific type of values
#Default ones: JavaScript core values, HTML ELEM, React RELEM, Immutable.js
#Can add custom ones (see below)
## - including jest-serializer-ansi-escapes: convert ANSI sequences to HTML-like <bold,...> tags
SERIALIZER.print(VAL, FUNC, NUM)
->STR #FUNC is the default serializer. NUM is indent
SERIALIZER.test(VAL)->BOOL #Check if this SERIALIZER should be used
CONF.snapshotSerializers #'MODULE'_ARR to additional SERIALIZERs
expect.addSnapshotSerializer
(SERIALIZER) #
CONF.snapshotFormat #OBJ passed as option to pretty-format
#Def: { escapeString: false }
jest-snapshot-utils #Internal utils to save files, retrieve snapshot data, etc.
#Not documented yet
┌──────────┐
│ DIFF │
└──────────┘
SNAPSHOT-DIFF ==> ##Version 0.3.0
##Snapshots of diffs, i.e. check that diff between values remains the same.
snapshotDiff(VAL, VAL2[, OPTS]) ##Returns pretty diff of VAL and VAL2
->STR ##Can be React RELEM (serialized with react-test-renderer)
##OPTS:
## - a|bAnnotation (def: 'First|second value'): prepended string
## - colors BOOL (def: false)
## - expand BOOL (def: false): expand the diff
## - contextLines NUM (def: 5): context lines around diffs
expect(VAL). ##Same as expect(snapshotDiff(VAL, VAL2[, OPTS])).toMatchSnapshot()
toMatchDiffSnapshot(VAL2[,OPTS])##Must first call expect.extend({ toMatchDiffSnapshot })
getSnapshotDiffSerializer() ##Custom SERIALIZER that removes extra sets of quotes around snapshot diffs (which are noisy)
┌──────────┐
│ CHAI │
└──────────┘
CHAI-JEST-SNAPSHOT ==> ##Version 2.0.0
##Use jest-snapshot but from Chai
##Re-use jest-snapshot library but does not require using Jest (although it can).
CHAI.use(CHAI-JEST-SNAPSHOT) ##
SHOULD.matchSnapshot
('FILENAME.snap', 'NAME') ##Like EXPECT.toMatchSnapshot(['NAME']), but using Chai
CHAI_JEST_SNAPSHOT_UPDATE_ALL= ##Similar to jest -u but without Jest
true ##Can also pass true as last argument to SHOULD.matchSnapshot()
CI=true ##Similar to jest --ci but without Jest
resetSnapshotRegistry() ##Must be called in each FILENAME before setTestName(), e.g. in before()
setFilename('FILENAME.snap') ##Sets default 'FILENAME.snap' used by toMatchSnapshot()
setTestName('NAME') ##Sets default 'NAME' used by toMatchSnapshot()
configureUsingMochaContext(this) ##To put in beforeEach|it()
##Automatically call setFilename|setTestName() using test information in `this`
addSerializer(...) ##Reference to jest-snapshot's expect.addSerializer()