Joel Whalen
04/24/2024, 1:20 PMAttempted to log "[Network error]: FetchError: request to <http://127.0.0.1:4000/graphql> failed, reason: connect ECONNREFUSED 127.0.0.1:4000".
and it was because I wasn't using async/await on the provider.setup() method for initializing the pact mockserver. I'm at a loss now though because my code looks correct; the server is definitely initializing before my tests. And I've checked my running node processes, there's nothing running at the port 4000. I've put my pact setup in an async beforeAll hook:
beforeAll(async () => await provider.setup());
The verification failed because the connection to the mockserver was refused 😕
ApolloClientPact is configured like this if it helps:
global.fetch = fetch;
export const ApolloClientPact: FC = ({ children }) => {
const { session } = useSession();
const client = makeClient({
url: '<http://127.0.0.1:4000/graphql>',
name: 'provider-app',
requestHeaders: () => ({ ...config, ...session }),
});
return <ApolloProvider client={client}>{children}</ApolloProvider>;
};
is this because I'm making the request from a component render function instead of calling the query from the client directly myself?
(useAddress as jest.Mock).mockReturnValue(mockRequest),
render(
<ApolloClientPact>
<MicrositeContext.Provider value={mockContextValue}>
<ThemeProvider>
<AddressModal />
</ThemeProvider>
</MicrositeContext.Provider>
</ApolloClientPact>
);
expect(screen.getByTestId('address-form'));Joel Whalen
04/24/2024, 3:35 PMJoel Whalen
04/24/2024, 5:42 PMJoel Whalen
04/24/2024, 5:43 PMlog: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),Tim Vahlbrock
04/25/2024, 9:20 AMJoel Whalen
04/25/2024, 3:00 PMJoel Whalen
05/02/2024, 3:00 PMrender(
<ApolloClientPact>
<MockComponent />
</ApolloClientPact>
);
// Assert that the loading state is initially displayed
expect(screen.getByText('Loading...')).toBeInTheDocument();
// Wait for the data to be loaded
await waitFor(() => screen.getByText('Data loaded'));
// Assert on the response data
expect(screen.getByText(/Data loaded/)).toBeInTheDocument();
expect(
screen.getByText(/"street":"5413 Dickens Glen Ln"/)
).toBeInTheDocument();
• This approach unit tests the actual query code
• I don't have to render the actual component under test since it doesn't have the response data I need to assert on
• Pact verify() call passes and the mockserver works as expected
Slightly different approach from doing the client.query() method as detailed in the pact js examples, so I figured it's worth sharing 🙂Joel Whalen
05/02/2024, 3:01 PMMockComponent looks like this in my test:
const MockComponent = () => {
const { data, loading } = useNormalizedAddressQuery(mockRequest);
if (loading) {
return <p>Loading...</p>;
}
return (
<div>
<p>Data loaded</p>
<p>{JSON.stringify(data)}</p>
</div>
);
};Matt (pactflow.io / pact-js / pact-go)
Jakob Jan Kamminga
05/06/2024, 8:55 AM__typename to the request before sending, and the matcher didn’t seem to be able to handle that. Any solutions are welcome, but I did find a solution.
I ended up just skipping Apollo altogether and just calling node fetch directly:
const request = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: print(mock.query),
variables: mock.variables,
operationName: 'Search',
}),
}
const searchResult: Response = await fetch(uri, request)
.then((res) => res)
.catch((error) => { throw new Error(error) });
it('returns results', async () => {
const data = searchResult.json();
...Matt (pactflow.io / pact-js / pact-go)
__typename issue. There is https://github.com/pact-foundation/pact-js/blob/master/src/dsl/apolloGraphql.ts but that doesn’t account for itMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Joel Whalen
05/06/2024, 4:37 PM__typename to your mock and it should workJakob Jan Kamminga
05/08/2024, 10:44 AM__typename, so that would add a lot of maintenance effort.
Is that a best practice? Maybe not, but it’s the pre-existing situation I have to work with.
It does seem to work fine with just node fetch though, so I’m sticking with that for now.Matt (pactflow.io / pact-js / pact-go)