https://github.com/yzevm/typeorm-mock-unit-testing-example
Example how to mock TypeORM database connection for your blazing unit-tests with Mocha and Jest
https://github.com/yzevm/typeorm-mock-unit-testing-example
docker-compose e2e-tests express jest mocha mock sinon stub tests typeorm typescript unit-testing
Last synced: 3 months ago
JSON representation
Example how to mock TypeORM database connection for your blazing unit-tests with Mocha and Jest
- Host: GitHub
- URL: https://github.com/yzevm/typeorm-mock-unit-testing-example
- Owner: yzevm
- License: mit
- Created: 2019-06-23T09:42:42.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T00:53:21.000Z (over 2 years ago)
- Last Synced: 2025-02-06T02:11:52.281Z (3 months ago)
- Topics: docker-compose, e2e-tests, express, jest, mocha, mock, sinon, stub, tests, typeorm, typescript, unit-testing
- Language: TypeScript
- Homepage:
- Size: 1.53 MB
- Stars: 102
- Watchers: 2
- Forks: 14
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this 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][travis-image]][travis-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![StackOverflow Question][so-image]][so-url]
[![Contributions welcome][pr-image]][pr-url]
[![License: MIT][license-image]][license-url]## Usage
### Testing
Run Mocha unit-tests
```sh
npm ci
npm run test:mocha
```Run Jest unit-tests
```sh
npm run test:jest
```Run e2e tests
```sh
docker-compose up -d
npm run test:e2e
```### Development
Run express server after changes
```sh
npm start
```Build express server
```sh
npm run build
```## Example
#### Source code
```js
class PostService {
public getById(id: number): Promise {
return this._findPostById(id)
}private _findPostById(id: number): Promise {
return createQueryBuilder()
.select(['post'])
.from(Post, 'post')
.leftJoinAndSelect('post.images', 'image')
.where('post.id = :id', { id })
.orderBy({ image: 'ASC' })
.getOne()
}
}
```#### Jest
```js
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
```js
describe('postService => getById', () => {
let sandbox: SinonSandboxbeforeEach(() => {
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')
})
})
```[travis-image]: https://travis-ci.org/yegorzaremba/typeorm-mock-unit-testing-example.svg?branch=master
[travis-url]: https://travis-ci.org/yegorzaremba/typeorm-mock-unit-testing-example
[coveralls-image]: https://coveralls.io/repos/github/YegorZaremba/typeorm-mock-unit-testing-example/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/YegorZaremba/typeorm-mock-unit-testing-example?branch=master
[so-image]: https://img.shields.io/badge/StackOverflow-Question-green.svg
[so-url]: https://stackoverflow.com/q/51482701/10432429
[pr-image]: https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat
[pr-url]: https://github.com/yegorzaremba/typeorm-mock-unit-testing-example/issues
[license-image]: https://img.shields.io/badge/License-MIT-yellow.svg
[license-url]: https://opensource.org/licenses/MIT