diff --git a/packages/api/test/mutations.test.ts b/packages/api/test/mutations.test.ts index 74c429e59e..c305959722 100644 --- a/packages/api/test/mutations.test.ts +++ b/packages/api/test/mutations.test.ts @@ -1,5 +1,4 @@ import { execute, parse } from 'graphql' -import type { GraphQLSchema } from 'graphql' import { checkoutOrderFormCustomDataInvalidFetch, @@ -30,11 +29,20 @@ const apiOptions = { }, } as Options -let schema: GraphQLSchema -let context: Record -const contextFactory = getContextFactory(apiOptions) const mockedFetch = jest.fn() +const createRunner = () => { + const schemaPromise = getSchema(apiOptions) + const contextFactory = getContextFactory(apiOptions) + + return async (query: string, variables?: any) => { + const schema = await schemaPromise + const context = contextFactory({}) + + return execute(schema, parse(query), null, context, variables) + } +} + function pickFetchAPICallResult( info: RequestInfo, _: RequestInit | undefined, @@ -56,15 +64,12 @@ jest.mock('../src/platforms/vtex/clients/fetch.ts', () => ({ mockedFetch(info, init), })) -beforeAll(async () => { - schema = await getSchema(apiOptions) -}) +const run = createRunner() // Always clear the mocked fetch before each test so we can count and validate // the calls performed by each query independently. beforeEach(() => { mockedFetch.mockClear() - context = contextFactory({}) }) test('`validateCart` mutation should return `null` when a valid cart is passed', async () => { @@ -78,13 +83,7 @@ test('`validateCart` mutation should return `null` when a valid cart is passed', pickFetchAPICallResult(info, init, fetchAPICalls) ) - const response = await execute( - schema, - parse(ValidateCartMutation), - null, - context, - { cart: ValidCart } - ) + const response = await run(ValidateCartMutation, { cart: ValidCart }) expect(mockedFetch).toHaveBeenCalledTimes(3) @@ -110,13 +109,7 @@ test('`validateCart` mutation should return the full order when an invalid cart pickFetchAPICallResult(info, init, fetchAPICalls) ) - const response = await execute( - schema, - parse(ValidateCartMutation), - null, - context, - { cart: InvalidCart } - ) + const response = await run(ValidateCartMutation, { cart: InvalidCart }) expect(mockedFetch).toHaveBeenCalledTimes(4) @@ -142,13 +135,7 @@ test('`validateCart` mutation should return new cart when etag is stale', async pickFetchAPICallResult(info, init, fetchAPICalls) ) - const response = await execute( - schema, - parse(ValidateCartMutation), - null, - context, - { cart: InvalidCart } - ) + const response = await run(ValidateCartMutation, { cart: InvalidCart }) expect(mockedFetch).toHaveBeenCalledTimes(3) diff --git a/packages/api/test/queries.test.ts b/packages/api/test/queries.test.ts index 71570f7283..fd14f971fd 100644 --- a/packages/api/test/queries.test.ts +++ b/packages/api/test/queries.test.ts @@ -1,4 +1,3 @@ -import type { GraphQLSchema } from 'graphql' import { execute, parse } from 'graphql' import type { Options } from '../src' @@ -42,11 +41,20 @@ const apiOptions = { }, } as Options -let schema: GraphQLSchema -let context: Record -const contextFactory = getContextFactory(apiOptions) const mockedFetch = jest.fn() +const createRunner = () => { + const schemaPromise = getSchema(apiOptions) + const contextFactory = getContextFactory(apiOptions) + + return async (query: string, variables?: any) => { + const schema = await schemaPromise + const context = contextFactory({}) + + return execute(schema, parse(query), null, context, variables) + } +} + function pickFetchAPICallResult( info: RequestInfo, _: RequestInit | undefined, @@ -67,15 +75,12 @@ jest.mock('../src/platforms/vtex/clients/fetch.ts', () => ({ fetchAPI: (info: RequestInfo, init?: RequestInit) => mockedFetch(info, init), })) -beforeAll(async () => { - schema = await getSchema(apiOptions) -}) +const run = createRunner() // Always clear the mocked fetch before each test so we can count and validate // the calls performed by each query independently. beforeEach(() => { mockedFetch.mockClear() - context = contextFactory({}) }) test('`collection` query', async () => { @@ -89,12 +94,7 @@ test('`collection` query', async () => { pickFetchAPICallResult(info, init, fetchAPICalls) ) - const response = await execute( - schema, - parse(CollectionDesksQuery), - null, - context - ) + const response = await run(CollectionDesksQuery) expect(mockedFetch).toHaveBeenCalledTimes(3) @@ -115,7 +115,7 @@ test('`product` query', async () => { pickFetchAPICallResult(info, init, fetchAPICalls) ) - const response = await execute(schema, parse(ProductByIdQuery), null, context) + const response = await run(ProductByIdQuery) expect(mockedFetch).toHaveBeenCalledTimes(1) @@ -144,12 +144,7 @@ test('`allCollections` query', async () => { pickFetchAPICallResult(info, init, fetchAPICalls) ) - const response = await execute( - schema, - parse(AllCollectionsQueryFirst5), - null, - context - ) + const response = await run(AllCollectionsQueryFirst5) expect(mockedFetch).toHaveBeenCalledTimes(7) @@ -170,12 +165,7 @@ test('`allProducts` query', async () => { pickFetchAPICallResult(info, init, fetchAPICalls) ) - const response = await execute( - schema, - parse(AllProductsQueryFirst5), - null, - context - ) + const response = await run(AllProductsQueryFirst5) expect(mockedFetch).toHaveBeenCalledTimes(1) @@ -199,12 +189,7 @@ test('`search` query', async () => { pickFetchAPICallResult(info, init, fetchAPICalls) ) - const response = await execute( - schema, - parse(SearchQueryFirst5Products), - null, - context - ) + const response = await run(SearchQueryFirst5Products) expect(mockedFetch).toHaveBeenCalledTimes(2)