snapshot_resolver.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 = _interopRequireDefault(require('path'));
  7. var _chalk = _interopRequireDefault(require('chalk'));
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. /**
  12. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  13. *
  14. * This source code is licensed under the MIT license found in the
  15. * LICENSE file in the root directory of this source tree.
  16. */
  17. const EXTENSION = 'snap';
  18. exports.EXTENSION = EXTENSION;
  19. const DOT_EXTENSION = '.' + EXTENSION;
  20. exports.DOT_EXTENSION = DOT_EXTENSION;
  21. const isSnapshotPath = path => path.endsWith(DOT_EXTENSION);
  22. exports.isSnapshotPath = isSnapshotPath;
  23. const cache = new Map();
  24. const buildSnapshotResolver = config => {
  25. const key = config.rootDir;
  26. if (!cache.has(key)) {
  27. cache.set(key, createSnapshotResolver(config.snapshotResolver));
  28. }
  29. return cache.get(key);
  30. };
  31. exports.buildSnapshotResolver = buildSnapshotResolver;
  32. function createSnapshotResolver(snapshotResolverPath) {
  33. return typeof snapshotResolverPath === 'string'
  34. ? createCustomSnapshotResolver(snapshotResolverPath)
  35. : createDefaultSnapshotResolver();
  36. }
  37. function createDefaultSnapshotResolver() {
  38. return {
  39. resolveSnapshotPath: testPath =>
  40. _path.default.join(
  41. _path.default.join(_path.default.dirname(testPath), '__snapshots__'),
  42. _path.default.basename(testPath) + DOT_EXTENSION
  43. ),
  44. resolveTestPath: snapshotPath =>
  45. _path.default.resolve(
  46. _path.default.dirname(snapshotPath),
  47. '..',
  48. _path.default.basename(snapshotPath, DOT_EXTENSION)
  49. ),
  50. testPathForConsistencyCheck: _path.default.posix.join(
  51. 'consistency_check',
  52. '__tests__',
  53. 'example.test.js'
  54. )
  55. };
  56. }
  57. function createCustomSnapshotResolver(snapshotResolverPath) {
  58. const custom = require(snapshotResolverPath);
  59. const keys = [
  60. ['resolveSnapshotPath', 'function'],
  61. ['resolveTestPath', 'function'],
  62. ['testPathForConsistencyCheck', 'string']
  63. ];
  64. keys.forEach(([propName, requiredType]) => {
  65. if (typeof custom[propName] !== requiredType) {
  66. throw new TypeError(mustImplement(propName, requiredType));
  67. }
  68. });
  69. const customResolver = {
  70. resolveSnapshotPath: testPath =>
  71. custom.resolveSnapshotPath(testPath, DOT_EXTENSION),
  72. resolveTestPath: snapshotPath =>
  73. custom.resolveTestPath(snapshotPath, DOT_EXTENSION),
  74. testPathForConsistencyCheck: custom.testPathForConsistencyCheck
  75. };
  76. verifyConsistentTransformations(customResolver);
  77. return customResolver;
  78. }
  79. function mustImplement(propName, requiredType) {
  80. return (
  81. _chalk.default.bold(
  82. `Custom snapshot resolver must implement a \`${propName}\` as a ${requiredType}.`
  83. ) +
  84. '\nDocumentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver'
  85. );
  86. }
  87. function verifyConsistentTransformations(custom) {
  88. const resolvedSnapshotPath = custom.resolveSnapshotPath(
  89. custom.testPathForConsistencyCheck
  90. );
  91. const resolvedTestPath = custom.resolveTestPath(resolvedSnapshotPath);
  92. if (resolvedTestPath !== custom.testPathForConsistencyCheck) {
  93. throw new Error(
  94. _chalk.default.bold(
  95. `Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${custom.testPathForConsistencyCheck}')) === ${resolvedTestPath}`
  96. )
  97. );
  98. }
  99. }