-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtypes.test.ts
More file actions
105 lines (95 loc) · 2.75 KB
/
types.test.ts
File metadata and controls
105 lines (95 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// TypeScript compilation test - verifies type definitions are correct
// This file is only compiled, not executed
import mock = require('./index');
// Test basic config
const basicApi = mock({
mockRoutes: [
{
name: 'getUsers',
mockRoute: '/api/users',
method: 'GET',
testScope: 'success',
jsonTemplate: '{ "users": [] }'
}
]
});
// Test full config with all options
const fullApi = mock({
jsonStore: './data.json',
cors: {
origin: 'http://localhost:3000',
credentials: true
},
logging: 'verbose',
presets: {
'error-mode': {
'*': { scope: 'error' }
},
'slow-mode': {
'getUsers': { latency: 1000, scenario: 'empty' }
}
},
mockRoutes: [
{
name: 'getUsers',
mockRoute: '/api/users',
method: 'GET',
testScope: 'success',
testScenario: 0,
latency: '100-500',
jsonTemplate: [
(req) => JSON.stringify({ users: [], query: req.query }),
{ 'empty': (req) => '{ "users": [] }' }
],
data: { customField: 'value' },
helpers: { customHelper: () => 'result' }
},
{
name: 'getUser',
mockRoute: '/api/users/:id',
method: 'GET',
testScope: 'success',
testScenario: (req) => req.params.id === '0' ? 'notFound' : 0,
jsonTemplate: (req) => JSON.stringify({ id: req.params.id })
},
{
name: 'createUser',
mockRoute: '/api/users',
method: 'POST',
testScope: 'created',
errorBody: { error: 'Custom error' }
}
]
});
// Test custom logging function
const customLogApi = mock({
logging: (info) => {
console.log(info.method, info.url, info.status, info.notFound);
},
mockRoutes: [{ name: 'test', mockRoute: '/test', testScope: 'success', jsonTemplate: '{}' }]
});
// Test createServer returns Express app
const app = basicApi.createServer();
// Test instance properties
const routes: mock.MockRoute[] = basicApi.routes;
const activePreset: string | null = basicApi.activePreset;
const presets: Record<string, mock.Preset> = basicApi.presets;
// Test methods exist
basicApi.reset();
// Test type exports are accessible
type Scope = mock.TestScope;
type Route = mock.MockRoute;
type Config = mock.MockConfig;
type Instance = mock.MockInstance;
type Log = mock.LogInfo;
const scope: Scope = 'success';
const logInfo: Log = {
method: 'GET',
url: '/api/test',
routeName: 'test',
scope: 'success',
scenario: 0,
latency: 100,
status: 200
};
console.log('Types are valid!');