fix tests ugh
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 5m53s

This commit is contained in:
2025-12-09 18:48:17 -08:00
parent 4674309ea1
commit 23e0c44b61
10 changed files with 89 additions and 79 deletions

View File

@@ -48,22 +48,16 @@ const server = setupServer(
}
} else if (contentType?.includes('multipart/form-data')) {
body = await request.formData();
// FIX: When MSW processes FormData, the file's 'name' property is lost in JSDOM.
// To fix the tests, we manually reconstruct a File-like object with the name
// from the headers for our spy. This makes the test assertions pass.
const fileEntry = Array.from((body as FormData).entries()).find(
([key, value]) => value instanceof File
);
if (fileEntry) {
const [key, file] = fileEntry as [string, File];
// Create a new object that looks like a File for the spy
(body as FormData).set(key, {
name: file.name, // JSDOM preserves the name on the original File object
size: file.size,
type: file.type,
// The test doesn't need the content, so we can omit it.
} as any);
}
// JSDOM's FormData processing loses the filename, replacing it with 'blob'.
// To work around this for our tests, we find the file entry in the FormData,
// and if it's a File object, we manually add its original `name` property
// to the object that will be spied on. This allows our assertions to pass.
(body as FormData).forEach((value, key) => {
if (value instanceof File) {
// Augment the File/Blob object with its original name for the spy.
(value as File & { originalName?: string }).originalName = value.name;
}
});
}
requestSpy({
@@ -113,10 +107,10 @@ describe('AI API Client (Network Mocking with MSW)', () => {
// FIX: Use a duck-typing check for FormData to avoid environment-specific instance issues.
expect(typeof (req.body as FormData).get).toBe('function');
const flyerFile = (req.body as FormData).get('flyerFile') as { name: string };
const flyerFile = (req.body as FormData).get('flyerFile') as File & { originalName: string };
const checksumValue = (req.body as FormData).get('checksum');
expect(flyerFile.name).toBe('flyer.pdf');
expect(flyerFile.originalName).toBe('flyer.pdf');
expect(checksumValue).toBe(checksum);
});
});
@@ -146,8 +140,8 @@ describe('AI API Client (Network Mocking with MSW)', () => {
expect(req.endpoint).toBe('check-flyer');
expect(req.method).toBe('POST');
expect(typeof (req.body as FormData).get).toBe('function');
const imageFile = (req.body as FormData).get('image') as { name: string };
expect(imageFile.name).toBe('flyer.jpg');
const imageFile = (req.body as FormData).get('image') as File & { originalName: string };
expect(imageFile.originalName).toBe('flyer.jpg');
});
});
@@ -161,8 +155,8 @@ describe('AI API Client (Network Mocking with MSW)', () => {
expect(req.endpoint).toBe('extract-address');
expect(typeof (req.body as FormData).get).toBe('function');
const imageFile = (req.body as FormData).get('image') as { name: string };
expect(imageFile.name).toBe('flyer.jpg');
const imageFile = (req.body as FormData).get('image') as File & { originalName: string };
expect(imageFile.originalName).toBe('flyer.jpg');
});
});
@@ -176,8 +170,8 @@ describe('AI API Client (Network Mocking with MSW)', () => {
expect(req.endpoint).toBe('extract-logo');
expect(typeof (req.body as FormData).get).toBe('function');
const imageFile = (req.body as FormData).get('images') as { name: string };
expect(imageFile.name).toBe('logo.jpg');
const imageFile = (req.body as FormData).get('images') as File & { originalName: string };
expect(imageFile.originalName).toBe('logo.jpg');
});
});
@@ -276,11 +270,11 @@ describe('AI API Client (Network Mocking with MSW)', () => {
expect(req.endpoint).toBe('rescan-area');
expect(typeof (req.body as FormData).get).toBe('function');
const imageFile = (req.body as FormData).get('image') as { name: string };
const imageFile = (req.body as FormData).get('image') as File & { originalName: string };
const cropAreaValue = (req.body as FormData).get('cropArea');
const extractionTypeValue = (req.body as FormData).get('extractionType');
expect(imageFile.name).toBe('flyer-page.jpg');
expect(imageFile.originalName).toBe('flyer-page.jpg');
expect(cropAreaValue).toBe(JSON.stringify(cropArea));
expect(extractionTypeValue).toBe(extractionType);
});