How do you mock typeorm's getManager using testdouble? How do you mock typeorm's getManager using testdouble? typescript typescript

How do you mock typeorm's getManager using testdouble?


I tried sinon instead of testdouble. I created a small repository, which shows how you can mock database for your blazing unit-tests :)

I tried to cover all TypeORM test cases using Jest and Mocha

Example

import * as typeorm from 'typeorm'import { createSandbox, SinonSandbox, createStubInstance } from 'sinon'import { deepEqual } from 'assert'class Mock {  sandbox: SinonSandbox  constructor(method: string | any, fakeData: any, args?: any) {    this.sandbox = createSandbox()    if (args) {      this.sandbox.stub(typeorm, method).withArgs(args).returns(fakeData)    } else {      this.sandbox.stub(typeorm, method).returns(fakeData)    }  }  close() {    this.sandbox.restore()  }}describe('mocha => typeorm => getManager', () => {  let mock: Mock  it('getAll method passed', async () => {    const fakeManager = createStubInstance(typeorm.EntityManager)    fakeManager.find.resolves([post])    mock = new Mock('getManager', fakeManager)    const result = await postService.getAll()    deepEqual(result, [post])  })  afterEach(() => mock.close())})