1
1
### AutoMock
2
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.
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.
6
10
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.
8
14
9
- Using AutoMock does not require any additional setup with Nest,
10
- simply install it and start using it.
11
15
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.
13
20
14
21
``` bash
15
22
$ npm i @automock/jest
16
23
```
17
24
18
- ### Examples
25
+ > info ** info** AutoMock only supports ` jest ` , soon there will be support for ` sinon `
26
+
27
+ #### Example
19
28
20
- Consider the following service:
29
+ Consider the following cats service, which takes three different class
30
+ (constructor) parameters:
21
31
22
32
``` ts
23
- // cats.service.ts
33
+ @@ filename ( cats .service )
24
34
import { Injectable } from ' @nestjs/core' ;
25
35
26
36
@Injectable ()
27
37
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 ,
32
42
) {}
33
43
34
44
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!' )
37
47
38
- this .anotherService .doSomethingWithCats (cats );
48
+ this .fooService .doSomethingWithCats (cats );
39
49
}
40
50
}
41
51
```
42
52
53
+ The following example shows how to create a unit-test for this service.
54
+
43
55
``` ts
44
- // cats.service.spec.ts
56
+ @@ filename ( cats .service .spec )
45
57
import { Spec } from ' @automock/jest' ;
46
58
import { CatsService } from ' ./cats.service' ;
47
59
@@ -50,7 +62,7 @@ import Mocked = jest.Mocked;
50
62
describe (' Cats Service Spec' , () => {
51
63
let catsService: CatsService ;
52
64
let logger: Mocked <Logger >;
53
- let anotherService : Mocked <AnotherService >;
65
+ let fooService : Mocked <FooService >;
54
66
55
67
beforeAll (() => {
56
68
const { unit, unitRef } = Spec .create (CatsService )
@@ -60,7 +72,7 @@ describe('Cats Service Spec', () => {
60
72
61
73
catsService = unit ;
62
74
logger = unitRef .get (Logger );
63
- anotherService = unitRef .get (AnotherService );
75
+ fooService = unitRef .get (FooService );
64
76
});
65
77
66
78
describe (' when getting all the cats' , () => {
@@ -71,15 +83,49 @@ describe('Cats Service Spec', () => {
71
83
});
72
84
73
85
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' }]);
75
87
});
76
88
});
77
89
});
78
90
```
79
91
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
+
80
122
Nest suggests some built in tools for creating tests, usually you need to use the ` Test ` \
81
123
factory from ` @nestjs/testing ` ([ example] ( ) ) and create a new testing module. This technique \
82
124
is great for creating integration/e2e tests, simply loading the whole module which includes all \
83
125
the class dependencies
84
126
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