title: TypeError: updatedService.save is not a function when using testing NestJS API with Jests and Mongoose
body: <p>Depending on the user input DTO, A product's information can be Updated using <code>HTTP PATCH</code>. My NestJS Service is as follows:</p>
  <pre class="lang-js prettyprint-override"><code>
  async updateAProduct(product: ProductDTO ) {
          const updatedProduct = await this.findProduct(product.id);
          if (product.title) {
              updatedProduct.title = product.title;
          }
          if (product.description) {
              updatedProduct.description = product.description;
          }
          if (product.price) {
              updatedProduct.price = product.price;
          }
          updatedProduct.save()
      }
  
  </code></pre>
  <p>Where <code>ProductDTO</code> is an interface:</p>
  <pre class="lang-js prettyprint-override"><code>export interface ProductDTO {
      id?: string;
      title?: string;
      description?: string;
      price?: number;
  }
  </code></pre>
  <p>The  <code>updatedProduct</code> is a Mongoose Document (<code>ProductDoc</code>) returned from <code>findProduct</code>:</p>
  <pre class="lang-js prettyprint-override"><code>import { Document } from 'mongoose';
  
  export interface ProductDoc extends Document {
      id: string;
      title: string;
      description: string;
      price: number;   
  }
  </code></pre>
  <p>The <code>updateAProduct</code> service is called in a controller as follows:</p>
  <pre class="lang-js prettyprint-override"><code>    @Patch('/update/:id')
      async updateAProduct(@Param('id') id: string, @Body() product: ProductDTO) {
          product.id = id;
          await this.productService.updateAProduct(product);
          return null;
      }
  </code></pre>
  <p>While writing <code>products.service.spec.ts</code> I wrote the following test:</p>
  <pre class="lang-js prettyprint-override"><code>
  describe('ProductsService', () =&gt; {
    let service: ProductsService;
    let model: Model&lt;ProductDoc&gt;;
  
    beforeEach(async () =&gt; {
      const module: TestingModule = await Test.createTestingModule({
        providers: [
          ProductsService,
          {
            provide: getModelToken('Product'),
            useValue: {
              new: jest.fn().mockResolvedValue(mockProduct()),
              constructor: jest.fn().mockResolvedValue(mockProduct()),
              findById: jest.fn(),
              find: jest.fn(),
              findOne: jest.fn(),
              update: jest.fn(),
              create: jest.fn(),
              remove: jest.fn(),
              exec: jest.fn(),
            },
          },
        ],
      }).compile();
  
      service = module.get&lt;ProductsService&gt;(ProductsService);
      model = module.get&lt;Model&lt;ProductDoc&gt;&gt;(getModelToken('Product'));
    });
  
    it('should update a product sucessfully', async() =&gt; {
      
      jest.spyOn(model, 'findById').mockReturnValue({
        exec: jest.fn().mockResolvedValueOnce(
          mockProductDoc({id: 'uuid1', title: 'Title1', description: 'Description1', price: 50.99})
        ),
      } as any);
      const updatedProduct = await service.updateAProduct({
        id: 'uuid1',
        title: 'New Title',
        price: 200.00
      });
  
      expect(updatedProduct).toEqual(mockProduct('uuid1', 'New Title', 'Description1',200.00));
    });
  
  </code></pre>
  <p>My Test fails as follows:</p>
  <pre class="lang-sh prettyprint-override"><code>
   FAIL  src/products/products.service.spec.ts (18.693s)
    ● ProductsService › should update a product sucessfully
  
      TypeError: updatedProduct.save is not a function
  
        49 |             updatedProduct.price = product.price;
        50 |         }
      &gt; 51 |         updatedProduct.save()
           |                        ^
        52 |     }
        53 | 
        54 |     async deleteAProduct(prodID: string) {
  
        at ProductsService.updateAProduct (products/products.service.ts:51:24)
  </code></pre>
  <p>How do I overcome the unavailable <code>.save()</code> in the Jest test?</p>
  <p>Sources:</p>
  <ol>
  <li>I am just replicating the <a href="https://youtu.be/ulfU5vY6I78" rel="nofollow noreferrer">Academind NestJS + MongoDB Tutorial</a></li>
  <li>Since there a no tests in the tutorial I am completely relying on Repository Here <a href="https://github.com/jmcdo29/testing-nestjs" rel="nofollow noreferrer">jmcdo29/testing-nestjs</a></li>
  </ol>
  <h3>EDIT</h3>
  <p><code>findProduct</code> within the service</p>
  <pre class="lang-js prettyprint-override"><code>
    private async findProduct(productID: string): Promise&lt;ProductDoc&gt; {
          let product;
          try {
              product = await this.productModel.findById(productID).exec();
          } catch(error) {
              throw new NotFoundException('Could Not Find Product for given ID.');
          }
          if (!product) {
              throw new NotFoundException('Could Not Find Product for given ID.');
          }
          return product;
      }
  </code></pre>