Skip to content

Instantly share code, notes, and snippets.

@lwillmeth
Created October 22, 2019 20:36
Show Gist options
  • Save lwillmeth/9fb7163fd729cd22e95985bbad79c6df to your computer and use it in GitHub Desktop.
Save lwillmeth/9fb7163fd729cd22e95985bbad79c6df to your computer and use it in GitHub Desktop.
const { expect } = require('chai');
const { after, before, describe, it } = require('mocha');
const path = require('path');
const { Pact, Matchers, ApolloGraphQLInteraction } = require('@pact-foundation/pact');
const gqlRequests = require('../lib/graphql_requests');
const ContractTestBrokerClient = require('../lib/index');
const API_PORT = process.env.API_PORT || 9123;
const CONSUMER = 'lib-contract-test-broker-client-nodejs';
const PROVIDER = 'lambda-contract-test-broker';
// Set up a mock server at localhost:port
const provider = new Pact({
port: API_PORT,
log: path.resolve(process.cwd(), 'test/contracts', 'consumer-contract.log'),
dir: path.resolve(process.cwd(), 'test/contracts'),
consumer: CONSUMER,
provider: PROVIDER,
pactfileWriteMode: 'overwrite' // overwrite | update | merge
});
// Send graphql requests to the pact mock server running at localhost
const brokerClient = new ContractTestBrokerClient(`http://localhost:${API_PORT}/graphql`);
before(() => provider.setup());
after(() => {
provider.verify();
provider.finalize();
});
describe(`Contract with ${provider.opts.provider}`, () => {
describe('Query.info', () => {
it('should respond with status: 200', async () => {
const gqlInteraction = new ApolloGraphQLInteraction()
.uponReceiving('a query.info request')
.withRequest({
path: '/graphql',
method: 'POST'
})
.withQuery(gqlRequests.queries.info)
.willRespondWith({
status: 200,
body: {
data: {
info: {
name: PROVIDER,
version: Matchers.like('1.2.3'),
__typename: 'HealthResponse'
}
},
loading: false,
networkStatus: 7, // 7 = OK
stale: false
}
});
await provider.addInteraction(gqlInteraction);
const response = await brokerClient.Query.info();
expect(response.name).to.eql(PROVIDER);
// provider.verify();
});
});
describe('Query.contractById', () => {
it('should return a pending contract from service-foo', async () => {
const args = {
provider: 'service-foo',
contractId: '5dfeef13e943367003570f0bdbca0648972d7afc'
};
const gqlInteraction = new ApolloGraphQLInteraction()
.uponReceiving('a query.contractId request for service-foo')
.withRequest({
path: '/graphql',
method: 'POST'
})
.withQuery(gqlRequests.queries.contractById)
.withVariables(args)
.willRespondWith({
// these values are from the Provider's fixture file
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: {
data: {
contractById: {
contractId: '5dfeef13e943367003570f0bdbca0648972d7afc',
provider: 'service-foo',
consumer: 'lib-foobar1',
createdAt: Matchers.like('2019-10-15T21:07:29Z'),
updatedAt: Matchers.like('2019-10-15T21:07:29Z'),
resolvedAt: null,
activityState: 'active',
contractStatus: 'pending',
interactions: [{
providerState: null,
description: 'a GET /health request',
__typename: 'ContractInteraction'
}],
metadata: null,
__typename: 'Contract'
}
}
}
});
await provider.addInteraction(gqlInteraction);
const response = await brokerClient.Query.contractById(args);
// the brokerClient function returns body.data.contractById
expect(response.provider).to.eql(args.provider);
// provider.verify();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment