Skip to content

Commit

Permalink
feat(dsl): Add pactWith.only and pactWith.skip, which behave like the…
Browse files Browse the repository at this point in the history
…ir describe counterparts
  • Loading branch information
TimothyJones committed Feb 23, 2021
1 parent 19c0b40 commit 540dc3c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ Jest-Pact has three functions:
- `xpactWith(JestPactOptions, (providerMock) => { /* tests go here */ })`: Like `xdescribe` in Jest, this skips the pact tests described within.
- `fpactWith(JestPactOptions, (providerMock) => { /* tests go here */ })`: Like `fdescribe` in Jest, this sets this test suite to only run this test.
Additionally, `pactWith.only` and `pactWith.skip` behave as you would expect from Jest.
There are two types exported:
- `JestProvidedPactFn`: This is the type of the second argument to `pactWith`, ie: `(provider: Pact) => void`
Expand Down
29 changes: 20 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,26 @@ const jestPactWrapper = (
const describeString = (options: JestPactOptions) =>
`Pact between ${options.consumer} and ${options.provider}`;

export const pactWith = (options: JestPactOptions, tests: JestProvidedPactFn) =>
describe(describeString(options), () => jestPactWrapper(options, tests));

export const xpactWith = (
type PactWithFn = (options: JestPactOptions, tests: JestProvidedPactFn) => void;
interface PactWith {
(options: JestPactOptions, tests: JestProvidedPactFn): void;
only: PactWithFn;
skip: PactWithFn;
}

const describePactWith = (describeFn: jest.Describe): PactWithFn => (
options: JestPactOptions,
tests: JestProvidedPactFn,
) => xdescribe(describeString(options), () => jestPactWrapper(options, tests));
) => describeFn(describeString(options), () => jestPactWrapper(options, tests));

export const fpactWith = (
options: JestPactOptions,
tests: JestProvidedPactFn,
) => fdescribe(describeString(options), () => jestPactWrapper(options, tests));
const extend = (pactWithfn: PactWithFn) => {
const ret = pactWithfn as PactWith;
ret.only = describePactWith(describe.only);
ret.skip = describePactWith(describe.skip);
return ret;
};

export const pactWith = extend(describePactWith(describe));

export const xpactWith = pactWith.skip;
export const fpactWith = pactWith.only;
15 changes: 15 additions & 0 deletions src/test/pactwith.only.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Pact } from '@pact-foundation/pact';
import { pactWith } from '../index';

describe('pactwith.only', () => {
pactWith.only(
{ consumer: 'MyConsumer', provider: 'NoProvider' },
(provider: Pact) => {
it('should only run this test', () => {});
},
);

test('the test that should be skipped', () => {
throw new Error('this test should not be run');
});
});
14 changes: 14 additions & 0 deletions src/test/pactwith.skip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Pact } from '@pact-foundation/pact';
import { pactWith } from '../index';

describe('pactwith.skip', () => {
pactWith.skip(
{ consumer: 'MyConsumer', provider: 'NoOtherProvider' },
(provider: Pact) => {
test('the test that should be skipped', () => {
throw new Error('tests inside xpactWith should not run');
});
},
);
test('this test should run', () => {});
});

0 comments on commit 540dc3c

Please sign in to comment.