SnapshotResolver.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.buildSnapshotResolver = exports.isSnapshotPath = exports.DOT_EXTENSION = exports.EXTENSION = void 0;
  6. var path = _interopRequireWildcard(require('path'));
  7. var _chalk = _interopRequireDefault(require('chalk'));
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. function _getRequireWildcardCache() {
  12. if (typeof WeakMap !== 'function') return null;
  13. var cache = new WeakMap();
  14. _getRequireWildcardCache = function () {
  15. return cache;
  16. };
  17. return cache;
  18. }
  19. function _interopRequireWildcard(obj) {
  20. if (obj && obj.__esModule) {
  21. return obj;
  22. }
  23. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  24. return {default: obj};
  25. }
  26. var cache = _getRequireWildcardCache();
  27. if (cache && cache.has(obj)) {
  28. return cache.get(obj);
  29. }
  30. var newObj = {};
  31. var hasPropertyDescriptor =
  32. Object.defineProperty && Object.getOwnPropertyDescriptor;
  33. for (var key in obj) {
  34. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  35. var desc = hasPropertyDescriptor
  36. ? Object.getOwnPropertyDescriptor(obj, key)
  37. : null;
  38. if (desc && (desc.get || desc.set)) {
  39. Object.defineProperty(newObj, key, desc);
  40. } else {
  41. newObj[key] = obj[key];
  42. }
  43. }
  44. }
  45. newObj.default = obj;
  46. if (cache) {
  47. cache.set(obj, newObj);
  48. }
  49. return newObj;
  50. }
  51. /**
  52. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  53. *
  54. * This source code is licensed under the MIT license found in the
  55. * LICENSE file in the root directory of this source tree.
  56. */
  57. const EXTENSION = 'snap';
  58. exports.EXTENSION = EXTENSION;
  59. const DOT_EXTENSION = '.' + EXTENSION;
  60. exports.DOT_EXTENSION = DOT_EXTENSION;
  61. const isSnapshotPath = path => path.endsWith(DOT_EXTENSION);
  62. exports.isSnapshotPath = isSnapshotPath;
  63. const cache = new Map();
  64. const buildSnapshotResolver = config => {
  65. const key = config.rootDir;
  66. if (!cache.has(key)) {
  67. cache.set(key, createSnapshotResolver(config.snapshotResolver));
  68. }
  69. return cache.get(key);
  70. };
  71. exports.buildSnapshotResolver = buildSnapshotResolver;
  72. function createSnapshotResolver(snapshotResolverPath) {
  73. return typeof snapshotResolverPath === 'string'
  74. ? createCustomSnapshotResolver(snapshotResolverPath)
  75. : createDefaultSnapshotResolver();
  76. }
  77. function createDefaultSnapshotResolver() {
  78. return {
  79. resolveSnapshotPath: testPath =>
  80. path.join(
  81. path.join(path.dirname(testPath), '__snapshots__'),
  82. path.basename(testPath) + DOT_EXTENSION
  83. ),
  84. resolveTestPath: snapshotPath =>
  85. path.resolve(
  86. path.dirname(snapshotPath),
  87. '..',
  88. path.basename(snapshotPath, DOT_EXTENSION)
  89. ),
  90. testPathForConsistencyCheck: path.posix.join(
  91. 'consistency_check',
  92. '__tests__',
  93. 'example.test.js'
  94. )
  95. };
  96. }
  97. function createCustomSnapshotResolver(snapshotResolverPath) {
  98. const custom = require(snapshotResolverPath);
  99. const keys = [
  100. ['resolveSnapshotPath', 'function'],
  101. ['resolveTestPath', 'function'],
  102. ['testPathForConsistencyCheck', 'string']
  103. ];
  104. keys.forEach(([propName, requiredType]) => {
  105. if (typeof custom[propName] !== requiredType) {
  106. throw new TypeError(mustImplement(propName, requiredType));
  107. }
  108. });
  109. const customResolver = {
  110. resolveSnapshotPath: testPath =>
  111. custom.resolveSnapshotPath(testPath, DOT_EXTENSION),
  112. resolveTestPath: snapshotPath =>
  113. custom.resolveTestPath(snapshotPath, DOT_EXTENSION),
  114. testPathForConsistencyCheck: custom.testPathForConsistencyCheck
  115. };
  116. verifyConsistentTransformations(customResolver);
  117. return customResolver;
  118. }
  119. function mustImplement(propName, requiredType) {
  120. return (
  121. _chalk.default.bold(
  122. `Custom snapshot resolver must implement a \`${propName}\` as a ${requiredType}.`
  123. ) +
  124. '\nDocumentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver'
  125. );
  126. }
  127. function verifyConsistentTransformations(custom) {
  128. const resolvedSnapshotPath = custom.resolveSnapshotPath(
  129. custom.testPathForConsistencyCheck
  130. );
  131. const resolvedTestPath = custom.resolveTestPath(resolvedSnapshotPath);
  132. if (resolvedTestPath !== custom.testPathForConsistencyCheck) {
  133. throw new Error(
  134. _chalk.default.bold(
  135. `Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${custom.testPathForConsistencyCheck}')) === ${resolvedTestPath}`
  136. )
  137. );
  138. }
  139. }