Skip to content

Commit 540dc3c

Browse files
committed
feat(dsl): Add pactWith.only and pactWith.skip, which behave like their describe counterparts
1 parent 19c0b40 commit 540dc3c

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ Jest-Pact has three functions:
195195
- `xpactWith(JestPactOptions, (providerMock) => { /* tests go here */ })`: Like `xdescribe` in Jest, this skips the pact tests described within.
196196
- `fpactWith(JestPactOptions, (providerMock) => { /* tests go here */ })`: Like `fdescribe` in Jest, this sets this test suite to only run this test.
197197
198+
Additionally, `pactWith.only` and `pactWith.skip` behave as you would expect from Jest.
199+
198200
There are two types exported:
199201
200202
- `JestProvidedPactFn`: This is the type of the second argument to `pactWith`, ie: `(provider: Pact) => void`

src/index.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,26 @@ const jestPactWrapper = (
8181
const describeString = (options: JestPactOptions) =>
8282
`Pact between ${options.consumer} and ${options.provider}`;
8383

84-
export const pactWith = (options: JestPactOptions, tests: JestProvidedPactFn) =>
85-
describe(describeString(options), () => jestPactWrapper(options, tests));
86-
87-
export const xpactWith = (
84+
type PactWithFn = (options: JestPactOptions, tests: JestProvidedPactFn) => void;
85+
interface PactWith {
86+
(options: JestPactOptions, tests: JestProvidedPactFn): void;
87+
only: PactWithFn;
88+
skip: PactWithFn;
89+
}
90+
91+
const describePactWith = (describeFn: jest.Describe): PactWithFn => (
8892
options: JestPactOptions,
8993
tests: JestProvidedPactFn,
90-
) => xdescribe(describeString(options), () => jestPactWrapper(options, tests));
94+
) => describeFn(describeString(options), () => jestPactWrapper(options, tests));
9195

92-
export const fpactWith = (
93-
options: JestPactOptions,
94-
tests: JestProvidedPactFn,
95-
) => fdescribe(describeString(options), () => jestPactWrapper(options, tests));
96+
const extend = (pactWithfn: PactWithFn) => {
97+
const ret = pactWithfn as PactWith;
98+
ret.only = describePactWith(describe.only);
99+
ret.skip = describePactWith(describe.skip);
100+
return ret;
101+
};
102+
103+
export const pactWith = extend(describePactWith(describe));
104+
105+
export const xpactWith = pactWith.skip;
106+
export const fpactWith = pactWith.only;

src/test/pactwith.only.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Pact } from '@pact-foundation/pact';
2+
import { pactWith } from '../index';
3+
4+
describe('pactwith.only', () => {
5+
pactWith.only(
6+
{ consumer: 'MyConsumer', provider: 'NoProvider' },
7+
(provider: Pact) => {
8+
it('should only run this test', () => {});
9+
},
10+
);
11+
12+
test('the test that should be skipped', () => {
13+
throw new Error('this test should not be run');
14+
});
15+
});

src/test/pactwith.skip.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Pact } from '@pact-foundation/pact';
2+
import { pactWith } from '../index';
3+
4+
describe('pactwith.skip', () => {
5+
pactWith.skip(
6+
{ consumer: 'MyConsumer', provider: 'NoOtherProvider' },
7+
(provider: Pact) => {
8+
test('the test that should be skipped', () => {
9+
throw new Error('tests inside xpactWith should not run');
10+
});
11+
},
12+
);
13+
test('this test should run', () => {});
14+
});

0 commit comments

Comments
 (0)