title: Process of testing with TypeORM and Nestjs, and jest using mocks?
body: <p><strong>This question can likely be generalized to stubbing repositories in a service and how to properly test and provide coverage in the context of this question.</strong></p>
  
  <p>I am in the process of learning more about testing, but am stuck with how to properly perform testing that involves the DB.  </p>
  
  <p>I have a User entity that defines the columns and some initial validation logic.</p>
  
  <pre class="lang-js prettyprint-override"><code>    import { IsAlphanumeric, IsEmail, MinLength } from 'class-validator';
      import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
      @Entity()
      export class User {
        @PrimaryGeneratedColumn()
        public id!: number;
  
        @Column()
        public name!: string;
  
        @IsEmail()
        @Column()
        public email!: string;
  
        @MinLength(8)
        @Column()
        public password!: string;
      }
  </code></pre>
  
  <p>And I have a UserService that injects the Repository for the entity.</p>
  
  <pre class="lang-js prettyprint-override"><code>    import { Injectable } from '@nestjs/common';
      import { InjectRepository } from '@nestjs/typeorm';
      import { validateOrReject } from 'class-validator';
      import { Repository } from 'typeorm';
      import { CreateUserDTO } from './dto/create-user.dto';
      import { User } from './user.entity';
  
      @Injectable()
      export class UserService {
        constructor(
          @InjectRepository(User) private readonly userRepository: Repository&lt;User&gt;
        ) {}
  
        public async create(dto: CreateUserDTO) {
          const user = this.userRepository.create(dto);
          await validateOrReject(user);
          await this.userRepository.save(user);
        }
  
        public async findAll(): Promise&lt;User[]&gt; {
          return await this.userRepository.find();
        }
  
        public async findByEmail(email: string): Promise&lt;User | undefined&gt; {
          return await this.userRepository.findOne({
            where: {
              email,
            },
          });
        }
      }
  </code></pre>
  
  <p>And here is my preliminary test so you can follow my train of thought...</p>
  
  <pre class="lang-js prettyprint-override"><code>    import { Test, TestingModule } from '@nestjs/testing';
      import { getRepositoryToken } from '@nestjs/typeorm';
      import { User } from './user.entity';
      import { UserService } from './user.service';
  
      const createMock = jest.fn((dto: any) =&gt; {
        return dto;
      });
  
      const saveMock = jest.fn((dto: any) =&gt; {
        return dto;
      });
  
      const MockRepository = jest.fn().mockImplementation(() =&gt; {
        return {
          create: createMock,
          save: saveMock,
        };
      });
      const mockRepository = new MockRepository();
  
      describe('UserService', () =&gt; {
        let service: UserService;
  
        beforeAll(async () =&gt; {
          const module: TestingModule = await Test.createTestingModule({
            providers: [
              UserService,
              {
                provide: getRepositoryToken(User),
                useValue: mockRepository,
              },
            ],
          }).compile();
          service = module.get&lt;UserService&gt;(UserService);
        });
  
        it('should be defined', () =&gt; {
          expect(service).toBeDefined();
        });
  
        it('should not create invalid user', async () =&gt; {
          // ??
        });
      });
  </code></pre>
  
  <p>So while I can make the test run and everything, I am not sure what I am actually supposed to be testing.  I can obviously test that it validates on create, and for other things like findAll, I feel like I am just mocking the database?  For me to properly test this, would it need to be connected to a database so I can check that the right data is returned?</p>
  
  <p>The nest documents say "we usually want to avoid any database connection", but doesn't doing that defeat the purpose since we aren't <em>really</em> testing the functionality?  Because while I can mock that the save returns a value, I am not testing for any errors that can occur with unique columns, nullable data, incrementing values to be set, etc... right?</p>