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

Code before:
        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 (
          typeof returnedValue === 'object' &&
          returnedValue !== null &&
          typeof returnedValue.then === 'function'
        ) {
          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;
        });

      f.mockName = (name: string) => {
        if (name) {
          const mockConfig = this._ensureMockConfig(f);

Review comment (suggestion):
```suggestion
          returnedValue != null &&
          typeof returnedValue === 'object' &&
```

probably doesn't matter, but easier to bail out early

Code after:
        // 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;
        });

      f.mockName = (name: string) => {
        if (name) {
          const mockConfig = this._ensureMockConfig(f);
          mockConfig.mockName = name;
        }