angular/angular · error · Error
Automatic conversion to ${format} is not supported for respo
Error message
Automatic conversion to ${format} is not supported for response type. What it means
Thrown by the HttpClient testing backend when the flushed body's runtime type matches none of string/number/object/boolean/array — i.e. `undefined`, a function, a symbol, or a bigint. The usual culprit is `flush(undefined)`: a variable that was never assigned. `null` is fine because `_maybeConvertBody` short-circuits it to a null body (with automatic status 204 when no status is given).
Source
Thrown at packages/common/http/testing/src/request.ts:207
| (boolean | string | number | Object | null)[],
format: string = 'JSON',
): Object | string | number | (Object | string | number)[] {
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
throw new Error(`Automatic conversion to ${format} is not supported for ArrayBuffers.`);
}
if (typeof Blob !== 'undefined' && body instanceof Blob) {
throw new Error(`Automatic conversion to ${format} is not supported for Blobs.`);
}
if (
typeof body === 'string' ||
typeof body === 'number' ||
typeof body === 'object' ||
typeof body === 'boolean' ||
Array.isArray(body)
) {
return body;
}
throw new Error(`Automatic conversion to ${format} is not supported for response type.`);
}
/**
* Helper function to convert a response body to a string.
*/
function _toTextBody(
body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[],
): string {
if (typeof body === 'string') {
return body;
}
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
throw new Error('Automatic conversion to text is not supported for ArrayBuffers.');
}
if (typeof Blob !== 'undefined' && body instanceof Blob) {
throw new Error('Automatic conversion to text is not supported for Blobs.');
}
return JSON.stringify(_toJsonBody(body, 'text'));View on GitHub (pinned to 51cb07e980)
Solutions
- Flush `null` for empty bodies: `req.flush(body ?? null)` — null is valid and yields a 204
- Flush a concrete empty representation (empty string, `{}`, `[]`) if the code under test expects a body
- Check the fixture: make sure the variable passed to flush() is actually assigned
Example fix
// before const body = response?.body; // undefined req.flush(body); // throws // after req.flush(body ?? null); // null body, status 204
Defensive patterns
Strategy: validation
Validate before calling
// Guard against accidentally flushing undefined
if (body === undefined) {
throw new Error('flush(undefined) is not supported — use null for an empty body');
}
req.flush(body ?? null); Type guard
const isFlushableBody = (b: unknown): b is string | number | boolean | object | null => b === null || !['undefined', 'function', 'symbol', 'bigint'].includes(typeof b);
Prevention
- Normalize with `req.flush(body ?? null)` when the body is conditionally populated
- Type mock fixtures explicitly (body: MyDto | null) so undefined cannot sneak in
- Flush concrete empty representations ({}, '', []) when the code under test expects a body
When it happens
Trigger: `req.flush(undefined)` in a spec (e.g. `req.flush(response.body)` where `response.body` was never set); flushing a function or bigint value into a json/text request.
Common situations: Optional fixture fields left unset; a helper that returns undefined for an empty payload; refactoring mocks so the body variable is conditionally populated.
Related errors
- Automatic conversion to ArrayBuffer is not supported for res
- Blob responses are not supported on this platform.
- Automatic conversion to Blob is not supported for response t
- Automatic conversion to ${format} is not supported for Array
- Automatic conversion to ${format} is not supported for Blobs
AI-assisted analysis of angular/angular@51cb07e980 (2026-08-22).
Data as JSON: /api/errors/ac2a3c9c6bf1c44c.
Report an issue: GitHub.