Connectum API Reference / @connectum/test-fixtures / index / createMockDescField
Function: createMockDescField() ​
createMockDescField(
name,options?):DescField
Defined in: mock-desc.ts:153
Create a mock DescField descriptor.
Produces a minimal object that satisfies the DescField shape expected by ConnectRPC interceptors and protobuf utilities.
The field is a proto3 singular scalar field with implicit presence: like a compiled descriptor, getDefaultValue() returns undefined (so create() fills in the scalar zero value) and longAsString is false.
With isSensitive: true, proto.options is a real google.protobuf.FieldOptions message with debugRedact set to true, as in a compiled descriptor; without it proto.options is undefined.
Limit: a field created here alone has parent undefined. Fields listed by createMockDescMessage are linked to their message through parent, which the protobuf-es helpers that read field.parent (toBinary, isFieldSet, equals, clone) need; declare the field through that factory when a test passes the descriptor to them.
Parameters ​
name ​
string
The field's name as written in the .proto file (for example user_id). localName and jsonName are derived from it by the protobuf-es rules: user_id gives userId, and a name that collides with a built-in object property (constructor, toString, toJSON, valueOf) gets a $ suffix in localName. A name that is already camelCase is kept unchanged. A custom json_name option cannot be expressed.
options? ​
Optional overrides for field number, scalar type, and sensitivity.
Returns ​
A mock DescField object.
Throws ​
TypeError when options.type is not one of the protobuf scalar type names (double, float, int64, uint64, int32, fixed64, fixed32, bool, string, bytes, uint32, sfixed32, sfixed64, sint32, sint64); "message" and "enum" are field kinds, not scalar types. The message names the field and the value.
Throws ​
RangeError when options.fieldNumber is not an integer from 1 to 536870911 outside the reserved range 19000 to 19999.
Example ​
import { createMockDescField } from "@connectum/testing";
const field = createMockDescField("user_id", { type: "int32", fieldNumber: 1 });
// field.name === "user_id"
// field.localName === "userId"
// field.scalar === 5 (INT32)