Skip to content

Commit 1c7e2d6

Browse files
committed
docs(recipies): add recipie for automock library
1 parent aa5f2c2 commit 1c7e2d6

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

content/recipes/automock.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
### AutoMock
2+
3+
AutoMock is a standalone library for creating unit tests (specs). \
4+
Using TypeScript's reflection mechanism, it is able to mock class dependencies
5+
from the constructor level.
6+
7+
### Installation
8+
9+
Using AutoMock does not require any additional setup with Nest,
10+
simply install it and start using it.
11+
12+
> info **info** Automock only supports `jest`, soon there will be support for `sinon` as well :)
13+
14+
```bash
15+
$ npm i @automock/jest
16+
```
17+
18+
### Examples
19+
20+
Consider the following service:
21+
22+
```ts
23+
// cats.service.ts
24+
import { Injectable } from '@nestjs/core';
25+
26+
@Injectable()
27+
export class CatsService {
28+
public constructor(
29+
private readonly logger: Logger,
30+
private readonly httpService: HttpService,
31+
private readonly anotherService: AnotherService,
32+
) {}
33+
34+
public async getAllCats() {
35+
const cats = await this.httpService.get('http://localhost:3033/cats');
36+
this.logger.log(`Fetched all the cats!`)
37+
38+
this.anotherService.doSomethingWithCats(cats);
39+
}
40+
}
41+
```
42+
43+
```ts
44+
// cats.service.spec.ts
45+
import { Spec } from '@automock/jest';
46+
import { CatsService } from './cats.service';
47+
48+
import Mocked = jest.Mocked;
49+
50+
describe('Cats Service Spec', () => {
51+
let catsService: CatsService;
52+
let logger: Mocked<Logger>;
53+
let anotherService: Mocked<AnotherService>;
54+
55+
beforeAll(() => {
56+
const { unit, unitRef } = Spec.create(CatsService)
57+
.mock(HttpService)
58+
.using({ get: async () => Promise.resolve([{ id: 1, name: 'Catty' }]), })
59+
.compile();
60+
61+
catsService = unit;
62+
logger = unitRef.get(Logger);
63+
anotherService = unitRef.get(AnotherService);
64+
});
65+
66+
describe('when getting all the cats', () => {
67+
beforeAll(async () => await catsService.getAllCats());
68+
69+
test('then call the logger log', () => {
70+
expect(logger.log).toBeCalled();
71+
});
72+
73+
test('then do something with fetched data', () => {
74+
expect(anotherService.doSomethingWithCats).toBeCalledWith([{ id: 1, name: 'Catty' }]);
75+
});
76+
});
77+
});
78+
```
79+
80+
Nest suggests some built in tools for creating tests, usually you need to use the `Test` \
81+
factory from `@nestjs/testing` ([example]()) and create a new testing module. This technique \
82+
is great for creating integration/e2e tests, simply loading the whole module which includes all \
83+
the class dependencies
84+
85+
> info **info** `Mocked` is exported from `jest`

src/app/homepage/menu/menu.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export class MenuComponent implements OnInit {
239239
{ title: 'Prisma', path: '/recipes/prisma' },
240240
{ title: 'Serve static', path: '/recipes/serve-static' },
241241
{ title: 'Commander', path: '/recipes/nest-commander' },
242+
{ title: 'AutoMock', path: '/recipes/automock' },
242243
],
243244
},
244245
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { BasePageComponent } from '../../page/page.component';
3+
4+
@Component({
5+
selector: 'app-automock',
6+
templateUrl: './automock.component.html',
7+
changeDetection: ChangeDetectionStrategy.OnPush,
8+
})
9+
export class AutomockComponent extends BasePageComponent {}

src/app/homepage/pages/recipes/recipes.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { SqlTypeormComponent } from './sql-typeorm/sql-typeorm.component';
1515
import { TerminusComponent } from './terminus/terminus.component';
1616
import { RouterModuleComponent } from './router-module/router-module.component';
1717
import { NestCommanderComponent } from './nest-commander/nest-commander.component';
18+
import { AutomockComponent } from './automock/automock.component';
1819

1920
const routes: Routes = [
2021
{
@@ -94,6 +95,11 @@ const routes: Routes = [
9495
component: NestCommanderComponent,
9596
data: { title: 'Nest Commander' },
9697
},
98+
{
99+
path: 'automock',
100+
component: AutomockComponent,
101+
data: { title: 'AutoMock' },
102+
},
97103
];
98104

99105
@NgModule({
@@ -112,6 +118,7 @@ const routes: Routes = [
112118
RouterModuleComponent,
113119
ServeStaticComponent,
114120
NestCommanderComponent,
121+
AutomockComponent,
115122
],
116123
})
117124
export class RecipesModule {}

0 commit comments

Comments
 (0)