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