Skip to content

Commit 48f2986

Browse files
author
Raymond Ottun
committed
feat: mocks can be prioritised
1 parent 2a0c95b commit 48f2986

File tree

5 files changed

+185
-105
lines changed

5 files changed

+185
-105
lines changed

docs/content/en/mocking.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ response:
7070

7171
# Optional: defaults to unlimited
7272
limit: (number|unlimited). how many times this mock should be used. defaults to unlimited
73+
74+
# Optional: defaults to 100
75+
priority: (number|unlimited). matching is ordered by priority (highest first) then creation (earliest first)
7376
```
7477
7578
</code-block>
@@ -103,7 +106,8 @@ limit: (number|unlimited). how many times this mock should be used. defaults to
103106
},
104107
"delay": "seconds to delay the response. defaults to 0"
105108
},
106-
"limit": "(number|unlimited). how many times this mock should be used. defaults to unlimited"
109+
"limit": "(number|unlimited). how many times this mock should be used. defaults to unlimited",
110+
"priority": "matching is ordered by priority (highest first) then creation (earliest first)"
107111
}
108112
```
109113

src/engine/engine.test.ts

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,26 @@ test('match simple method request', () => {
5050
});
5151

5252
expect(matched).toMatchInlineSnapshot(`
53-
Array [
54-
Object {
55-
"id": "exp1",
56-
"limit": "unlimited",
57-
"name": "sample1",
58-
"request": Object {
59-
"headers": Object {},
60-
"path": "/todos",
61-
},
62-
"response": Object {
63-
"body": Array [
64-
Object {
65-
"id": 2,
66-
"text": "get request",
67-
},
68-
],
53+
Array [
54+
Object {
55+
"id": "exp1",
56+
"limit": "unlimited",
57+
"name": "sample1",
58+
"request": Object {
59+
"headers": Object {},
60+
"path": "/todos",
61+
},
62+
"response": Object {
63+
"body": Array [
64+
Object {
65+
"id": 2,
66+
"text": "get request",
6967
},
70-
},
71-
]
72-
`);
68+
],
69+
},
70+
},
71+
]
72+
`);
7373
});
7474

7575
test('match regex method request', () => {
@@ -220,29 +220,29 @@ test('match headers request', () => {
220220
});
221221

222222
expect(matched).toMatchInlineSnapshot(`
223-
Array [
224-
Object {
225-
"id": "exp1",
226-
"limit": "unlimited",
227-
"name": "sample1",
228-
"request": Object {
229-
"headers": Object {
230-
"host": "example.com",
231-
},
232-
"method": "GET",
233-
"path": "/todos",
234-
},
235-
"response": Object {
236-
"body": Array [
237-
Object {
238-
"id": 2,
239-
"text": "get request",
240-
},
241-
],
242-
},
223+
Array [
224+
Object {
225+
"id": "exp1",
226+
"limit": "unlimited",
227+
"name": "sample1",
228+
"request": Object {
229+
"headers": Object {
230+
"host": "example.com",
243231
},
244-
]
245-
`);
232+
"method": "GET",
233+
"path": "/todos",
234+
},
235+
"response": Object {
236+
"body": Array [
237+
Object {
238+
"id": 2,
239+
"text": "get request",
240+
},
241+
],
242+
},
243+
},
244+
]
245+
`);
246246
});
247247

248248
test('match json body request', () => {
@@ -287,26 +287,26 @@ test('match json body request', () => {
287287
});
288288

289289
expect(matched).toMatchInlineSnapshot(`
290-
Array [
291-
Object {
292-
"id": "exp2",
293-
"limit": "unlimited",
294-
"name": "sample2",
295-
"request": Object {
296-
"body": Object {
297-
"id": 3,
298-
"text": "new post",
299-
},
300-
"headers": Object {},
301-
"method": "POST",
302-
"path": "/todos",
303-
},
304-
"response": Object {
305-
"body": Array [],
306-
},
290+
Array [
291+
Object {
292+
"id": "exp2",
293+
"limit": "unlimited",
294+
"name": "sample2",
295+
"request": Object {
296+
"body": Object {
297+
"id": 3,
298+
"text": "new post",
307299
},
308-
]
309-
`);
300+
"headers": Object {},
301+
"method": "POST",
302+
"path": "/todos",
303+
},
304+
"response": Object {
305+
"body": Array [],
306+
},
307+
},
308+
]
309+
`);
310310
});
311311

312312
test('match string body request', () => {
@@ -348,23 +348,23 @@ test('match string body request', () => {
348348
});
349349

350350
expect(matched).toMatchInlineSnapshot(`
351-
Array [
352-
Object {
353-
"id": "exp2",
354-
"limit": "unlimited",
355-
"name": "sample2",
356-
"request": Object {
357-
"body": "[0-9]th todo",
358-
"headers": Object {},
359-
"method": "POST",
360-
"path": "/todos",
361-
},
362-
"response": Object {
363-
"body": Array [],
364-
},
365-
},
366-
]
367-
`);
351+
Array [
352+
Object {
353+
"id": "exp2",
354+
"limit": "unlimited",
355+
"name": "sample2",
356+
"request": Object {
357+
"body": "[0-9]th todo",
358+
"headers": Object {},
359+
"method": "POST",
360+
"path": "/todos",
361+
},
362+
"response": Object {
363+
"body": Array [],
364+
},
365+
},
366+
]
367+
`);
368368
});
369369

370370
test('mock: add results in error', () => {
@@ -384,18 +384,18 @@ test('mock: add', () => {
384384
});
385385
expect(add).not.toThrow();
386386
expect(engine.mocks).toMatchInlineSnapshot(`
387-
Array [
388-
Object {
389-
"id": "new",
390-
"limit": "unlimited",
391-
"name": "base expectation",
392-
"request": Object {
393-
"path": "/hello",
394-
},
395-
"response": Object {},
396-
},
397-
]
398-
`);
387+
Array [
388+
Object {
389+
"id": "new",
390+
"limit": "unlimited",
391+
"name": "base expectation",
392+
"request": Object {
393+
"path": "/hello",
394+
},
395+
"response": Object {},
396+
},
397+
]
398+
`);
399399
});
400400

401401
test('mock: remove', () => {

src/engine/engine.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ export class Engine extends EventEmitter {
9999
return !!interfaces;
100100
};
101101

102-
private baseMock() {
102+
private baseMock(): Mock {
103103
return {
104104
id: shortId(),
105105
name: 'Mock',
106106
limit: 'unlimited',
107+
request: { path: '/hello' },
107108
};
108109
}
109110

@@ -160,7 +161,11 @@ export class Engine extends EventEmitter {
160161
return pastMatches < mock.limit;
161162
});
162163

163-
return matches;
164+
return matches.sort((m1, m2) => {
165+
const pr1 = m1.priority || 0;
166+
const pr2 = m2.priority || 0;
167+
return pr2 - pr1;
168+
});
164169
}
165170

166171
async proxy(req: ProxyRequest): Promise<Response> {

src/engine/priority.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { create } from '../engine';
2+
import { Mock, Request } from '../types';
3+
4+
test('priority', async () => {
5+
const mocks: Mock[] = [
6+
{
7+
id: 'exp1',
8+
name: 'sample1',
9+
request: {
10+
path: '/todos',
11+
},
12+
response: {
13+
body: 'No Priority set',
14+
},
15+
limit: 2,
16+
},
17+
{
18+
id: 'exp2',
19+
name: 'sample2',
20+
request: {
21+
path: '/todos',
22+
},
23+
response: {
24+
body: 'Mock priority -1',
25+
},
26+
limit: 2,
27+
priority: -1,
28+
},
29+
{
30+
id: 'exp2',
31+
name: 'sample2',
32+
request: {
33+
path: '/todos',
34+
},
35+
response: {
36+
body: 'Mock priority 0',
37+
},
38+
limit: 2,
39+
priority: 0,
40+
},
41+
{
42+
id: 'exp2',
43+
name: 'sample2',
44+
request: {
45+
path: '/todos',
46+
},
47+
response: {
48+
body: 'Mock priority 1',
49+
},
50+
limit: 2,
51+
priority: 1,
52+
},
53+
];
54+
55+
const request: Request = {
56+
path: '/todos',
57+
method: 'GET',
58+
headers: {},
59+
};
60+
61+
const engine = create({ mocks, config: {} });
62+
63+
await engine.execute(request);
64+
const lastMatch = await engine.execute(request);
65+
66+
expect(lastMatch.response).toMatchInlineSnapshot(`
67+
Object {
68+
"body": "Mock priority 1",
69+
}
70+
`);
71+
});

src/server/routes/mocks.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,17 @@ test('retrieve all mocks', async () => {
298298

299299
expect(res.status).toBe(200);
300300
expect(res.body).toMatchInlineSnapshot(`
301-
Array [
302-
Object {
303-
"id": "sample-mock",
304-
"limit": "unlimited",
305-
"name": "test mocks",
306-
"request": Object {
307-
"method": "GET",
308-
"path": "/tasks",
309-
},
310-
"response": null,
311-
},
312-
]
313-
`);
301+
Array [
302+
Object {
303+
"id": "sample-mock",
304+
"limit": "unlimited",
305+
"name": "test mocks",
306+
"request": Object {
307+
"method": "GET",
308+
"path": "/tasks",
309+
},
310+
"response": null,
311+
},
312+
]
313+
`);
314314
});

0 commit comments

Comments
 (0)