lambder 1.0.129 → 1.0.130
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +49 -0
- package/dist/LambderMSW.d.ts +104 -0
- package/dist/LambderMSW.js +203 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/docs/MSW_MOCKING.md +590 -0
- package/examples/msw-mocking-example.ts +241 -0
- package/package.json +9 -1
- package/src/LambderMSW.ts +330 -0
- package/src/index.ts +11 -0
- package/tests/msw-integration.test.ts +183 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example test file demonstrating MSW integration with Lambder
|
|
3
|
+
*
|
|
4
|
+
* To run this test:
|
|
5
|
+
* 1. Install MSW: npm install --save-dev msw
|
|
6
|
+
* 2. Install Vitest (if not already): npm install --save-dev vitest
|
|
7
|
+
* 3. Run: npm test
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect, beforeAll, afterEach, afterAll } from 'vitest';
|
|
11
|
+
// Uncomment when MSW is installed:
|
|
12
|
+
// import { setupServer } from 'msw/node';
|
|
13
|
+
import {
|
|
14
|
+
mockLambderApi,
|
|
15
|
+
mockLambderApiError,
|
|
16
|
+
mockLambderSessionExpired,
|
|
17
|
+
LambderCaller,
|
|
18
|
+
type ApiContractShape
|
|
19
|
+
} from '../src/index.js';
|
|
20
|
+
|
|
21
|
+
// Define test API contract
|
|
22
|
+
const testApiContract = {
|
|
23
|
+
'test.echo': {
|
|
24
|
+
input: { message: '' as string },
|
|
25
|
+
output: { echo: '' as string }
|
|
26
|
+
},
|
|
27
|
+
'test.add': {
|
|
28
|
+
input: { a: 0 as number, b: 0 as number },
|
|
29
|
+
output: { result: 0 as number }
|
|
30
|
+
},
|
|
31
|
+
'test.getUser': {
|
|
32
|
+
input: { userId: '' as string },
|
|
33
|
+
output: { id: '' as string, name: '' as string }
|
|
34
|
+
}
|
|
35
|
+
} satisfies ApiContractShape;
|
|
36
|
+
|
|
37
|
+
type TestApiContract = typeof testApiContract;
|
|
38
|
+
|
|
39
|
+
// MSW handlers
|
|
40
|
+
const handlers = [
|
|
41
|
+
mockLambderApi<TestApiContract, 'test.echo'>(
|
|
42
|
+
'test.echo',
|
|
43
|
+
(input) => ({
|
|
44
|
+
echo: input.message
|
|
45
|
+
})
|
|
46
|
+
),
|
|
47
|
+
mockLambderApi<TestApiContract, 'test.add'>(
|
|
48
|
+
'test.add',
|
|
49
|
+
(input) => ({
|
|
50
|
+
result: input.a + input.b
|
|
51
|
+
})
|
|
52
|
+
),
|
|
53
|
+
mockLambderApi<TestApiContract, 'test.getUser'>(
|
|
54
|
+
'test.getUser',
|
|
55
|
+
(input) => ({
|
|
56
|
+
id: input.userId,
|
|
57
|
+
name: `User ${input.userId}`
|
|
58
|
+
})
|
|
59
|
+
)
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
// Set up MSW server (uncomment when MSW is installed)
|
|
63
|
+
// const server = setupServer(...handlers);
|
|
64
|
+
|
|
65
|
+
describe('Lambder MSW Integration', () => {
|
|
66
|
+
// Uncomment when MSW is installed:
|
|
67
|
+
// beforeAll(() => server.listen());
|
|
68
|
+
// afterEach(() => server.resetHandlers());
|
|
69
|
+
// afterAll(() => server.close());
|
|
70
|
+
|
|
71
|
+
// Initialize Lambder caller
|
|
72
|
+
const caller = new LambderCaller<TestApiContract>({
|
|
73
|
+
apiPath: '/api',
|
|
74
|
+
isCorsEnabled: false
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it.skip('should mock echo API successfully', async () => {
|
|
78
|
+
const result = await caller.api('test.echo', { message: 'Hello MSW!' });
|
|
79
|
+
|
|
80
|
+
expect(result).toEqual({ echo: 'Hello MSW!' });
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it.skip('should mock math API successfully', async () => {
|
|
84
|
+
const result = await caller.api('test.add', { a: 5, b: 3 });
|
|
85
|
+
|
|
86
|
+
expect(result).toEqual({ result: 8 });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it.skip('should mock user API with dynamic data', async () => {
|
|
90
|
+
const result = await caller.api('test.getUser', { userId: '123' });
|
|
91
|
+
|
|
92
|
+
expect(result).toEqual({
|
|
93
|
+
id: '123',
|
|
94
|
+
name: 'User 123'
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it.skip('should handle API errors', async () => {
|
|
99
|
+
// Override handler for this test
|
|
100
|
+
// server.use(
|
|
101
|
+
// mockLambderApiError<TestApiContract, 'test.getUser'>(
|
|
102
|
+
// 'test.getUser',
|
|
103
|
+
// 'User not found'
|
|
104
|
+
// )
|
|
105
|
+
// );
|
|
106
|
+
|
|
107
|
+
const result = await caller.api('test.getUser', { userId: '999' });
|
|
108
|
+
|
|
109
|
+
expect(result).toBeNull();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it.skip('should handle session expired', async () => {
|
|
113
|
+
// Override handler for this test
|
|
114
|
+
// server.use(
|
|
115
|
+
// mockLambderSessionExpired<TestApiContract, 'test.getUser'>(
|
|
116
|
+
// 'test.getUser'
|
|
117
|
+
// )
|
|
118
|
+
// );
|
|
119
|
+
|
|
120
|
+
const result = await caller.api('test.getUser', { userId: '123' });
|
|
121
|
+
|
|
122
|
+
expect(result).toBeNull();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should export MSW utilities', () => {
|
|
126
|
+
// Verify exports are available
|
|
127
|
+
expect(mockLambderApi).toBeDefined();
|
|
128
|
+
expect(mockLambderApiError).toBeDefined();
|
|
129
|
+
expect(mockLambderSessionExpired).toBeDefined();
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('MSW Utility Functions', () => {
|
|
134
|
+
it('should create type-safe mock handlers', () => {
|
|
135
|
+
const handler = mockLambderApi<TestApiContract, 'test.echo'>(
|
|
136
|
+
'test.echo',
|
|
137
|
+
(input) => ({
|
|
138
|
+
echo: input.message // Type-safe input access
|
|
139
|
+
})
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(handler).toBeDefined();
|
|
143
|
+
expect(typeof handler).toBe('function');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should create error mock handlers', () => {
|
|
147
|
+
const handler = mockLambderApiError<TestApiContract, 'test.echo'>(
|
|
148
|
+
'test.echo',
|
|
149
|
+
'Test error message'
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
expect(handler).toBeDefined();
|
|
153
|
+
expect(typeof handler).toBe('function');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('should support handler options', () => {
|
|
157
|
+
const handler = mockLambderApi<TestApiContract, 'test.echo'>(
|
|
158
|
+
'test.echo',
|
|
159
|
+
(input) => ({ echo: input.message }),
|
|
160
|
+
{
|
|
161
|
+
delay: 500,
|
|
162
|
+
apiPath: '/custom-api',
|
|
163
|
+
apiVersion: 'v1'
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
expect(handler).toBeDefined();
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* To enable these tests:
|
|
173
|
+
*
|
|
174
|
+
* 1. Install MSW:
|
|
175
|
+
* npm install --save-dev msw
|
|
176
|
+
*
|
|
177
|
+
* 2. Uncomment the MSW server setup lines above
|
|
178
|
+
*
|
|
179
|
+
* 3. Remove .skip from the test cases
|
|
180
|
+
*
|
|
181
|
+
* 4. Run tests:
|
|
182
|
+
* npm test
|
|
183
|
+
*/
|