{"id":14967400,"url":"https://github.com/yzevm/typeorm-mock-unit-testing-example","last_synced_at":"2025-10-25T19:31:00.433Z","repository":{"id":40292928,"uuid":"193330614","full_name":"yzevm/typeorm-mock-unit-testing-example","owner":"yzevm","description":"Example how to mock TypeORM database connection for your blazing unit-tests with Mocha and Jest","archived":false,"fork":false,"pushed_at":"2023-01-04T00:53:21.000Z","size":1604,"stargazers_count":102,"open_issues_count":22,"forks_count":14,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-06T02:11:52.281Z","etag":null,"topics":["docker-compose","e2e-tests","express","jest","mocha","mock","sinon","stub","tests","typeorm","typescript","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yzevm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-23T09:42:42.000Z","updated_at":"2025-01-20T05:37:22.000Z","dependencies_parsed_at":"2023-02-01T16:31:46.967Z","dependency_job_id":null,"html_url":"https://github.com/yzevm/typeorm-mock-unit-testing-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yzevm%2Ftypeorm-mock-unit-testing-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yzevm%2Ftypeorm-mock-unit-testing-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yzevm%2Ftypeorm-mock-unit-testing-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yzevm%2Ftypeorm-mock-unit-testing-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yzevm","download_url":"https://codeload.github.com/yzevm/typeorm-mock-unit-testing-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238200886,"owners_count":19433119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker-compose","e2e-tests","express","jest","mocha","mock","sinon","stub","tests","typeorm","typescript","unit-testing"],"created_at":"2024-09-24T13:38:00.110Z","updated_at":"2025-10-25T19:30:59.925Z","avatar_url":"https://github.com/yzevm.png","language":"TypeScript","readme":"## TypeORM mock unit testing examples with Jest and Mocha\n\nExample how to mock TypeORM for your blazing unit tests with Mocha and Jest.\nIt's a simple `express` server\n\n[![Build Status][travis-image]][travis-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n[![StackOverflow Question][so-image]][so-url]\n[![Contributions welcome][pr-image]][pr-url]\n[![License: MIT][license-image]][license-url]\n\n## Usage\n\n### Testing\n\nRun Mocha unit-tests\n\n```sh\nnpm ci\nnpm run test:mocha\n```\n\nRun Jest unit-tests\n\n```sh\nnpm run test:jest\n```\n\nRun e2e tests\n\n```sh\ndocker-compose up -d\nnpm run test:e2e\n```\n\n### Development\n\nRun express server after changes\n\n```sh\nnpm start\n```\n\nBuild express server\n\n```sh\nnpm run build\n```\n\n## Example\n\n#### Source code\n```js\nclass PostService {\n  public getById(id: number): Promise\u003cPost\u003e {\n    return this._findPostById(id)\n  }\n\n  private _findPostById(id: number): Promise\u003cPost\u003e {\n    return createQueryBuilder()\n      .select(['post'])\n      .from(Post, 'post')\n      .leftJoinAndSelect('post.images', 'image')\n      .where('post.id = :id', { id })\n      .orderBy({ image: 'ASC' })\n      .getOne()\n  }\n}\n```\n\n#### Jest\n```js\ndescribe('postService =\u003e getById', () =\u003e {\n  it('getById method passed', async () =\u003e {\n    typeorm.createQueryBuilder = jest.fn().mockReturnValue({\n      select: jest.fn().mockReturnThis(),\n      from: jest.fn().mockReturnThis(),\n      leftJoinAndSelect: jest.fn().mockReturnThis(),\n      where: jest.fn().mockReturnThis(),\n      orderBy: jest.fn().mockReturnThis(),\n      getOne: jest.fn().mockResolvedValue('0x0')\n    })\n    const result = await postService.getById(post.id)\n\n    expect(result).toEqual('0x0')\n\n    const qBuilder = typeorm.createQueryBuilder()\n    expect(qBuilder.select).toHaveBeenNthCalledWith(1, ['post'])\n    expect(qBuilder.from).toHaveBeenNthCalledWith(1, Post, 'post')\n    expect(qBuilder.leftJoinAndSelect).toHaveBeenNthCalledWith(1, 'post.images', 'image')\n    expect(qBuilder.where).toHaveBeenNthCalledWith(1, 'post.id = :id', { id: post.id })\n    expect(qBuilder.orderBy).toHaveBeenNthCalledWith(1, { image: 'ASC' })\n    expect(qBuilder.getOne).toHaveBeenNthCalledWith(1)\n  })\n})\n```\n\n#### Sinon\n```js\ndescribe('postService =\u003e getById', () =\u003e {\n  let sandbox: SinonSandbox\n\n  beforeEach(() =\u003e {\n    sandbox = createSandbox()\n  })\n\n  afterEach(() =\u003e {\n    sandbox.restore()\n  })\n\n  it('getById method passed', async () =\u003e {\n    const fakeQueryBuilder = createStubInstance(typeorm.SelectQueryBuilder)\n\n    fakeQueryBuilder.select.withArgs(['post']).returnsThis()\n    fakeQueryBuilder.from.withArgs(Post, 'post').returnsThis()\n    fakeQueryBuilder.leftJoinAndSelect.withArgs('post.images', 'image').returnsThis()\n    fakeQueryBuilder.where.withArgs('post.id = :id', { id: post.id }).returnsThis()\n    fakeQueryBuilder.orderBy.withArgs({ image: 'ASC' }).returnsThis()\n    fakeQueryBuilder.getOne.resolves('0x0')\n\n    sandbox.stub(typeorm, 'createQueryBuilder').returns(fakeQueryBuilder as any)\n\n    const result = await postService.getById(post.id)\n    assert.equal(result, '0x0')\n  })\n})\n```\n\n[travis-image]: https://travis-ci.org/yegorzaremba/typeorm-mock-unit-testing-example.svg?branch=master\n[travis-url]: https://travis-ci.org/yegorzaremba/typeorm-mock-unit-testing-example\n[coveralls-image]: https://coveralls.io/repos/github/YegorZaremba/typeorm-mock-unit-testing-example/badge.svg?branch=master\n[coveralls-url]: https://coveralls.io/github/YegorZaremba/typeorm-mock-unit-testing-example?branch=master\n[so-image]: https://img.shields.io/badge/StackOverflow-Question-green.svg\n[so-url]: https://stackoverflow.com/q/51482701/10432429\n[pr-image]: https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat\n[pr-url]: https://github.com/yegorzaremba/typeorm-mock-unit-testing-example/issues\n[license-image]: https://img.shields.io/badge/License-MIT-yellow.svg\n[license-url]: https://opensource.org/licenses/MIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyzevm%2Ftypeorm-mock-unit-testing-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyzevm%2Ftypeorm-mock-unit-testing-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyzevm%2Ftypeorm-mock-unit-testing-example/lists"}