1
1
# mocr [ ![ npm] [ npm-image ]] [ npm-url ] [ ![ github-ci] [ github-ci-image ]] [ github-ci-url ]
2
2
3
- A mock http server used in tests
3
+ > A mock http server used in tests
4
+
5
+ <p align =" center " >
6
+ <img src =" https://user-images.githubusercontent.com/6333409/99885443-dcd40e00-2c2c-11eb-9261-6cfd3d7de5a0.png " alt =" Mocr Logo " width =" 125 " height =" 125 " />
7
+ </p >
4
8
5
9
## Features
6
10
7
11
- Easy to use, mock http server
8
- - Spy on the requests received by the server
12
+ - Spy/track requests received by the server
9
13
- Designed to work with end-to-end & unit tests
10
14
- Strongly typed, types included
11
15
- Zero dependencies
@@ -22,19 +26,19 @@ npm install --save-dev mocr
22
26
23
27
All config options mentioned below are ** _ optional_ ** .
24
28
25
- | Name | Default | Description |
26
- | ---------- | --------- | ------------------------------------------------------------------------------------------------------------ |
27
- | debug | false | When set to true, logging will be enabled. |
28
- | port | 9091 | The port that the server will be running. |
29
- | requestSpy | undefined | Can be a spy or a call . See [ usage ] ( #usage ) below. If defined, will be called with ` (request, requestBody) ` . |
29
+ | Name | Default | Description |
30
+ | ---------- | --------- | ----------------------------------------------------------------------------------------------------------- |
31
+ | debug | false | When set to true, logging will be enabled. |
32
+ | port | 9091 | The port that the server will be running. |
33
+ | requestSpy | undefined | This spy can track all the requests that reach the server . See [ createRequestSpy ] ( #createRequestSpy ) below. |
30
34
31
35
## Usage
32
36
33
37
``` js
34
- import mocr from ' mocr' ;
38
+ import mocr , { createRequestSpy } from ' mocr' ;
35
39
36
40
describe (' my tests' , () => {
37
- const requestSpy = jest . fn ();
41
+ const requestSpy = createRequestSpy ();
38
42
39
43
const mockServer = mocr ({
40
44
/* Configuration */
@@ -47,7 +51,7 @@ describe('my tests', () => {
47
51
48
52
beforeEach (async () => {
49
53
// Reset the request spy
50
- requestSpy .mockReset ();
54
+ requestSpy .reset ();
51
55
});
52
56
53
57
afterAll (async () => {
@@ -56,25 +60,37 @@ describe('my tests', () => {
56
60
});
57
61
58
62
it (' should make a call to the backend when pressing the button' , () => {
59
- const requestSpy = jest .fn ();
60
-
61
63
// Press the button
62
64
63
- const request = requestSpy .mock .calls [0 ][0 ];
64
- const requestBody = requestSpy .mock .calls [0 ][1 ];
65
+ const { request , body } = requestSpy .calls [0 ];
65
66
66
67
expect (requestSpy).toHaveBeenCalledTimes (1 );
67
68
expect (request .method ).toBe (/* Expected Method, ie. POST, PUT */ );
68
- expect (requestBody ).toHaveBeenCalledWith (/* Expected Request Body */ );
69
+ expect (body ).toHaveBeenCalledWith (/* Expected Request Body */ );
69
70
});
70
71
});
71
72
```
72
73
73
- [ github-ci-image ] : https://github.com/manosim/mocr/workflows/Run%20Tests/badge.svg
74
- [ github-ci-url ] : https://github.com/manosim/mocr/actions
75
- [ npm-image ] : https://badge.fury.io/js/mocr.svg
76
- [ npm-url ] : https://www.npmjs.com/package/mocr
74
+ ## Methods
75
+
76
+ ### mocr
77
+
78
+ Used to create an instance of _ mocr_ - it accepts _ optional_ configuration. You can have as many _ mocr_ servers running in parallel as long as they run on a [ different port] ( #configuration ) .
79
+
80
+ ### createRequestSpy
81
+
82
+ Creates a fresh request spy. This records/tracks all _ incoming_ requests to the mock server along with their body/data(if any). To be used for validating requests/content leaving your application. Below you can find all available methods for a RequestSpy.
83
+
84
+ | Name | Description |
85
+ | ----- | --------------------------------------------------------------------- |
86
+ | calls | An array of all the calls. `[ {request: IncomingMessage. body: string | {} } ] ` |
87
+ | reset | Empties the ` calls ` array. |
77
88
78
89
## License
79
90
80
91
This project is licensed under the MIT License - see the [ LICENSE] ( LICENSE ) file for details
92
+
93
+ [ github-ci-image ] : https://github.com/manosim/mocr/workflows/Run%20Tests/badge.svg
94
+ [ github-ci-url ] : https://github.com/manosim/mocr/actions
95
+ [ npm-image ] : https://badge.fury.io/js/mocr.svg
96
+ [ npm-url ] : https://www.npmjs.com/package/mocr
0 commit comments