Typeorm Mock Unit Testing Example Save

Example how to mock TypeORM database connection for your blazing unit-tests with Mocha and Jest

Project README

TypeORM mock unit testing examples with Jest and Mocha

Example how to mock TypeORM for your blazing unit tests with Mocha and Jest. It's a simple express server

Build Status Coverage Status StackOverflow Question Contributions welcome License: MIT

Usage

Testing

Run Mocha unit-tests

npm ci
npm run test:mocha

Run Jest unit-tests

npm run test:jest

Run e2e tests

docker-compose up -d
npm run test:e2e

Development

Run express server after changes

npm start

Build express server

npm run build

Example

Source code

class PostService {
  public getById(id: number): Promise<Post> {
    return this._findPostById(id)
  }

  private _findPostById(id: number): Promise<Post> {
    return createQueryBuilder()
      .select(['post'])
      .from(Post, 'post')
      .leftJoinAndSelect('post.images', 'image')
      .where('post.id = :id', { id })
      .orderBy({ image: 'ASC' })
      .getOne()
  }
}

Jest

describe('postService => getById', () => {
  it('getById method passed', async () => {
    typeorm.createQueryBuilder = jest.fn().mockReturnValue({
      select: jest.fn().mockReturnThis(),
      from: jest.fn().mockReturnThis(),
      leftJoinAndSelect: jest.fn().mockReturnThis(),
      where: jest.fn().mockReturnThis(),
      orderBy: jest.fn().mockReturnThis(),
      getOne: jest.fn().mockResolvedValue('0x0')
    })
    const result = await postService.getById(post.id)

    expect(result).toEqual('0x0')

    const qBuilder = typeorm.createQueryBuilder()
    expect(qBuilder.select).toHaveBeenNthCalledWith(1, ['post'])
    expect(qBuilder.from).toHaveBeenNthCalledWith(1, Post, 'post')
    expect(qBuilder.leftJoinAndSelect).toHaveBeenNthCalledWith(1, 'post.images', 'image')
    expect(qBuilder.where).toHaveBeenNthCalledWith(1, 'post.id = :id', { id: post.id })
    expect(qBuilder.orderBy).toHaveBeenNthCalledWith(1, { image: 'ASC' })
    expect(qBuilder.getOne).toHaveBeenNthCalledWith(1)
  })
})

Sinon

describe('postService => getById', () => {
  let sandbox: SinonSandbox

  beforeEach(() => {
    sandbox = createSandbox()
  })

  afterEach(() => {
    sandbox.restore()
  })

  it('getById method passed', async () => {
    const fakeQueryBuilder = createStubInstance(typeorm.SelectQueryBuilder)

    fakeQueryBuilder.select.withArgs(['post']).returnsThis()
    fakeQueryBuilder.from.withArgs(Post, 'post').returnsThis()
    fakeQueryBuilder.leftJoinAndSelect.withArgs('post.images', 'image').returnsThis()
    fakeQueryBuilder.where.withArgs('post.id = :id', { id: post.id }).returnsThis()
    fakeQueryBuilder.orderBy.withArgs({ image: 'ASC' }).returnsThis()
    fakeQueryBuilder.getOne.resolves('0x0')

    sandbox.stub(typeorm, 'createQueryBuilder').returns(fakeQueryBuilder as any)

    const result = await postService.getById(post.id)
    assert.equal(result, '0x0')
  })
})
Open Source Agenda is not affiliated with "Typeorm Mock Unit Testing Example" Project. README Source: yzevm/typeorm-mock-unit-testing-example
Stars
99
Open Issues
22
Last Commit
1 year ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating