Skip to content

Commit b516f17

Browse files
committed
fix(mock): rewrite ref-alias return types to ArrayBuffer for binary responses
1 parent 78e5ee6 commit b516f17

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

packages/mock/src/msw/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ describe('generateMSW', () => {
350350
'HttpResponse.arrayBuffer',
351351
);
352352
expect(result.implementation.handler).not.toContain('JSON.stringify');
353+
// The ref alias return type must be rewritten to ArrayBuffer to stay consistent with the arrayBuffer body.
354+
expect(result.implementation.function).toContain(': ArrayBuffer');
355+
expect(result.implementation.function).not.toContain(': TestPdfFile');
353356
});
354357

355358
it('should not force binary path when preferredContentType narrows to a non-binary success variant', () => {

packages/mock/src/msw/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,18 @@ function generateDefinition(
151151
const isTextResponse =
152152
(isExactlyStringReturnType && hasTextLikeContentType) ||
153153
contentTypesByPreference.some((ct) => isTextLikeContentType(ct));
154+
const isSchemaBinary = (r: ResReqTypesValue) =>
155+
r.originalSchema?.format === 'binary' ||
156+
(r.originalSchema?.contentMediaType === 'application/octet-stream' &&
157+
!r.originalSchema.contentEncoding);
154158
const isBinaryResponse =
155159
contentTypesByPreference.some((ct) => isBinaryLikeContentType(ct)) ||
156-
responsesByPreference.some(
157-
(r) =>
158-
r.originalSchema?.format === 'binary' ||
159-
(r.originalSchema?.contentMediaType === 'application/octet-stream' &&
160-
!r.originalSchema.contentEncoding),
161-
);
160+
responsesByPreference.some((r) => isSchemaBinary(r));
161+
// Ref aliases of schema-binary responses; rewritten to `ArrayBuffer` alongside literal `Blob`.
162+
const binaryAliasTypeNames = responsesByPreference
163+
.filter((r) => isSchemaBinary(r))
164+
.map((r) => r.value)
165+
.filter((name) => name !== 'Blob');
162166
const isReturnHttpResponse = value && value !== 'undefined';
163167

164168
const getResponseMockFunctionName = `${getResponseMockFunctionNameBase}${pascal(
@@ -175,8 +179,15 @@ function generateDefinition(
175179
? `${addedSplitMockImplementations.join('\n\n')}\n\n`
176180
: '';
177181

182+
const binaryTypePattern = ['Blob', ...binaryAliasTypeNames]
183+
.map((n) => escapeRegExp(n))
184+
.join('|');
185+
const binaryTypeRewriteRegex = new RegExp(
186+
String.raw`\b(?:${binaryTypePattern})\b`,
187+
'g',
188+
);
178189
const mockReturnType = isBinaryResponse
179-
? returnType.replaceAll(/\bBlob\b/g, 'ArrayBuffer')
190+
? returnType.replaceAll(binaryTypeRewriteRegex, 'ArrayBuffer')
180191
: returnType;
181192

182193
// Detect when the return type is a union containing void (e.g. "Resource | void"

0 commit comments

Comments
 (0)