Skip to content

Commit 633f50e

Browse files
committed
docs: add info about providers, unit and unit-ref
1 parent 964c386 commit 633f50e

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

content/recipes/automock.md

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ test development by automatically mocking class external dependencies.
66

77
#### Introduction
88

9-
The dependency injection container is an essential component of the Nest module system.
10-
This container is utilized both during the testing phase and the application runtime. \
9+
The dependency injection (DI) container is an essential component of the Nest module system.
10+
This container is utilized both during testing, and the application execution. \
1111
Unit tests vary from other types of tests, such as integration tests, in that they must
12-
fully override providers/services within the DI container. External dependencies (providers)
13-
of the so-called "unit" should be totally isolated. That is, all dependencies within
14-
the DI container should be replaced by mock objects. \
15-
As a result, loading the modules and replacing the providers inside them is a process that
16-
loops back on itself. Automock tackles this issue by automatically mocking all the
17-
external dependencies/providers, resulting in total isolation of the unit/class
18-
under test.
12+
fully override providers/services within the DI container. External class dependencies
13+
(providers) of the so-called "unit", have to be totally isolated. That is, all dependencies
14+
within the DI container should be replaced by mock objects. \
15+
As a result, loading the target module and replacing the providers inside it is a process
16+
that loops back on itself. Automock tackles this issue by automatically mocking all the
17+
class external providers, resulting in total isolation of the unit under test.
1918

2019
#### Installation
2120

@@ -101,26 +100,29 @@ const { unit, unitRef } = TestBed.create(CatsService).compile();
101100

102101
Calling `.compile()` returns an object with two properties, `unit`, and `unitRef`.
103102

104-
**`unit`** is the "unit" (service/provider) under test, it is an actual instance of
105-
class being tested (also known as "unit under test").
103+
**`unit`** is the unit under test, it is an actual instance of class being tested.
106104

107-
To store the mocked dependencies of the tested class, the "unit reference" (`unitRef`)
108-
serves as a small container. The container's `.get()` method returns the mocked
109-
dependency with all of its methods automatically stubbed (using `jest.fn()`).
105+
**`unitRef`** is the "unit reference", where the mocked dependencies of the tested class
106+
are stored, in a small container. The container's `.get()` method returns the mocked
107+
dependency with all of its methods automatically stubbed (using `jest.fn()`):
110108

111-
The `.get()` method can accept either a `string` or an actual class (`Type`) as its argument.
112-
This essentially depends on how the class dependency is being injected.
113-
Some specific usage scenarios, including when a `string` and when a `Type` are acceptable,
114-
are provided below.
109+
```typescript
110+
const { unit, unitRef } = TestBed.create(CatsService).compile();
111+
112+
let httpServiceMock: jest.Mocked<HttpService> = unitRef.get(HttpService);
113+
```
115114

116-
#### Handling Different Injections Types
117-
Providers are one of the most important ideas in Nest. A lot of the basic Nest classes,
118-
such as services, repositories, factories, helpers, and so on, can be treated of as providers.
119-
The main idea behind a provider is that it can be injected as a dependency.
115+
> info **info** The `.get()` method can accept either a `string` or an actual class (`Type`) as its argument.
116+
> This essentially depends on how the provider is being injected to the class under test.
120117
121-
##### Working with Interfaces
122-
Consider the following `CatsService` which takes one param which is an instance
123-
of the `Logger` interface.
118+
#### Working with different providers
119+
Providers are one of the most important elements in Nest. You can think of many of
120+
the default Nest classes as providers, including services, repositories, factories,
121+
helpers, and so on. A provider's primary function is to take the form of an
122+
`Injectable` dependency.
123+
124+
Consider the following `CatsService`, it takes one parameter, which is an instance
125+
of the following `Logger` interface:
124126

125127
```typescript
126128
export interface Logger {
@@ -132,23 +134,29 @@ export class CatsService {
132134
}
133135
```
134136

135-
After compiling,
136-
137-
##### Working with Injection Tokens (`@Inject()`)
138-
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget faucibus felis.
139-
Integer feugiat rhoncus nibh, in elementum dolor sodales a. Pellentesque non luctus dolor,
140-
id ultrices mauris. Nulla convallis diam rhoncus mauris ultrices malesuada.
141-
142-
##### Working with `forardRef()`
137+
TypeScript's Reflection API does not support interface reflection yet.
138+
Nest solves this issue with string-based injection tokens (see [Custom Providers](https://docs.nestjs.com/fundamentals/custom-providers)):
143139

144140
```typescript
145-
export class HttpService {}
141+
export const MyLoggerProvider = {
142+
provide: 'MY_LOGGER_TOKEN',
143+
useValue: { ... },
144+
}
146145

147146
export class CatsService {
148-
constructor(@Inject(forwardRef(() => HttpService)) private httpService: HttpService) {}
147+
constructor(@Inject('MY_LOGGER_TOKEN') private readonly logger: Logger) {}
149148
}
150149
```
151150

151+
Automock follows this practice and lets you provide a string-based token instead
152+
of providing the actual class in the `unitRef.get()` method:
153+
154+
```typescript
155+
const { unit, unitRef } = TestBed.create(CatsService).compile();
156+
157+
let loggerMock: jest.Mocked<Logger> = unitRef.get('MY_LOGGER_TOKEN');
158+
```
159+
152160
#### More Information
153161
Visit [Automock GitHub repository](https://github.com/omermorad/automock) for more
154162
information.

0 commit comments

Comments
 (0)