convertDescriptorToString.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = convertDescriptorToString;
  6. /**
  7. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. */
  12. // See: https://github.com/facebook/jest/pull/5154
  13. function convertDescriptorToString(descriptor) {
  14. if (
  15. typeof descriptor === 'string' ||
  16. typeof descriptor === 'number' ||
  17. descriptor === undefined
  18. ) {
  19. return descriptor;
  20. }
  21. if (typeof descriptor !== 'function') {
  22. throw new Error('describe expects a class, function, number, or string.');
  23. }
  24. if (descriptor.name !== undefined) {
  25. return descriptor.name;
  26. } // Fallback for old browsers, pardon Flow
  27. const stringified = descriptor.toString();
  28. const typeDescriptorMatch = stringified.match(/class|function/);
  29. const indexOfNameSpace = // @ts-ignore: typeDescriptorMatch exists
  30. typeDescriptorMatch.index + typeDescriptorMatch[0].length;
  31. const indexOfNameAfterSpace = stringified.search(/\(|\{/);
  32. const name = stringified.substring(indexOfNameSpace, indexOfNameAfterSpace);
  33. return name.trim();
  34. }