From 7f56b5ddf30751f3b4ac225c899ce8f812db7ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rold=C3=A1n?= Date: Tue, 17 Dec 2024 13:45:56 +0100 Subject: [PATCH] Add tests for Graphql.ts --- .../cli-kit/src/private/node/request-ids.ts | 2 +- .../src/public/node/api/graphql.test.ts | 74 ++++++++++++++++--- .../cli-kit/src/public/node/api/graphql.ts | 8 +- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/packages/cli-kit/src/private/node/request-ids.ts b/packages/cli-kit/src/private/node/request-ids.ts index 6c54d0d8f57..e149e37b3ff 100644 --- a/packages/cli-kit/src/private/node/request-ids.ts +++ b/packages/cli-kit/src/private/node/request-ids.ts @@ -26,7 +26,7 @@ class RequestIDCollection { } /** - * Get all collected request IDs as a comma-separated string + * Get all collected request IDs */ getRequestIds(): string[] { return this.requestIds diff --git a/packages/cli-kit/src/public/node/api/graphql.test.ts b/packages/cli-kit/src/public/node/api/graphql.test.ts index 33db86c421e..969118b44f4 100644 --- a/packages/cli-kit/src/public/node/api/graphql.test.ts +++ b/packages/cli-kit/src/public/node/api/graphql.test.ts @@ -1,17 +1,26 @@ import {graphqlRequest, graphqlRequestDoc} from './graphql.js' -import {retryAwareRequest} from '../../../private/node/api.js' +import * as api from '../../../private/node/api.js' import * as debugRequest from '../../../private/node/api/graphql.js' import {buildHeaders} from '../../../private/node/api/headers.js' +import {requestIdsCollection} from '../../../private/node/request-ids.js' +import * as metadata from '../metadata.js' import {GraphQLClient} from 'graphql-request' import {test, vi, describe, expect, beforeEach} from 'vitest' -import {Headers} from 'node-fetch' import {TypedDocumentNode} from '@graphql-typed-document-node/core' -vi.mock('../../../private/node/api.js') +let mockedRequestId = 'request-id-123' + vi.mock('graphql-request', async () => { const actual = await vi.importActual('graphql-request') const client = vi.fn() - client.prototype.rawRequest = vi.fn() + client.prototype.rawRequest = () => { + return { + status: 200, + headers: new Headers({ + 'x-request-id': mockedRequestId, + }), + } + } return { ...(actual as object), @@ -20,20 +29,21 @@ vi.mock('graphql-request', async () => { }) vi.spyOn(debugRequest, 'debugLogRequestInfo').mockResolvedValue(undefined) -const mockedAddress = 'mockedAddress' +const mockedAddress = 'http://localhost:3000' const mockVariables = {some: 'variables'} const mockToken = 'token' const mockedAddedHeaders = {some: 'header'} beforeEach(async () => { - vi.mocked(retryAwareRequest).mockResolvedValue({ - status: 200, - headers: {} as Headers, - }) + requestIdsCollection.clear() }) describe('graphqlRequest', () => { test('calls debugLogRequestInfo once', async () => { + // Given + const retryAwareSpy = vi.spyOn(api, 'retryAwareRequest') + + // When await graphqlRequest({ query: 'query', api: 'mockApi', @@ -42,6 +52,8 @@ describe('graphqlRequest', () => { addedHeaders: mockedAddedHeaders, variables: mockVariables, }) + + // Then expect(GraphQLClient).toHaveBeenCalledWith(mockedAddress, { agent: expect.any(Object), headers: { @@ -54,12 +66,46 @@ describe('graphqlRequest', () => { request: expect.any(Function), url: mockedAddress, } - expect(retryAwareRequest).toHaveBeenCalledWith(receivedObject, expect.any(Function), undefined) + + expect(retryAwareSpy).toHaveBeenCalledWith(receivedObject, expect.any(Function), undefined) + }) + + test('Logs the request ids to metadata and requestIdCollection', async () => { + // Given + const metadataSpyOn = vi.spyOn(metadata, 'addPublicMetadata').mockImplementation(async () => {}) + + // When + await graphqlRequest({ + query: 'query', + api: 'mockApi', + url: mockedAddress, + token: mockToken, + addedHeaders: mockedAddedHeaders, + variables: mockVariables, + }) + + mockedRequestId = 'request-id-456' + + await graphqlRequest({ + query: 'query', + api: 'mockApi', + url: mockedAddress, + token: mockToken, + addedHeaders: mockedAddedHeaders, + variables: mockVariables, + }) + + // Then + expect(requestIdsCollection.getRequestIds()).toEqual(['request-id-123', 'request-id-456']) + expect(metadataSpyOn).toHaveBeenCalledTimes(2) + expect(metadataSpyOn.mock.calls[0]![0]()).toEqual({cmd_all_last_graphql_request_id: 'request-id-123'}) + expect(metadataSpyOn.mock.calls[1]![0]()).toEqual({cmd_all_last_graphql_request_id: 'request-id-456'}) }) }) describe('graphqlRequestDoc', () => { test('converts document before querying', async () => { + // Given const document = { kind: 'Document', definitions: [ @@ -80,6 +126,9 @@ describe('graphqlRequestDoc', () => { ], } as unknown as TypedDocumentNode + const retryAwareSpy = vi.spyOn(api, 'retryAwareRequest') + + // When await graphqlRequestDoc({ query: document, api: 'mockApi', @@ -89,7 +138,8 @@ describe('graphqlRequestDoc', () => { variables: mockVariables, }) - expect(retryAwareRequest).toHaveBeenCalledWith( + // Then + expect(retryAwareSpy).toHaveBeenCalledWith( { request: expect.any(Function), url: mockedAddress, @@ -102,7 +152,7 @@ describe('graphqlRequestDoc', () => { `query QueryName { example }`, - 'mockedAddress', + 'http://localhost:3000', mockVariables, expect.anything(), ) diff --git a/packages/cli-kit/src/public/node/api/graphql.ts b/packages/cli-kit/src/public/node/api/graphql.ts index 3ddccc680fb..e61eeeacc92 100644 --- a/packages/cli-kit/src/public/node/api/graphql.ts +++ b/packages/cli-kit/src/public/node/api/graphql.ts @@ -107,11 +107,9 @@ async function logLastRequestIdFromResponse(response: GraphQLResponse) try { const requestId = response.headers.get('x-request-id') requestIdsCollection.addRequestId(requestId) - await addPublicMetadata(async () => { - return { - cmd_all_last_graphql_request_id: requestId ?? undefined, - } - }) + await addPublicMetadata(() => ({ + cmd_all_last_graphql_request_id: requestId ?? undefined, + })) // eslint-disable-next-line no-catch-all/no-catch-all } catch { // no problem if unable to get request ID.