Skip to content

Commit

Permalink
feat: reset consumer test state to enable re-use of PactV3 class
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jun 29, 2021
1 parent e8bc7be commit 0134ea8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 97 deletions.
72 changes: 8 additions & 64 deletions examples/v3/e2e/test/consumer.spec.js
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const expect = chai.expect;
const { PactV3, MatchersV3, XmlBuilder } = require('@pact-foundation/pact/v3');
const { PactV3, MatchersV3, XmlBuilder } = require('../../../../dist/v3');
const LOG_LEVEL = process.env.LOG_LEVEL || 'WARN';

chai.use(chaiAsPromised);
Expand Down Expand Up @@ -87,20 +87,20 @@ describe('Pact V3', () => {
availableAnimals,
} = require('../consumer');

const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
cors: true,
});

// Verify service client works as expected.
//
// Note that we don't call the consumer API endpoints directly, but
// use unit-style tests that test the collaborating function behaviour -
// we want to test the function that is calling the external service.
describe('when a call to list all animals from the Animal Service is made', () => {
describe('and the user is not authenticated', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
cors: true,
});

before(() =>
provider
.given('is not authenticated')
Expand All @@ -124,11 +124,6 @@ describe('Pact V3', () => {
describe('and the user is authenticated', () => {
describe('and there are animals in the database', () => {
it('returns a list of animals', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});
provider
.given('is authenticated')
.given('Has some animals')
Expand Down Expand Up @@ -160,12 +155,6 @@ describe('Pact V3', () => {
});

it('returns a filtered list of animals', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
port: 1234,
});
provider
.given('is authenticated')
.given('Has some animals')
Expand Down Expand Up @@ -214,11 +203,6 @@ describe('Pact V3', () => {
});
});
it('returns a filtered list of animals (query containing chinese characters)', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});
provider
.given('is authenticated')
.given('Has some animals')
Expand Down Expand Up @@ -269,11 +253,6 @@ describe('Pact V3', () => {
});
});
it('returns a filtered list of animals (query containing devanagari characters)', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});
provider
.given('is authenticated')
.given('Has some animals')
Expand Down Expand Up @@ -331,11 +310,6 @@ describe('Pact V3', () => {
describe('and there is an animal in the DB with ID 100', () => {
let responseBody = animalBodyExpectation;
responseBody.id = 100;
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});

before(() =>
provider
Expand Down Expand Up @@ -367,12 +341,6 @@ describe('Pact V3', () => {
});

describe('and there no animals in the database', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});

before(() =>
provider
.given('is authenticated')
Expand Down Expand Up @@ -400,12 +368,6 @@ describe('Pact V3', () => {

describe('when a call to the Animal Service is made to retrieve a single animal in text by ID', () => {
describe('and there is an animal in the DB with ID 100', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});

before(() =>
provider
.given('is authenticated')
Expand Down Expand Up @@ -445,12 +407,6 @@ describe('Pact V3', () => {
});

describe('when a call to the Animal Service is made to create a new mate', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});

before(() =>
provider
.given('is authenticated')
Expand Down Expand Up @@ -481,12 +437,6 @@ describe('Pact V3', () => {
});

describe('when a call to the Animal Service is made to create a new mate using form-data body', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});

before(() =>
provider
.given('is authenticated')
Expand Down Expand Up @@ -522,12 +472,6 @@ describe('Pact V3', () => {
});

describe('when a call to the Animal Service is made to get animals in XML format', () => {
const provider = new PactV3({
consumer: 'Matching Service V3',
provider: 'Animal Profile Service V3',
dir: path.resolve(process.cwd(), 'pacts'),
});

before(() =>
provider
.given('is authenticated')
Expand Down
Expand Up @@ -2,17 +2,20 @@ const path = require('path');
const transactionService = require('./transaction-service');
const { PactV3, MatchersV3, XmlBuilder } = require('@pact-foundation/pact/v3');
const { expect } = require('chai');
const { string, integer, url2, regex, datetime, fromProviderState } =
MatchersV3;
const {
string,
integer,
url2,
regex,
datetime,
fromProviderState,
} = MatchersV3;

describe('Transaction service - create a new transaction for an account', () => {
let provider;
beforeEach(() => {
provider = new PactV3({
consumer: 'TransactionService',
provider: 'AccountService',
dir: path.resolve(process.cwd(), 'pacts'),
});
const provider = new PactV3({
consumer: 'TransactionService',
provider: 'AccountService',
dir: path.resolve(process.cwd(), 'pacts'),
});

it('queries the account service for the account details', () => {
Expand Down
33 changes: 15 additions & 18 deletions examples/v3/todo-consumer/test/consumer.spec.js
Expand Up @@ -2,8 +2,15 @@ const path = require('path');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const { PactV3, MatchersV3, XmlBuilder } = require('@pact-foundation/pact/v3');
const { string, eachLike, integer, boolean, atLeastOneLike, timestamp, regex } =
MatchersV3;
const {
string,
eachLike,
integer,
boolean,
atLeastOneLike,
timestamp,
regex,
} = MatchersV3;

const TodoApp = require('../src/todo');

Expand All @@ -12,16 +19,16 @@ const expect = chai.expect;
chai.use(chaiAsPromised);

describe('Pact V3', () => {
const provider = new PactV3({
consumer: 'TodoApp',
provider: 'TodoServiceV3',
dir: path.resolve(process.cwd(), 'pacts'),
});

context('when there are a list of projects', () => {
describe('and there is a valid user session', () => {
describe('with JSON request', () => {
let provider;
before(() => {
provider = new PactV3({
consumer: 'TodoApp',
provider: 'TodoServiceV3',
dir: path.resolve(process.cwd(), 'pacts'),
});
provider
.given('i have a list of projects')
.uponReceiving('a request for projects')
Expand Down Expand Up @@ -72,11 +79,6 @@ describe('Pact V3', () => {

describe('with XML requests', () => {
before(() => {
provider = new PactV3({
consumer: 'TodoApp',
provider: 'TodoServiceV3',
dir: path.resolve(process.cwd(), 'pacts'),
});
provider
.given('i have a list of projects')
.uponReceiving('a request for projects in XML')
Expand Down Expand Up @@ -151,11 +153,6 @@ describe('Pact V3', () => {

describe('with image uploads', () => {
before(() => {
provider = new PactV3({
consumer: 'TodoApp',
provider: 'TodoServiceV3',
dir: path.resolve(process.cwd(), 'pacts'),
});
provider
.given('i have a project', { id: '1001', name: 'Home Chores' })
.uponReceiving('a request to store an image against the project')
Expand Down
20 changes: 14 additions & 6 deletions src/v3/pact.ts
Expand Up @@ -155,12 +155,7 @@ export class PactV3 {

constructor(opts: PactV3Options) {
this.opts = opts;
this.pact = new PactNative.Pact(
opts.consumer,
opts.provider,
pactPackageVersion,
omit(['consumer', 'provider', 'dir'], opts)
);
this.setup();
}

public given(providerState: string, parameters?: JsonMap): PactV3 {
Expand Down Expand Up @@ -272,9 +267,22 @@ export class PactV3 {
})
.finally(() => {
this.pact.shutdownTest(result);
this.setup();
});
}
this.pact.shutdownTest(result);
this.setup();
return Promise.reject(result.testError);
}

// reset the internal state
// (this.pact cannot be re-used between tests)
private setup() {
this.pact = new PactNative.Pact(
this.opts.consumer,
this.opts.provider,
pactPackageVersion,
omit(['consumer', 'provider', 'dir'], this.opts)
);
}
}

0 comments on commit 0134ea8

Please sign in to comment.