State.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. 'use strict';
  2. var global = (function () {
  3. if (typeof globalThis !== 'undefined') {
  4. return globalThis;
  5. } else if (typeof global !== 'undefined') {
  6. return global;
  7. } else if (typeof self !== 'undefined') {
  8. return self;
  9. } else if (typeof window !== 'undefined') {
  10. return window;
  11. } else {
  12. return Function('return this')();
  13. }
  14. })();
  15. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  16. Object.defineProperty(exports, '__esModule', {
  17. value: true
  18. });
  19. exports.default = void 0;
  20. var fs = _interopRequireWildcard(require('graceful-fs'));
  21. var _jestMessageUtil = require('jest-message-util');
  22. var _InlineSnapshots = require('./InlineSnapshots');
  23. var _utils = require('./utils');
  24. function _getRequireWildcardCache(nodeInterop) {
  25. if (typeof WeakMap !== 'function') return null;
  26. var cacheBabelInterop = new WeakMap();
  27. var cacheNodeInterop = new WeakMap();
  28. return (_getRequireWildcardCache = function (nodeInterop) {
  29. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  30. })(nodeInterop);
  31. }
  32. function _interopRequireWildcard(obj, nodeInterop) {
  33. if (!nodeInterop && obj && obj.__esModule) {
  34. return obj;
  35. }
  36. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  37. return {default: obj};
  38. }
  39. var cache = _getRequireWildcardCache(nodeInterop);
  40. if (cache && cache.has(obj)) {
  41. return cache.get(obj);
  42. }
  43. var newObj = {};
  44. var hasPropertyDescriptor =
  45. Object.defineProperty && Object.getOwnPropertyDescriptor;
  46. for (var key in obj) {
  47. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  48. var desc = hasPropertyDescriptor
  49. ? Object.getOwnPropertyDescriptor(obj, key)
  50. : null;
  51. if (desc && (desc.get || desc.set)) {
  52. Object.defineProperty(newObj, key, desc);
  53. } else {
  54. newObj[key] = obj[key];
  55. }
  56. }
  57. }
  58. newObj.default = obj;
  59. if (cache) {
  60. cache.set(obj, newObj);
  61. }
  62. return newObj;
  63. }
  64. var global = (function () {
  65. if (typeof globalThis !== 'undefined') {
  66. return globalThis;
  67. } else if (typeof global !== 'undefined') {
  68. return global;
  69. } else if (typeof self !== 'undefined') {
  70. return self;
  71. } else if (typeof window !== 'undefined') {
  72. return window;
  73. } else {
  74. return Function('return this')();
  75. }
  76. })();
  77. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  78. var global = (function () {
  79. if (typeof globalThis !== 'undefined') {
  80. return globalThis;
  81. } else if (typeof global !== 'undefined') {
  82. return global;
  83. } else if (typeof self !== 'undefined') {
  84. return self;
  85. } else if (typeof window !== 'undefined') {
  86. return window;
  87. } else {
  88. return Function('return this')();
  89. }
  90. })();
  91. var jestExistsFile =
  92. global[Symbol.for('jest-native-exists-file')] || fs.existsSync;
  93. function _defineProperty(obj, key, value) {
  94. if (key in obj) {
  95. Object.defineProperty(obj, key, {
  96. value: value,
  97. enumerable: true,
  98. configurable: true,
  99. writable: true
  100. });
  101. } else {
  102. obj[key] = value;
  103. }
  104. return obj;
  105. }
  106. class SnapshotState {
  107. // @ts-expect-error
  108. constructor(snapshotPath, options) {
  109. _defineProperty(this, '_counters', void 0);
  110. _defineProperty(this, '_dirty', void 0);
  111. _defineProperty(this, '_index', void 0);
  112. _defineProperty(this, '_updateSnapshot', void 0);
  113. _defineProperty(this, '_snapshotData', void 0);
  114. _defineProperty(this, '_initialData', void 0);
  115. _defineProperty(this, '_snapshotPath', void 0);
  116. _defineProperty(this, '_inlineSnapshots', void 0);
  117. _defineProperty(this, '_uncheckedKeys', void 0);
  118. _defineProperty(this, '_prettierPath', void 0);
  119. _defineProperty(this, '_snapshotFormat', void 0);
  120. _defineProperty(this, 'added', void 0);
  121. _defineProperty(this, 'expand', void 0);
  122. _defineProperty(this, 'matched', void 0);
  123. _defineProperty(this, 'unmatched', void 0);
  124. _defineProperty(this, 'updated', void 0);
  125. this._snapshotPath = snapshotPath;
  126. const {data, dirty} = (0, _utils.getSnapshotData)(
  127. this._snapshotPath,
  128. options.updateSnapshot
  129. );
  130. this._initialData = data;
  131. this._snapshotData = data;
  132. this._dirty = dirty;
  133. this._prettierPath = options.prettierPath;
  134. this._inlineSnapshots = [];
  135. this._uncheckedKeys = new Set(Object.keys(this._snapshotData));
  136. this._counters = new Map();
  137. this._index = 0;
  138. this.expand = options.expand || false;
  139. this.added = 0;
  140. this.matched = 0;
  141. this.unmatched = 0;
  142. this._updateSnapshot = options.updateSnapshot;
  143. this.updated = 0;
  144. this._snapshotFormat = options.snapshotFormat;
  145. }
  146. markSnapshotsAsCheckedForTest(testName) {
  147. this._uncheckedKeys.forEach(uncheckedKey => {
  148. if ((0, _utils.keyToTestName)(uncheckedKey) === testName) {
  149. this._uncheckedKeys.delete(uncheckedKey);
  150. }
  151. });
  152. }
  153. _addSnapshot(key, receivedSerialized, options) {
  154. this._dirty = true;
  155. if (options.isInline) {
  156. const error = options.error || new Error();
  157. const lines = (0, _jestMessageUtil.getStackTraceLines)(
  158. (0, _utils.removeLinesBeforeExternalMatcherTrap)(error.stack || '')
  159. );
  160. const frame = (0, _jestMessageUtil.getTopFrame)(lines);
  161. if (!frame) {
  162. throw new Error(
  163. "Jest: Couldn't infer stack frame for inline snapshot."
  164. );
  165. }
  166. this._inlineSnapshots.push({
  167. frame,
  168. snapshot: receivedSerialized
  169. });
  170. } else {
  171. this._snapshotData[key] = receivedSerialized;
  172. }
  173. }
  174. clear() {
  175. this._snapshotData = this._initialData;
  176. this._inlineSnapshots = [];
  177. this._counters = new Map();
  178. this._index = 0;
  179. this.added = 0;
  180. this.matched = 0;
  181. this.unmatched = 0;
  182. this.updated = 0;
  183. }
  184. save() {
  185. const hasExternalSnapshots = Object.keys(this._snapshotData).length;
  186. const hasInlineSnapshots = this._inlineSnapshots.length;
  187. const isEmpty = !hasExternalSnapshots && !hasInlineSnapshots;
  188. const status = {
  189. deleted: false,
  190. saved: false
  191. };
  192. if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) {
  193. if (hasExternalSnapshots) {
  194. (0, _utils.saveSnapshotFile)(this._snapshotData, this._snapshotPath);
  195. }
  196. if (hasInlineSnapshots) {
  197. (0, _InlineSnapshots.saveInlineSnapshots)(
  198. this._inlineSnapshots,
  199. this._prettierPath
  200. );
  201. }
  202. status.saved = true;
  203. } else if (!hasExternalSnapshots && jestExistsFile(this._snapshotPath)) {
  204. if (this._updateSnapshot === 'all') {
  205. fs.unlinkSync(this._snapshotPath);
  206. }
  207. status.deleted = true;
  208. }
  209. return status;
  210. }
  211. getUncheckedCount() {
  212. return this._uncheckedKeys.size || 0;
  213. }
  214. getUncheckedKeys() {
  215. return Array.from(this._uncheckedKeys);
  216. }
  217. removeUncheckedKeys() {
  218. if (this._updateSnapshot === 'all' && this._uncheckedKeys.size) {
  219. this._dirty = true;
  220. this._uncheckedKeys.forEach(key => delete this._snapshotData[key]);
  221. this._uncheckedKeys.clear();
  222. }
  223. }
  224. match({testName, received, key, inlineSnapshot, isInline, error}) {
  225. this._counters.set(testName, (this._counters.get(testName) || 0) + 1);
  226. const count = Number(this._counters.get(testName));
  227. if (!key) {
  228. key = (0, _utils.testNameToKey)(testName, count);
  229. } // Do not mark the snapshot as "checked" if the snapshot is inline and
  230. // there's an external snapshot. This way the external snapshot can be
  231. // removed with `--updateSnapshot`.
  232. if (!(isInline && this._snapshotData[key] !== undefined)) {
  233. this._uncheckedKeys.delete(key);
  234. }
  235. const receivedSerialized = (0, _utils.addExtraLineBreaks)(
  236. (0, _utils.serialize)(received, undefined, this._snapshotFormat)
  237. );
  238. const expected = isInline ? inlineSnapshot : this._snapshotData[key];
  239. const pass = expected === receivedSerialized;
  240. const hasSnapshot = expected !== undefined;
  241. const snapshotIsPersisted = isInline || fs.existsSync(this._snapshotPath);
  242. if (pass && !isInline) {
  243. // Executing a snapshot file as JavaScript and writing the strings back
  244. // when other snapshots have changed loses the proper escaping for some
  245. // characters. Since we check every snapshot in every test, use the newly
  246. // generated formatted string.
  247. // Note that this is only relevant when a snapshot is added and the dirty
  248. // flag is set.
  249. this._snapshotData[key] = receivedSerialized;
  250. } // These are the conditions on when to write snapshots:
  251. // * There's no snapshot file in a non-CI environment.
  252. // * There is a snapshot file and we decided to update the snapshot.
  253. // * There is a snapshot file, but it doesn't have this snaphsot.
  254. // These are the conditions on when not to write snapshots:
  255. // * The update flag is set to 'none'.
  256. // * There's no snapshot file or a file without this snapshot on a CI environment.
  257. if (
  258. (hasSnapshot && this._updateSnapshot === 'all') ||
  259. ((!hasSnapshot || !snapshotIsPersisted) &&
  260. (this._updateSnapshot === 'new' || this._updateSnapshot === 'all'))
  261. ) {
  262. if (this._updateSnapshot === 'all') {
  263. if (!pass) {
  264. if (hasSnapshot) {
  265. this.updated++;
  266. } else {
  267. this.added++;
  268. }
  269. this._addSnapshot(key, receivedSerialized, {
  270. error,
  271. isInline
  272. });
  273. } else {
  274. this.matched++;
  275. }
  276. } else {
  277. this._addSnapshot(key, receivedSerialized, {
  278. error,
  279. isInline
  280. });
  281. this.added++;
  282. }
  283. return {
  284. actual: '',
  285. count,
  286. expected: '',
  287. key,
  288. pass: true
  289. };
  290. } else {
  291. if (!pass) {
  292. this.unmatched++;
  293. return {
  294. actual: (0, _utils.removeExtraLineBreaks)(receivedSerialized),
  295. count,
  296. expected:
  297. expected !== undefined
  298. ? (0, _utils.removeExtraLineBreaks)(expected)
  299. : undefined,
  300. key,
  301. pass: false
  302. };
  303. } else {
  304. this.matched++;
  305. return {
  306. actual: '',
  307. count,
  308. expected: '',
  309. key,
  310. pass: true
  311. };
  312. }
  313. }
  314. }
  315. fail(testName, _received, key) {
  316. this._counters.set(testName, (this._counters.get(testName) || 0) + 1);
  317. const count = Number(this._counters.get(testName));
  318. if (!key) {
  319. key = (0, _utils.testNameToKey)(testName, count);
  320. }
  321. this._uncheckedKeys.delete(key);
  322. this.unmatched++;
  323. return key;
  324. }
  325. }
  326. exports.default = SnapshotState;