Repository: facebook/jest
File: packages/jest-mock/src/index.ts (TypeScript)

Code before:

      f.mockReturnValue = (value: ReturnType<T>) =>
        // next function call will return specified return value or this one
        f.mockImplementation(() => value);

      f.mockResolvedValue = (value: ResolveType<T>) =>
        f.mockImplementation(() => Promise.resolve(value));

      f.mockRejectedValue = (value: unknown) =>
        f.mockImplementation(() => Promise.reject(value));

      f.mockImplementationOnce = (fn: UnknownFunction) => {
        // next function call will use this mock implementation return value
        // or default mock implementation return value
        const mockConfig = this._ensureMockConfig(f);
        mockConfig.specificMockImpls.push(fn);
        return f;
      };

      f.withImplementation = <R extends WithImplementationCallbackReturn>(
        fn: UnknownFunction,
        callback: () => R,
        // @ts-expect-error: Type guards are not advanced enough for this use case
      ): R extends WithImplementationAsyncCallbackReturn
        ? Promise<void>
        : undefined => {
        // Remember previous mock implementation, then set new one
        const mockConfig = this._ensureMockConfig(f);
        const previousImplementation = mockConfig.mockImpl;
        mockConfig.mockImpl = fn;

        const returnedValue = callback();

        if (
          typeof returnedValue === 'object' &&
          returnedValue !== null &&
          typeof returnedValue.then === 'function'
        ) {
          // @ts-expect-error: Type guards are not advanced enough for this use case
          return returnedValue.then(() => {
            mockConfig.mockImpl = previousImplementation;
          });
        } else {
          mockConfig.mockImpl = previousImplementation;
        }
      };

      f.mockImplementation = (fn: UnknownFunction) => {
        // next function call will use mock implementation return value
        const mockConfig = this._ensureMockConfig(f);
        mockConfig.mockImpl = fn;

Review comment (suggestion):
Oh.. Forgot to add this part as well (just move `function withImplementation` to some better place or there is no need?): 

```suggestion
      f.withImplementation = withImplementation.bind(this);

      function withImplementation(fn: T, callback: () => void): void;
      function withImplementation(
        fn: T,
        callback: () => Promise<unknown>,
      ): Promise<void>;
      function withImplementation(
        this: ModuleMocker,
        fn: T,
        callback: (() => void) | (() => Promise<unknown>),
      ): void | Promise<void> {
```

Code after:
      f.mockRejectedValue = (value: unknown) =>
        f.mockImplementation(() => Promise.reject(value));

      f.mockImplementationOnce = (fn: UnknownFunction) => {
        // next function call will use this mock implementation return value
        // or default mock implementation return value
        const mockConfig = this._ensureMockConfig(f);
        mockConfig.specificMockImpls.push(fn);
        return f;
      };

      f.withImplementation = withImplementation.bind(this);

      function withImplementation(fn: T, callback: () => void): void;
      function withImplementation(
        fn: T,
        callback: () => Promise<unknown>,
      ): Promise<void>;
      function withImplementation(
        this: ModuleMocker,
        fn: T,
        callback: (() => void) | (() => Promise<unknown>),
      ): void | Promise<void> {
        // Remember previous mock implementation, then set new one
        const mockConfig = this._ensureMockConfig(f);
        const previousImplementation = mockConfig.mockImpl;
        mockConfig.mockImpl = fn;

        const returnedValue = callback();

        if (isPromise(returnedValue)) {
          return returnedValue.then(() => {
            mockConfig.mockImpl = previousImplementation;
          });
        } else {
          mockConfig.mockImpl = previousImplementation;
        }
      }

      f.mockImplementation = (fn: UnknownFunction) => {
        // next function call will use mock implementation return value
        const mockConfig = this._ensureMockConfig(f);
        mockConfig.mockImpl = fn;
        return f;
      };

      f.mockReturnThis = () =>
        f.mockImplementation(function (this: ReturnType<T>) {
          return this;
        });