Julie Laursen
09/04/2024, 10:14 PMimport { mockCustomTaskCreateInput } from '@/__mocks__/generated/graphql';
import { CREATE_TASK } from '@/services/customTasks';
import { PactTestServerConfig } from '@/utils/pact-constants';
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';
import { GraphQLInteraction, Pact } from '@pact-foundation/pact';
import fetch from 'cross-fetch';
import * as path from 'path';
const mockVariables = { input: mockCustomTaskCreateInput() };
const EXPECTED_RESPONSE = mockVariables.input;
let client: ApolloClient<unknown>;
// Create a 'pact' between the two applications in the integration we are testing
const provider = new Pact({
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), './../pacts'),
consumer: 'company-app',
provider: 'custom-tasks',
port: PactTestServerConfig.Port,
});
beforeAll(async () => {
await provider.setup();
});
describe('custom task', () => {
beforeAll(() => {
client = new ApolloClient({
cache: new InMemoryCache({ addTypename: false }),
uri: `<http://127.0.0.1>:${PactTestServerConfig.Port}/graphql`,
link: new HttpLink({
uri: `<http://127.0.0.1>:${PactTestServerConfig.Port}/graphql`,
fetch,
}),
});
// Arrange: Setup our expected interactions
const graphqlMutation = new GraphQLInteraction()
.uponReceiving('a request to create a custom task')
.withOperation('CreateCustomTask')
//@ts-expect-error gql can be undefined
.withMutation(CREATE_TASK.loc.source.body)
.withRequest({
method: 'POST',
path: '/graphql',
headers: {
'Content-Type': 'application/json',
},
})
.withVariables(mockVariables)
.willRespondWith({
status: 200,
headers: { 'Content-Type': 'application/json' },
body: {
data: {
createCustomTask: EXPECTED_RESPONSE,
},
},
});
return provider.addInteraction(graphqlMutation);
});
afterAll(async () => {
await provider.verify();
await provider.finalize();
});
it('should retrieve a created task', async () => {
try {
const results = await client.mutate({
mutation: CREATE_TASK,
variables: mockVariables,
});
expect(results.data.createCustomTask).toEqual(EXPECTED_RESPONSE);
console.log('results', EXPECTED_RESPONSE);
} catch (error) {
console.log('error inside catch', error);
}
});
});
I'm also having a hell of time trying to get the env vars to point to test branches so i can play with the consumer side and see if the pact in the broker updates. Our docker commands are through make files and there's a long trail of following commands and short cuts to figure out what's going on, this is a huge monorepo.Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
.withMutation(CREATE_TASK.loc.source.body)
.withRequest({
method: 'POST',
path: '/graphql',
headers: {
'Content-Type': 'application/json',
},
})
It’s not clear what the contents of withMutation is here, so hard to say what it should be doing.Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
client = new ApolloClient({
cache: new InMemoryCache({ addTypename: false }),
uri: `<http://127.0.0.1>:${PactTestServerConfig.Port}/graphql`,
link: new HttpLink({
uri: `<http://127.0.0.1>:${PactTestServerConfig.Port}/graphql`,
fetch,
}),
});
Be careful with caches in these tests, they tend to cache things and then you get funny test results batting your head against them wondering why a single test always works, but not when you run multipleJulie Laursen
09/05/2024, 4:58 PM.withMutation(
`
mutation CreateCustomTask($input: CustomTaskCreateInput!) {
createCustomTask(input: $input) {
entityId
enabled
description
required
name
link
title
instructions
requireUpload
uploads {
id
filename
mimetype
encoding
url
}
}
}
`
)
Running that nowJulie Laursen
09/05/2024, 5:02 PM{
"operationName": "CreateCustomTask",
"query": "\n mutation CreateCustomTask($input: CustomTaskCreateInput!) {\n createCustomTask(input: $input) {\n entityId\n enabled\n description\n required\n name\n link\n title\n instructions\n requireUpload\n uploads {\n id\n filename\n mimetype\n encoding\n url\n }\n }\n }\n ",
"variables": {
"input": {
"description": "id",
"enabled": false,
"entityId": "9d29ce8e-d99f-4406-8d05-2bdc3d2bca3b",
"instructions": "cupiditate",
"link": "aliquid",
"linkText": "assumenda",
"name": "aliquid",
"requireUpload": true,
"required": true,
"title": "quia",
"uploads": [
{
"encoding": "eos",
"filename": "molestias",
"id": "voluptatem",
"mimetype": "sed",
"url": "quis"
},
{
"encoding": "eos",
"filename": "molestias",
"id": "voluptatem",
"mimetype": "sed",
"url": "quis"
}
]
}
}
}Julie Laursen
09/05/2024, 5:02 PM{
"description": "id",
"enabled": false,
"entityId": "9d29ce8e-d99f-4406-8d05-2bdc3d2bca3b",
"instructions": "cupiditate",
"link": "aliquid",
"linkText": "assumenda",
"name": "aliquid",
"requireUpload": true,
"required": true,
"title": "quia",
"uploads": [
{
"encoding": "eos",
"filename": "molestias",
"id": "voluptatem",
"mimetype": "sed",
"url": "quis"
},
{
"encoding": "eos",
"filename": "molestias",
"id": "voluptatem",
"mimetype": "sed",
"url": "quis"
}
]
}Julie Laursen
09/05/2024, 5:07 PMJulie Laursen
09/05/2024, 5:13 PM