Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support GraphQL for PactV3 and PactV4 #1228

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ jobs:
run: cd examples/v4/plugins && npm install --ignore-scripts && npm test
- name: example_v4_matchers
run: cd examples/v4/matchers && npm install --ignore-scripts && npm test
- name: example_v4_graphql
run: cd examples/v4/graphql && npm install --ignore-scripts && npm test
# - name: Upload dist folder
# if: runner.os == 'Linux'
# uses: actions/upload-artifact@v3
Expand Down
30 changes: 10 additions & 20 deletions examples/graphql/src/consumer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import * as chai from 'chai';
import * as path from 'path';
import * as chaiAsPromised from 'chai-as-promised';
import { query } from './consumer';
import {
Pact,
GraphQLInteraction,
Matchers,
LogLevel,
} from '@pact-foundation/pact';
import { Matchers, LogLevel, GraphQLPactV3 } from '@pact-foundation/pact';
const { like } = Matchers;
const LOG_LEVEL = process.env.LOG_LEVEL || 'TRACE';

Expand All @@ -17,21 +12,18 @@ const expect = chai.expect;
chai.use(chaiAsPromised);

describe('GraphQL example', () => {
const provider = new Pact({
const provider = new GraphQLPactV3({
port: 4000,
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'pacts'),
consumer: 'GraphQLConsumer',
provider: 'GraphQLProvider',
logLevel: LOG_LEVEL as LogLevel,
});

before(() => provider.setup());
after(() => provider.finalize());

describe('query hello on /graphql', () => {
describe('When the "hello" query on /graphql is made', () => {
before(() => {
const graphqlQuery = new GraphQLInteraction()
provider
.given('the world exists')
.uponReceiving('a hello request')
.withQuery(
`
Expand Down Expand Up @@ -59,16 +51,14 @@ describe('GraphQL example', () => {
},
},
});
return provider.addInteraction(graphqlQuery);
});

it('returns the correct response', () => {
return expect(query()).to.eventually.deep.equal({
hello: 'Hello world!',
it('returns the correct response', async () => {
await provider.executeTest(async () => {
return expect(query()).to.eventually.deep.equal({
hello: 'Hello world!',
});
});
});

// verify with Pact, and reset expectations
afterEach(() => provider.verify());
});
});
2 changes: 2 additions & 0 deletions examples/v4/graphql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/*.js
src/*.d.ts
8 changes: 8 additions & 0 deletions examples/v4/graphql/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bail": true,
"reporter": "spec",
"colors": true,
"timeout": 60000,
"exit": true,
"require": ["ts-node/register", "source-map-support/register"]
}
10 changes: 10 additions & 0 deletions examples/v4/graphql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# GraphQL + Pact

Demonstrates how to do Pact testing against a GraphQL endpoint. This is a POC, demonstrating how PactJS could provide a simple helper function that wraps the GraphQL request into a basic Interaction suitable for Pact (after all, GraphQL is simply an interface over HTTP).

Test it out here:

```
npm run test:consumer
npm run test:provider
```
Loading
Loading