title: Cannot spyOn on a primitive value; undefined given in nestJS
body: <p>I know this question is asked before but I'm having this error in nestjs when I run service test function</p>
  <p>here is my service code</p>
  <p>user.service.ts</p>
  <pre><code>import { Injectable,HttpException, HttpCode, HttpStatus  } from '@nestjs/common';
  import { InjectRepository } from '@nestjs/typeorm';
  import { Repository } from 'typeorm';
  import { User } from './user.entity';
  import { UserRegisterAC } from '../application/userRegisterAC';
  
  @Injectable()
  export class UserService {
  
  constructor(
  @InjectRepository(User)
  private readonly userRepository: Repository&lt;User&gt;) {}
  
  async allUsersList(): Promise&lt;User[]&gt; {
   var users = this.userRepository.find();
  
   if(users==null){
    throw new HttpException(&quot;Users not found&quot;,HttpStatus.NOT_FOUND)
   }else{
    return users as unknown as User[];
   }
  }
  
  async create(userDTO: UserRegisterAC): Promise&lt;User&gt; {
  const user = new User();
  
  user.name = userDTO.name;
  user.phoneNo = userDTO.phoneNo;
  return this.userRepository.save(user);
  }
  }
  </code></pre>
  <p>user.spec.ts</p>
  <pre><code>import { Test, TestingModule } from '@nestjs/testing';
  import { UserService } from './user.service';
  
  
  describe('UserService', () =&gt; {
  let service: UserService;
  
  beforeEach(async () =&gt; {
  const module: TestingModule = await Test.createTestingModule({
    providers: [UserService],
  }).compile();
  
  service = module.get&lt;UserService&gt;(UserService);
  });
  
  it('should return user array', async() =&gt; {
  const result = [{id:1,name:'mayur',phoneNo:'9998814695'}];
  // @ts-ignore
  jest.spyOn(service,'allUsersList').mockImplementation(() =&gt; result); 
  expect(await service.allUsersList()).toBe(result);
   });
  });
  </code></pre>
  <p>In this file I want test allUserlist function in user service which return array of user.
  user entity contain id,name and phoneNo.
  when I run test I'm getting error like</p>
  <pre><code>Cannot spyOn on a primitive value; undefined given
  
    17 |     const result = [{id:1,name:'mayur',phoneNo:'9998814695'}];
    18 |     // @ts-ignore
  &gt; 19 |     jest.spyOn(service,'allUsersList').mockImplementation(() =&gt; result);
       |          ^
    20 |     expect(await service.allUsersList()).toBe(result);
    21 |   });
    22 | });
  </code></pre>
  <p>Here is my jest config</p>
  <pre><code>{
   &quot;moduleFileExtensions&quot;: [&quot;js&quot;, &quot;json&quot;, &quot;ts&quot;],
   &quot;rootDir&quot;: &quot;.&quot;,
   &quot;testEnvironment&quot;: &quot;node&quot;,
   &quot;testRegex&quot;: &quot;.spec.ts$&quot;,
   &quot;transform&quot;: {
   &quot;^.+\\.(t|j)s$&quot;: &quot;ts-jest&quot;
  
  },
  &quot;automock&quot;: true
  
  }
  </code></pre>
  <p>I tried everything please give some suggestion</p>