Skip to content

Commit d9126f0

Browse files
fix: improve FormData mock with properly typed Proxy implementation
1 parent eef4e5a commit d9126f0

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

tests/resources/drafts.spec.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jest.mock('formdata-node', () => {
1414
const getAppendedData = () => appendedData;
1515

1616
const createMockFormData = () => {
17-
const instance = {
17+
const mockInstance: MockedFormData = {
1818
append(key: string, value: any): void {
1919
if (value && typeof value === 'object' && 'content' in value) {
2020
// Handle File objects
@@ -24,16 +24,23 @@ jest.mock('formdata-node', () => {
2424
}
2525
},
2626
_getAppendedData: getAppendedData,
27+
form: null as any, // Will be set by proxy
2728
};
2829

29-
Object.defineProperty(instance, 'form', {
30-
get() {
31-
return instance;
32-
},
33-
enumerable: true,
30+
// Create a proxy to ensure all properties are available on both the instance and form
31+
const proxy: MockedFormData = new Proxy(mockInstance, {
32+
get(target: MockedFormData, prop: string | symbol): any {
33+
if (prop === 'form') {
34+
return proxy;
35+
}
36+
return target[prop as keyof MockedFormData];
37+
}
3438
});
3539

36-
return instance;
40+
// Set the form property to point to the proxy itself
41+
mockInstance.form = proxy;
42+
43+
return proxy;
3744
};
3845

3946
const mockFormData = createMockFormData() as MockedFormData;

0 commit comments

Comments
 (0)