1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | const path = require('path')
|
5 | const debug = require('debug')('snap-shot-core')
|
6 | const verbose = require('debug')('snap-shot-core:verbose')
|
7 | const la = require('lazy-ass')
|
8 | const is = require('check-more-types')
|
9 | const mkdirp = require('mkdirp')
|
10 | const vm = require('vm')
|
11 | const escapeQuotes = require('escape-quotes')
|
12 | const pluralize = require('pluralize')
|
13 |
|
14 | const removeExtraNewLines = require('./utils').removeExtraNewLines
|
15 | const exportText = require('./utils').exportText
|
16 | const exportObject = require('./utils').exportObject
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | const cwd = process.cwd()
|
25 |
|
26 |
|
27 |
|
28 | const fromCurrentFolder = path.relative.bind(null, cwd)
|
29 | const snapshotsFolderName = '__snapshots__'
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | const joinSnapshotsFolder = path.join.bind(null, cwd, snapshotsFolderName)
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const snapshotsFolder = fromCurrentFolder(snapshotsFolderName)
|
44 | debug('process cwd: %s', cwd)
|
45 | debug('snapshots folder: %s', snapshotsFolder)
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | const resolveToCwd = path.resolve.bind(null, cwd)
|
56 |
|
57 | const isSaveOptions = is.schema({
|
58 | sortSnapshots: is.bool
|
59 | })
|
60 |
|
61 | const isLoadOptions = is.schema({
|
62 | useRelativePath: is.bool
|
63 | })
|
64 |
|
65 | function getSnapshotsFolder (specFile, opts = { useRelativePath: false }) {
|
66 | if (!opts.useRelativePath) {
|
67 |
|
68 | return snapshotsFolder
|
69 | }
|
70 |
|
71 | const relativeDir = fromCurrentFolder(path.dirname(specFile))
|
72 | verbose('relative path to spec file %s is %s', specFile, relativeDir)
|
73 |
|
74 |
|
75 | const folder = joinSnapshotsFolder(relativeDir)
|
76 | verbose('snapshot folder %s', folder)
|
77 |
|
78 | return folder
|
79 | }
|
80 |
|
81 | function loadSnaps (snapshotPath) {
|
82 | const full = require.resolve(snapshotPath)
|
83 | if (!fs.existsSync(snapshotPath)) {
|
84 | return {}
|
85 | }
|
86 |
|
87 | const sandbox = {
|
88 | exports: {}
|
89 | }
|
90 | const source = fs.readFileSync(full, 'utf8')
|
91 | try {
|
92 | vm.runInNewContext(source, sandbox)
|
93 | return removeExtraNewLines(sandbox.exports)
|
94 | } catch (e) {
|
95 | console.error('Could not load file', full)
|
96 | console.error(source)
|
97 | console.error(e)
|
98 | if (e instanceof SyntaxError) {
|
99 | throw e
|
100 | }
|
101 | return {}
|
102 | }
|
103 | }
|
104 |
|
105 | function fileForSpec (specFile, ext, opts = { useRelativePath: false }) {
|
106 | la(is.unemptyString(specFile), 'missing spec file', specFile)
|
107 | la(is.maybe.string(ext), 'invalid extension to find', ext)
|
108 | la(isLoadOptions(opts), 'expected fileForSpec options', opts)
|
109 |
|
110 | const specName = path.basename(specFile)
|
111 | la(
|
112 | is.unemptyString(specName),
|
113 | 'could not get spec name from spec file',
|
114 | specFile
|
115 | )
|
116 |
|
117 | const snapshotFolder = getSnapshotsFolder(specFile, opts)
|
118 |
|
119 | verbose(
|
120 | 'spec file "%s" has name "%s" and snapshot folder %s',
|
121 | specFile,
|
122 | specName,
|
123 | snapshotFolder
|
124 | )
|
125 |
|
126 | let filename = path.join(snapshotFolder, specName)
|
127 | if (ext) {
|
128 | if (!filename.endsWith(ext)) {
|
129 | filename += ext
|
130 | }
|
131 | }
|
132 | verbose('formed filename %s', filename)
|
133 | const fullName = resolveToCwd(filename)
|
134 | verbose('full resolved name %s', fullName)
|
135 |
|
136 | return fullName
|
137 | }
|
138 |
|
139 | function loadSnapshotsFrom (filename) {
|
140 | la(is.unemptyString(filename), 'missing snapshots filename', filename)
|
141 |
|
142 | debug('loading snapshots from %s', filename)
|
143 | let snapshots = {}
|
144 | if (fs.existsSync(filename)) {
|
145 | snapshots = loadSnaps(filename)
|
146 | } else {
|
147 | debug('could not find snapshots file %s', filename)
|
148 | }
|
149 | return snapshots
|
150 | }
|
151 |
|
152 | function loadSnapshots (specFile, ext, opts = { useRelativePath: false }) {
|
153 | la(is.unemptyString(specFile), 'missing specFile name', specFile)
|
154 | la(isLoadOptions(opts), 'expected loadSnapshots options', opts)
|
155 |
|
156 | const filename = fileForSpec(specFile, ext, opts)
|
157 | verbose('from spec %s got snap filename %s', specFile, filename)
|
158 | return loadSnapshotsFrom(filename)
|
159 | }
|
160 |
|
161 | function prepareFragments (snapshots, opts = { sortSnapshots: true }) {
|
162 | la(isSaveOptions(opts), 'expected prepare fragments options', opts)
|
163 |
|
164 | const keys = Object.keys(snapshots)
|
165 | debug(
|
166 | 'prepare %s, sorted? %d',
|
167 | pluralize('snapshot', keys.length, true),
|
168 | opts.sortSnapshots
|
169 | )
|
170 | const names = opts.sortSnapshots ? keys.sort() : keys
|
171 |
|
172 | const fragments = names.map(testName => {
|
173 | debug(`snapshot fragment name "${testName}"`)
|
174 | const value = snapshots[testName]
|
175 | const escapedName = escapeQuotes(testName)
|
176 | return is.string(value)
|
177 | ? exportText(escapedName, value)
|
178 | : exportObject(escapedName, value)
|
179 | })
|
180 |
|
181 | return fragments
|
182 | }
|
183 |
|
184 | function maybeSortAndSave (snapshots, filename, opts = { sortSnapshots: true }) {
|
185 | const fragments = prepareFragments(snapshots, opts)
|
186 | debug('have %s', pluralize('fragment', fragments.length, true))
|
187 |
|
188 | const s = fragments.join('\n')
|
189 | fs.writeFileSync(filename, s, 'utf8')
|
190 | return s
|
191 | }
|
192 |
|
193 |
|
194 | function saveSnapshots (
|
195 | specFile,
|
196 | snapshots,
|
197 | ext,
|
198 | opts = { sortSnapshots: true, useRelativePath: false }
|
199 | ) {
|
200 | la(
|
201 | isSaveOptions(opts) && isLoadOptions(opts),
|
202 | 'expected save snapshots options',
|
203 | opts
|
204 | )
|
205 |
|
206 | const snapshotsFolder = getSnapshotsFolder(specFile, opts)
|
207 | debug('for spec file %s', specFile)
|
208 | debug('making folder "%s" for snapshot if does not exist', snapshotsFolder)
|
209 |
|
210 | mkdirp.sync(snapshotsFolder)
|
211 | const filename = fileForSpec(specFile, ext, opts)
|
212 | const specRelativeName = fromCurrentFolder(specFile)
|
213 | debug('saving snapshots into %s for %s', filename, specRelativeName)
|
214 | debug('snapshots are')
|
215 | debug(snapshots)
|
216 | debug('saveSnapshots options %o', opts)
|
217 |
|
218 | return maybeSortAndSave(snapshots, filename, opts)
|
219 | }
|
220 |
|
221 | const isValidCompareResult = is.schema({
|
222 | orElse: is.fn
|
223 | })
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | function raiseIfDifferent (options) {
|
232 | options = options || {}
|
233 |
|
234 | const value = options.value
|
235 | const expected = options.expected
|
236 | const specName = options.specName
|
237 | const compare = options.compare
|
238 |
|
239 | la(value, 'missing value to compare', value)
|
240 | la(expected, 'missing expected value', expected)
|
241 | la(is.unemptyString(specName), 'missing spec name', specName)
|
242 |
|
243 | const result = compare({ expected, value })
|
244 | la(
|
245 | isValidCompareResult(result),
|
246 | 'invalid compare result',
|
247 | result,
|
248 | 'when comparing value\n',
|
249 | value,
|
250 | 'with expected\n',
|
251 | expected
|
252 | )
|
253 |
|
254 | result.orElse(message => {
|
255 | debug('Test "%s" snapshot difference', specName)
|
256 | la(is.unemptyString(message), 'missing err string', message)
|
257 |
|
258 | const fullMessage = `Different value of snapshot "${specName}"\n${message}`
|
259 |
|
260 |
|
261 | console.error(fullMessage)
|
262 |
|
263 | throw new Error(fullMessage)
|
264 | })
|
265 | }
|
266 |
|
267 | module.exports = {
|
268 | readFileSync: fs.readFileSync,
|
269 | fromCurrentFolder,
|
270 | loadSnapshots,
|
271 | loadSnapshotsFrom,
|
272 | saveSnapshots,
|
273 | maybeSortAndSave,
|
274 | raiseIfDifferent,
|
275 | fileForSpec,
|
276 | exportText,
|
277 | prepareFragments,
|
278 | joinSnapshotsFolder,
|
279 | snapshotsFolderName
|
280 | }
|