Repository: jestjs/jest (TypeScript)

Issue:
[Bug]: 
### Version

27.5.1

### Steps to reproduce

1. Clone repo at https://github.com/kpeters-cbsi/jest-fails-to-mock-selenium-webdriver
2. `yarn install`
3. `yarn test`

### Expected behavior

I expect the test to complete successfully.

### Actual behavior

```
 FAIL  test/index.ts
  ● Test suite failed to run

    TypeError: name.startsWith is not a function

      at ModuleMocker._createMockFunction (node_modules/jest-mock/build/index.js:594:22)
          at Array.forEach (<anonymous>)
```


### Additional context

I did a bit of debugging on my own, and I think this is happening in [_createMockFunction](https://github.com/facebook/jest/blob/67c1aa20c5fec31366d733e901fee2b981cb1850/packages/jest-mock/src/index.ts#L810):

```
    if (name && name.startsWith(boundFunctionPrefix)) { <--- HERE
      do {
        name = name.substring(boundFunctionPrefix.length);
        // Call bind() just to alter the function name.
        bindCall = '.bind(null)';
      } while (name && name.startsWith(boundFunctionPrefix));
    }
```

Something in `selenium-webdriver` has a `name` property that's a function, rather than a string, which is causing that error. I wasn't able to figure out which class in `selenium-webdriver` was the culprit, however.

### Environment

```shell
System:
    OS: macOS 11.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.19.0 - ~/.nvm/versions/node/v14.19.0/bin/node
    Yarn: 3.2.0 - ~/.nvm/versions/node/v14.19.0/bin/yarn
    npm: 8.5.5 - ~/.nvm/versions/node/v14.19.0/bin/npm
  npmPackages:
    jest: ^27.5.1 => 27.5.1
```



Hints:
This is due to https://github.com/SeleniumHQ/selenium/blob/8b53734fd2bc97f7d084dc8326bf4b6c47b7052e/javascript/node/selenium-webdriver/lib/by.js#L205-L207 messing with our logic for gettting the name of a function here: https://github.com/facebook/jest/blob/1cce9a7dc194103576fad6c2086052efbea5ae77/packages/jest-mock/src/index.ts#L974-L975.

It looks weird in Node's REPL as well.

<img width="327" alt="image" src="https://user-images.githubusercontent.com/1404810/163550046-5dfbf788-617d-48f1-bdf6-64cdde310bda.png">

<img width="363" alt="image" src="https://user-images.githubusercontent.com/1404810/163550112-492e1392-3c68-455b-8867-6c3aeab4202a.png">

It is also an error in typescript: 
<img width="699" alt="image" src="https://user-images.githubusercontent.com/1404810/163550624-31b45e86-6191-4dbd-90ea-893e1d35f509.png">

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#function_names_in_classes

Patch:
diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts
--- a/packages/jest-mock/src/index.ts
+++ b/packages/jest-mock/src/index.ts
@@ -971,8 +971,12 @@ export class ModuleMocker {
       metadata.value = component;
       return metadata;
     } else if (type === 'function') {
-      // @ts-expect-error component is a function so it has a name
-      metadata.name = component.name;
+      // @ts-expect-error component is a function so it has a name, but not
+      // necessarily a string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#function_names_in_classes
+      const componentName = component.name;
+      if (typeof componentName === 'string') {
+        metadata.name = componentName;
+      }
       if (this.isMockFunction(component)) {
         metadata.mockImpl = component.getMockImplementation() as T;
       }


Test patch:
diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts
--- a/packages/jest-mock/src/__tests__/index.test.ts
+++ b/packages/jest-mock/src/__tests__/index.test.ts
@@ -30,6 +30,17 @@ describe('moduleMocker', () => {
       expect(metadata.name).toBe('x');
     });
 
+    it('does not return broken name property', () => {
+      class By {
+        static name() {
+          return 'this is not a name';
+        }
+      }
+      const metadata = moduleMocker.getMetadata(By);
+      expect(typeof By.name).toBe('function');
+      expect(metadata).not.toHaveProperty('name');
+    });
+
     it('mocks constant values', () => {
       const metadata = moduleMocker.getMetadata(Symbol.for('bowties.are.cool'));
       expect(metadata.value).toEqual(Symbol.for('bowties.are.cool'));