index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. var _fs = require('fs');
  3. var _fs2 = _interopRequireDefault(_fs);
  4. var _path = require('path');
  5. var _path2 = _interopRequireDefault(_path);
  6. var _jestDiff = require('jest-diff');
  7. var _jestDiff2 = _interopRequireDefault(_jestDiff);
  8. var _jestMatcherUtils = require('jest-matcher-utils');
  9. var _State = require('./State');
  10. var _State2 = _interopRequireDefault(_State);
  11. var _plugins = require('./plugins');
  12. var _utils = require('./utils');
  13. var utils = _interopRequireWildcard(_utils);
  14. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. /**
  17. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  18. *
  19. * This source code is licensed under the MIT license found in the
  20. * LICENSE file in the root directory of this source tree.
  21. *
  22. *
  23. */
  24. const fileExists = (filePath, hasteFS) => hasteFS.exists(filePath) || _fs2.default.existsSync(filePath);
  25. const cleanup = (hasteFS, update) => {
  26. const pattern = '\\.' + utils.SNAPSHOT_EXTENSION + '$';
  27. const files = hasteFS.matchFiles(pattern);
  28. const filesRemoved = files.filter(snapshotFile => !fileExists(_path2.default.resolve(_path2.default.dirname(snapshotFile), '..', _path2.default.basename(snapshotFile, '.' + utils.SNAPSHOT_EXTENSION)), hasteFS)).map(snapshotFile => {
  29. if (update === 'all') {
  30. _fs2.default.unlinkSync(snapshotFile);
  31. }
  32. }).length;
  33. return {
  34. filesRemoved
  35. };
  36. };
  37. const toMatchSnapshot = function (received, testName) {
  38. this.dontThrow && this.dontThrow();
  39. const currentTestName = this.currentTestName,
  40. isNot = this.isNot,
  41. snapshotState = this.snapshotState;
  42. if (isNot) {
  43. throw new Error('Jest: `.not` cannot be used with `.toMatchSnapshot()`.');
  44. }
  45. if (!snapshotState) {
  46. throw new Error('Jest: snapshot state must be initialized.');
  47. }
  48. const result = snapshotState.match(testName && currentTestName ? `${currentTestName}: ${testName}` : currentTestName || '', received);
  49. const count = result.count,
  50. pass = result.pass;
  51. let actual = result.actual,
  52. expected = result.expected;
  53. let report;
  54. if (pass) {
  55. return { message: () => '', pass: true };
  56. } else if (!expected) {
  57. report = () => `New snapshot was ${(0, _jestMatcherUtils.RECEIVED_COLOR)('not written')}. The update flag ` + `must be explicitly passed to write a new snapshot.\n\n` + `This is likely because this test is run in a continuous integration ` + `(CI) environment in which snapshots are not written by default.\n\n` + `${(0, _jestMatcherUtils.RECEIVED_COLOR)('Received value')} ` + `${actual}`;
  58. } else {
  59. expected = (expected || '').trim();
  60. actual = (actual || '').trim();
  61. const diffMessage = (0, _jestDiff2.default)(expected, actual, {
  62. aAnnotation: 'Snapshot',
  63. bAnnotation: 'Received',
  64. expand: snapshotState.expand
  65. });
  66. report = () => `${(0, _jestMatcherUtils.RECEIVED_COLOR)('Received value')} does not match ` + `${(0, _jestMatcherUtils.EXPECTED_COLOR)('stored snapshot ' + count)}.\n\n` + (diffMessage || (0, _jestMatcherUtils.EXPECTED_COLOR)('- ' + (expected || '')) + '\n' + (0, _jestMatcherUtils.RECEIVED_COLOR)('+ ' + actual));
  67. }
  68. // Passing the the actual and expected objects so that a custom reporter
  69. // could access them, for example in order to display a custom visual diff,
  70. // or create a different error message
  71. return {
  72. actual,
  73. expected,
  74. message: () => (0, _jestMatcherUtils.matcherHint)('.toMatchSnapshot', 'value', '') + '\n\n' + report(),
  75. name: 'toMatchSnapshot',
  76. pass: false,
  77. report
  78. };
  79. };
  80. const toThrowErrorMatchingSnapshot = function (received, testName, fromPromise) {
  81. this.dontThrow && this.dontThrow();
  82. const isNot = this.isNot;
  83. if (isNot) {
  84. throw new Error('Jest: `.not` cannot be used with `.toThrowErrorMatchingSnapshot()`.');
  85. }
  86. let error;
  87. if (fromPromise) {
  88. error = received;
  89. } else {
  90. try {
  91. received();
  92. } catch (e) {
  93. error = e;
  94. }
  95. }
  96. if (error === undefined) {
  97. throw new Error((0, _jestMatcherUtils.matcherHint)('.toThrowErrorMatchingSnapshot', '() => {}', '') + '\n\n' + `Expected the function to throw an error.\n` + `But it didn't throw anything.`);
  98. }
  99. return toMatchSnapshot.call(this, error.message, testName);
  100. };
  101. module.exports = {
  102. EXTENSION: utils.SNAPSHOT_EXTENSION,
  103. SnapshotState: _State2.default,
  104. addSerializer: _plugins.addSerializer,
  105. cleanup,
  106. getSerializers: _plugins.getSerializers,
  107. toMatchSnapshot,
  108. toThrowErrorMatchingSnapshot,
  109. utils
  110. };