Skip to content

Commit

Permalink
feat(dsl): Add xpactWith and fpactWith to improve the experience when…
Browse files Browse the repository at this point in the history
… skipping tests
  • Loading branch information
TimothyJones committed Jul 18, 2020
1 parent b314834 commit da82056
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,8 @@ pair, you will also need to add `--runInBand` to your `jest` or `react-scripts
test` command in your package.json. This avoids race conditions with the mock
server writing to the pact file.


## Usage

```js
pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
// regular pact tests go here
});
```

## Example

Say that your API layer looks something like this:

```js
Expand Down Expand Up @@ -116,9 +107,10 @@ pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
expect(health).toEqual('up');
}));
});

```
# Best practices
You can make your tests easier to read by extracting your request and responses:
Expand Down Expand Up @@ -173,6 +165,14 @@ pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
});
```
# API Documentation
Jest-Pact has three functions:
* `pactWith(JestPactOptions, (providerMock) => { /* tests go here */ })`: a wrapper that sets up a pact mock provider
* `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.
## Configuration
Expand Down
39 changes: 25 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,33 @@ export const getProviderBaseUrl = (provider: pact.Pact) =>
? provider.mockService.baseUrl
: `http://${provider.opts.host}:${provider.opts.port}`;

export const pactWith = (options: JestPactOptions, tests: any) =>
describe(`Pact between ${options.consumer} and ${options.provider}`, () => {
const pactTestTimeout = options.timeout || 30000;

describe(`with ${pactTestTimeout} ms timeout for Pact`, () => {
let originalTimeout: number;
const jestPactWrapper = (options: JestPactOptions, tests: any) => () => {
const pactTestTimeout = options.timeout || 30000;

beforeAll(() => {
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = pactTestTimeout;
});
describe(`with ${pactTestTimeout} ms timeout for Pact`, () => {
let originalTimeout: number;

afterAll(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});
beforeAll(() => {
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = pactTestTimeout;
});

tests(setupProvider(applyDefaults(options)));
afterAll(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});

tests(setupProvider(applyDefaults(options)));
});
};

const describeString = (options: JestPactOptions) =>
`Pact between ${options.consumer} and ${options.provider}`;

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

export const xpactWith = (options: JestPactOptions, tests: any) =>
xdescribe(describeString(options), jestPactWrapper(options, tests));

export const fpactWith = (options: JestPactOptions, tests: any) =>
fdescribe(describeString(options), jestPactWrapper(options, tests));
14 changes: 14 additions & 0 deletions src/test/fpactwith.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { fpactWith } from '../index';

describe('fpactwith', () => {
fpactWith(
{ consumer: 'MyConsumer', provider: 'NoProvider' },
(provider: any) => {
it('should only run this test', () => {});
},
);

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

describe('xpactwith', () => {
xpactWith(
{ consumer: 'MyConsumer', provider: 'NoOtherProvider' },
(provider: any) => {
test('the test that should be skipped', () => {
throw new Error('tests inside xpactWith should not run');
});
},
);
test('this test should run', () => {});
});
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"jsx-no-lambda": false,
"object-literal-sort-keys": false,
"no-switch-case-fall-through": true,
"switch-default": true
"switch-default": true,
"no-empty": false
}
}

0 comments on commit da82056

Please sign in to comment.