diff options
Diffstat (limited to 'node_modules/jest-snapshot/build/State.js')
-rw-r--r-- | node_modules/jest-snapshot/build/State.js | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/node_modules/jest-snapshot/build/State.js b/node_modules/jest-snapshot/build/State.js new file mode 100644 index 000000000..156bb283d --- /dev/null +++ b/node_modules/jest-snapshot/build/State.js @@ -0,0 +1,176 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * + */ + +'use strict';var _require = + + + + + + + + + + + +require('./utils');const saveSnapshotFile = _require.saveSnapshotFile,getSnapshotData = _require.getSnapshotData,getSnapshotPath = _require.getSnapshotPath,keyToTestName = _require.keyToTestName,serialize = _require.serialize,testNameToKey = _require.testNameToKey,unescape = _require.unescape; +const fileExists = require('jest-file-exists'); +const fs = require('fs'); + +class SnapshotState { + + + + + + + + + + + + + + + + constructor( + testPath, + update, + snapshotPath, + expand) + { + this._snapshotPath = snapshotPath || getSnapshotPath(testPath);var _getSnapshotData = + getSnapshotData(this._snapshotPath, update);const data = _getSnapshotData.data,dirty = _getSnapshotData.dirty; + this._snapshotData = data; + this._dirty = dirty; + this._uncheckedKeys = new Set(Object.keys(this._snapshotData)); + this._counters = new Map(); + this._index = 0; + this.expand = expand || false; + this.added = 0; + this.matched = 0; + this.unmatched = 0; + this.update = update; + this.updated = 0; + this.skippedTests = new Set(); + this.failedTests = new Set(); + } + + markSnapshotsAsCheckedForTest(testName) { + this._uncheckedKeys.forEach(uncheckedKey => { + if (keyToTestName(uncheckedKey) === testName) { + this._uncheckedKeys.delete(uncheckedKey); + } + }); + } + + _addSnapshot(key, receivedSerialized) { + this._dirty = true; + this._snapshotData[key] = receivedSerialized; + } + + save(update) { + const status = { + deleted: false, + saved: false }; + + + const isEmpty = Object.keys(this._snapshotData).length === 0; + + if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) { + saveSnapshotFile(this._snapshotData, this._snapshotPath); + status.saved = true; + } else if (isEmpty && fileExists(this._snapshotPath)) { + if (update) { + fs.unlinkSync(this._snapshotPath); + } + status.deleted = true; + } + + return status; + } + + getUncheckedCount() { + return this._uncheckedKeys.size || 0; + } + + removeUncheckedKeys() { + if (this._uncheckedKeys.size) { + this._dirty = true; + this._uncheckedKeys.forEach(key => delete this._snapshotData[key]); + this._uncheckedKeys.clear(); + } + } + + match(testName, received, key) { + this._counters.set(testName, (this._counters.get(testName) || 0) + 1); + const count = Number(this._counters.get(testName)); + + if (!key) { + key = testNameToKey(testName, count); + } + + this._uncheckedKeys.delete(key); + + const receivedSerialized = serialize(received); + const expected = this._snapshotData[key]; + const pass = expected === receivedSerialized; + const hasSnapshot = this._snapshotData[key] !== undefined; + + if (pass) { + // Executing a snapshot file as JavaScript and writing the strings back + // when other snapshots have changed loses the proper escaping for some + // characters. Since we check every snapshot in every test, use the newly + // generated formatted string. + // Note that this is only relevant when a snapshot is added and the dirty + // flag is set. + this._snapshotData[key] = receivedSerialized; + } + + if ( + !fileExists(this._snapshotPath) || // there's no snapshot file + hasSnapshot && this.update || // there is a file, but we're updating + !hasSnapshot // there is a file, but it doesn't have this snaphsot + ) { + if (this.update) { + if (!pass) { + if (hasSnapshot) { + this.updated++; + } else { + this.added++; + } + this._addSnapshot(key, receivedSerialized); + } else { + this.matched++; + } + } else { + this._addSnapshot(key, receivedSerialized); + this.added++; + } + + return { pass: true }; + } else { + if (!pass) { + this.unmatched++; + return { + actual: unescape(receivedSerialized), + count, + expected: unescape(expected), + pass: false }; + + } else { + this.matched++; + return { pass: true }; + } + } + }} + + +module.exports = SnapshotState;
\ No newline at end of file |