Skip to content

Commit c66d83e

Browse files
committed
docs(): shaping the document
1 parent 1c7e2d6 commit c66d83e

File tree

1 file changed

+68
-22
lines changed

1 file changed

+68
-22
lines changed

content/recipes/automock.md

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,59 @@
11
### AutoMock
22

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.
3+
#### Philosophy
4+
AutoMock is a stand-alone library. It allows complete isolation of class-dependencies
5+
during unit-testing. Automock provides on-the-fly, small DI container that contains
6+
only the class dependencies, or rather - a mock for each class dependency.
7+
By using the TypeScript reflection mechanism, AutoMock can to emit class metadata,
8+
(specifically using `@Injectable` decorator), and thus can perform different manipulations
9+
on the class dependencies.
610

7-
### Installation
11+
AutoMock cuts a lot of time when writing unit tests, and in addition, it also creates
12+
uniformity in the test writing style. It is important to say that AutoMock is intended
13+
for unit-tests only, and is not suitable for other types of tests, such as integration tests.
814

9-
Using AutoMock does not require any additional setup with Nest,
10-
simply install it and start using it.
1115

12-
> info **info** Automock only supports `jest`, soon there will be support for `sinon` as well :)
16+
#### Installation
17+
18+
AutoMock does not require any additional setup with Nest,
19+
simply install it and start using it.
1320

1421
```bash
1522
$ npm i @automock/jest
1623
```
1724

18-
### Examples
25+
> info **info** AutoMock only supports `jest`, soon there will be support for `sinon`
26+
27+
#### Example
1928

20-
Consider the following service:
29+
Consider the following cats service, which takes three different class
30+
(constructor) parameters:
2131

2232
```ts
23-
// cats.service.ts
33+
@@filename(cats.service)
2434
import { Injectable } from '@nestjs/core';
2535

2636
@Injectable()
2737
export class CatsService {
28-
public constructor(
29-
private readonly logger: Logger,
30-
private readonly httpService: HttpService,
31-
private readonly anotherService: AnotherService,
38+
constructor(
39+
private logger: Logger,
40+
private httpService: HttpService,
41+
private fooService: FooService,
3242
) {}
3343

3444
public async getAllCats() {
35-
const cats = await this.httpService.get('http://localhost:3033/cats');
36-
this.logger.log(`Fetched all the cats!`)
45+
const cats = await this.httpService.get('http://localhost:3000/api/cats');
46+
this.logger.log('Fetched all the cats!')
3747

38-
this.anotherService.doSomethingWithCats(cats);
48+
this.fooService.doSomethingWithCats(cats);
3949
}
4050
}
4151
```
4252

53+
The following example shows how to create a unit-test for this service.
54+
4355
```ts
44-
// cats.service.spec.ts
56+
@@filename(cats.service.spec)
4557
import { Spec } from '@automock/jest';
4658
import { CatsService } from './cats.service';
4759

@@ -50,7 +62,7 @@ import Mocked = jest.Mocked;
5062
describe('Cats Service Spec', () => {
5163
let catsService: CatsService;
5264
let logger: Mocked<Logger>;
53-
let anotherService: Mocked<AnotherService>;
65+
let fooService: Mocked<FooService>;
5466

5567
beforeAll(() => {
5668
const { unit, unitRef } = Spec.create(CatsService)
@@ -60,7 +72,7 @@ describe('Cats Service Spec', () => {
6072

6173
catsService = unit;
6274
logger = unitRef.get(Logger);
63-
anotherService = unitRef.get(AnotherService);
75+
fooService = unitRef.get(FooService);
6476
});
6577

6678
describe('when getting all the cats', () => {
@@ -71,15 +83,49 @@ describe('Cats Service Spec', () => {
7183
});
7284

7385
test('then do something with fetched data', () => {
74-
expect(anotherService.doSomethingWithCats).toBeCalledWith([{ id: 1, name: 'Catty' }]);
86+
expect(fooService.doSomethingWithCats).toBeCalledWith([{ id: 1, name: 'Catty' }]);
7587
});
7688
});
7789
});
7890
```
7991

92+
> info **Hint** The `Mocked` type is imported directly from `Jest` namespace.
93+
94+
#### Handling different scenarios
95+
Vivamus at pellentesque turpis. Praesent tincidunt, tellus eu ultricies faucibus,
96+
nulla velit venenatis risus, sollicitudin sollicitudin dolor lectus in dui.
97+
Integer nec lacus eu lacus sodales efficitur sed nec ligula.
98+
99+
##### Working with interfaces
100+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget faucibus felis.
101+
Integer feugiat rhoncus nibh, in elementum dolor sodales a. Pellentesque non luctus dolor,
102+
id ultrices mauris. Nulla convallis diam rhoncus mauris ultrices malesuada.
103+
104+
##### Working with `forardRef()`
105+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget faucibus felis.
106+
Integer feugiat rhoncus nibh, in elementum dolor sodales a. Pellentesque non luctus dolor,
107+
id ultrices mauris. Nulla convallis diam rhoncus mauris ultrices malesuada.
108+
109+
##### Working with injection tokens
110+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget faucibus felis.
111+
Integer feugiat rhoncus nibh, in elementum dolor sodales a. Pellentesque non luctus dolor,
112+
id ultrices mauris. Nulla convallis diam rhoncus mauris ultrices malesuada.
113+
114+
##### Working with `forardRef()`
115+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget faucibus felis.
116+
Integer feugiat rhoncus nibh, in elementum dolor sodales a. Pellentesque non luctus dolor,
117+
id ultrices mauris. Nulla convallis diam rhoncus mauris ultrices malesuada.
118+
119+
120+
#### What's the difference from Nest's built-in testing tools?
121+
80122
Nest suggests some built in tools for creating tests, usually you need to use the `Test` \
81123
factory from `@nestjs/testing` ([example]()) and create a new testing module. This technique \
82124
is great for creating integration/e2e tests, simply loading the whole module which includes all \
83125
the class dependencies
84126

85-
> info **info** `Mocked` is exported from `jest`
127+
128+
#### More Information
129+
130+
Visit the [AutoMock docs site](https://automock.dev/docs) for more information, examples, and API documentation.
131+

0 commit comments

Comments
 (0)