convert_descriptor_to_string.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = convertDescriptorToString;
  6. /**
  7. * Copyright (c) 2017-present, Facebook, Inc. 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. *
  13. */
  14. // See: https://github.com/facebook/jest/pull/5154
  15. function convertDescriptorToString(descriptor) {
  16. if (
  17. typeof descriptor === 'string' ||
  18. typeof descriptor === 'number' ||
  19. descriptor === undefined
  20. ) {
  21. return descriptor;
  22. }
  23. if (typeof descriptor !== 'function') {
  24. throw new Error('describe expects a class, function, number, or string.');
  25. }
  26. if (descriptor.name !== undefined) {
  27. return descriptor.name;
  28. }
  29. // Fallback for old browsers, pardon Flow
  30. const stringified = descriptor.toString();
  31. const typeDescriptorMatch = stringified.match(/class|function/);
  32. const indexOfNameSpace =
  33. // $FlowFixMe
  34. typeDescriptorMatch.index + typeDescriptorMatch[0].length;
  35. // $FlowFixMe
  36. const indexOfNameAfterSpace = stringified.search(/\(|\{/, indexOfNameSpace);
  37. const name = stringified.substring(indexOfNameSpace, indexOfNameAfterSpace);
  38. return name.trim();
  39. }