Repository: jestjs/jest (TypeScript)

Issue:
[Bug]: mockImplementationOnce bleeds into withImplementation
### Version

29.4.2

### Steps to reproduce

1. Open [online repo](https://stackblitz.com/edit/node-7pqpfa?file=mock.test.js)
2. Run `npm install`
3. Run `npm test`

### Expected behavior

```js
 const mock = jest
    .fn(() => 'outside callback')
    .mockImplementationOnce(() => 'once');

  mock.withImplementation(
    () => 'inside callback',
    () => {
      expect(mock()).toBe('inside callback');
    }
  );

  expect(mock()).toBe('once');
  expect(mock()).toBe('outside callback');
```

### Actual behavior

```js
 const mock = jest
    .fn(() => 'outside callback')
    .mockImplementationOnce(() => 'once');

  mock.withImplementation(
    () => 'inside callback',
    () => {
       mock() // 'once'
    }
  );

  mock() // 'outside callback'
  mock() // 'outside callback'
```

### Additional context

I think it is a little counterintuitive when unused `implementationOnce` bleeds into `withImplementation`.

### Environment

```shell
System:
    OS: macOS 13.0.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    Yarn: 1.22.4 - ~/.yarn/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
  npmPackages:
    jest: ^29.4.2 => 29.4.2
```



Hints:

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
@@ -842,16 +842,20 @@ export class ModuleMocker {
         // Remember previous mock implementation, then set new one
         const mockConfig = this._ensureMockConfig(f);
         const previousImplementation = mockConfig.mockImpl;
+        const previousSpecificImplementations = mockConfig.specificMockImpls;
         mockConfig.mockImpl = fn;
+        mockConfig.specificMockImpls = [];
 
         const returnedValue = callback();
 
         if (isPromise(returnedValue)) {
           return returnedValue.then(() => {
             mockConfig.mockImpl = previousImplementation;
+            mockConfig.specificMockImpls = previousSpecificImplementations;
           });
         } else {
           mockConfig.mockImpl = previousImplementation;
+          mockConfig.specificMockImpls = previousSpecificImplementations;
         }
       }
 


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
@@ -1143,6 +1143,38 @@ describe('moduleMocker', () => {
 
       expect.assertions(4);
     });
+
+    it('mockImplementationOnce does not bleed into withImplementation', () => {
+      const mock = jest
+        .fn(() => 'outside callback')
+        .mockImplementationOnce(() => 'once');
+
+      mock.withImplementation(
+        () => 'inside callback',
+        () => {
+          expect(mock()).toBe('inside callback');
+        },
+      );
+
+      expect(mock()).toBe('once');
+      expect(mock()).toBe('outside callback');
+    });
+
+    it('mockReturnValueOnce does not bleed into withImplementation', () => {
+      const mock = jest
+        .fn(() => 'outside callback')
+        .mockReturnValueOnce('once');
+
+      mock.withImplementation(
+        () => 'inside callback',
+        () => {
+          expect(mock()).toBe('inside callback');
+        },
+      );
+
+      expect(mock()).toBe('once');
+      expect(mock()).toBe('outside callback');
+    });
   });
 
   test('mockReturnValue does not override mockImplementationOnce', () => {