# Introduction Followup of https://github.com/twentyhq/twenty/pull/15331 ( Reducing size by concerns ) This PR centralizes v2 format error types in `twenty-shared` and consuming them in the existing v2 error format logic in `twenty-server` ## Next This https://github.com/twentyhq/twenty/pull/15360 handles the frontend v2 format error refactor ## Conclusion Related to https://github.com/twentyhq/core-team-issues/issues/1776
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { extractRecordIdsAndDatesAsExpectAny } from 'test/utils/extract-record-ids-and-dates-as-expect-any';
|
|
import { jestExpectToBeDefined } from 'test/utils/jest-expect-to-be-defined.util.test';
|
|
|
|
import {
|
|
type BaseGraphQLError,
|
|
type ErrorCode,
|
|
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
|
|
|
|
export interface GraphQLResponse<T extends Record<string, unknown>> {
|
|
status: number;
|
|
body: {
|
|
data?: T;
|
|
errors?: BaseGraphQLError[];
|
|
};
|
|
}
|
|
|
|
export const assertGraphQLSuccessfulResponse = <
|
|
T extends Record<string, unknown>,
|
|
>(
|
|
response: GraphQLResponse<T>,
|
|
expectedData?: Partial<T>,
|
|
) => {
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.data).toBeDefined();
|
|
expect(response.body.errors).toBeUndefined();
|
|
|
|
if (expectedData) {
|
|
expect(response.body.data).toMatchObject(expectedData);
|
|
}
|
|
};
|
|
|
|
export const assertGraphQLErrorResponse = <T extends Record<string, unknown>>(
|
|
response: GraphQLResponse<T>,
|
|
expectedErrorCode: ErrorCode,
|
|
expectedErrorMessage?: string,
|
|
) => {
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.errors).toBeDefined();
|
|
expect(response.body.errors).toHaveLength(1);
|
|
|
|
if (expectedErrorCode && response.body.errors) {
|
|
expect(response.body.errors[0].extensions?.code).toBe(expectedErrorCode);
|
|
}
|
|
|
|
if (expectedErrorMessage && response.body.errors) {
|
|
expect(response.body.errors[0].message).toBe(expectedErrorMessage);
|
|
}
|
|
};
|
|
|
|
export const assertGraphQLErrorResponseWithSnapshot = <
|
|
T extends Record<string, unknown>,
|
|
>(
|
|
response: GraphQLResponse<T>,
|
|
) => {
|
|
expect(response.status).toBe(200);
|
|
jestExpectToBeDefined(response.body.errors);
|
|
expect(response.body.errors).toHaveLength(1);
|
|
|
|
const firstError = response.body.errors[0];
|
|
|
|
jestExpectToBeDefined(firstError);
|
|
expect(firstError).toMatchSnapshot(
|
|
extractRecordIdsAndDatesAsExpectAny(firstError),
|
|
);
|
|
};
|