SnapshotResolver.js 5.1 KB

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