Skip to content

Commit

Permalink
feat(options): Add convienience options logDir and logFileName
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyJones committed Aug 18, 2020
1 parent baa97ac commit 2d61354
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 22 deletions.
40 changes: 23 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ yarn add jest-pact --dev
```

If you have more than one file with pact tests for the same consumer/provider
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
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
Expand All @@ -46,13 +45,13 @@ Say that your API layer looks something like this:
```js
import axios from 'axios';

const defaultBaseUrl = "http://your-api.example.com"
const defaultBaseUrl = 'http://your-api.example.com';

export const api = (baseUrl = defaultBaseUrl) => ({
getHealth: () => axios.get(`${baseUrl}/health`)
.then(response => response.data.status)
/* other endpoints here */
})
getHealth: () =>
axios.get(`${baseUrl}/health`).then((response) => response.data.status),
/* other endpoints here */
});
```

Then your test might look like:
Expand Down Expand Up @@ -113,7 +112,6 @@ pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
You can make your tests easier to read by extracting your request and responses:
```js
/* pact.fixtures.js */
import { Matchers } from '@pact-foundation/pact';
Expand All @@ -131,10 +129,9 @@ export const healthyResponse = {
body: {
status: Matchers.like('up'),
},
}
};
```
```js
import { pactWith } from 'jest-pact';
import { healthRequest, healthyResponse } from "./pact.fixtures";
Expand Down Expand Up @@ -169,10 +166,9 @@ pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
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.
- `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 All @@ -187,11 +183,14 @@ pactWith(JestPactOptions, provider => {
interface JestPactOptions = PactOptions & {
timeout?: number; // Timeout for pact service start/teardown, expressed in milliseconds
// Default is 30000 milliseconds (30 seconds).
logDir?: string; // path for the log file
logFileName?: string; // filename for the log file
}
```
### Defaults
Jest-Pact sets some helpful defaults for you. You can override any of these by explicitly setting corresponding option.
Jest-Pact sets some helpful default PactOptions for you. You can override any of these by explicitly setting corresponding option. Here are the defaults:
- `log` is set so that log files are written to /pact/logs, and named `<consumer>-<provider>-mockserver-interaction.log`
- `dir` is set so that pact files are written to /pact/pacts
Expand All @@ -201,6 +200,13 @@ Jest-Pact sets some helpful defaults for you. You can override any of these by e
Most of the time you won't need to change these.
A common use case for `log` is to change only the filename or the path for
logging. To help with this, Jest-Pact provides convienience options `logDir`
and `logFileName`. These allow you to set the path or the filename
independently. In case you're wondering, if you specify `log`, `logDir` and
`logFileName`, the convienience options are ignored and `log` takes
precidence.
### Jest Watch Mode
By default Jest will watch all your files for changes, which means it will run in an infinite loop as your pact tests will generate json pact files and log files.
Expand All @@ -211,8 +217,8 @@ Example
```js
module.exports = {
testMatch: ["**/*.test.(ts|js)", "**/*.it.(ts|js)", "**/*.pacttest.(ts|js)"],
watchPathIgnorePatterns: ["pact/logs/*", "pact/pacts/*"]
testMatch: ['**/*.test.(ts|js)', '**/*.it.(ts|js)', '**/*.pacttest.(ts|js)'],
watchPathIgnorePatterns: ['pact/logs/*', 'pact/pacts/*'],
};
```
Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import * as path from 'path';

export type JestPactOptions = PactOptions & {
timeout?: number;
logDir?: string;
logFileName?: string;
};

const logHint = (options: JestPactOptions) =>
options.port ? `-port-${options.port}` : '';

const applyDefaults = (options: JestPactOptions) => ({
log: path.resolve(
process.cwd(),
'pact/logs',
`${options.consumer}-${options.provider}-mockserver-interaction${logHint(
options,
)}.log`,
options.logDir ? options.logDir : path.join(process.cwd(), 'pact', 'logs'),
options.logFileName
? options.logFileName
: `${options.consumer}-${
options.provider
}-mockserver-interaction${logHint(options)}.log`,
),
dir: path.resolve(process.cwd(), 'pact/pacts'),
spec: 2,
Expand Down
54 changes: 54 additions & 0 deletions src/test/pactwith.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,57 @@ pactWith({ consumer: 'MyConsumer', provider: 'pactWith2' }, (provider: any) => {
});
});
});

describe('custom log locations', () => {
const arbitraryPact = (provider: any) => {
describe('pact test', () => {
beforeEach(() => provider.addInteraction(postValidRequest));

test('works', () =>
getClient(provider)
.get('/v2/pet/1845563262948980200')
.set('api_key', '[]')
.expect(200));
});
};

describe('with logDir', () => {
describe('without logFileName', () => {
pactWith(
{
consumer: 'MyConsumer',
provider: 'pactWith2',
logDir: 'pact/log/custom',
},
(provider: any) => {
arbitraryPact(provider);
},
);
});
describe('with logFileName', () => {
pactWith(
{
consumer: 'MyConsumer',
provider: 'pactWith2',
logDir: 'pact/log/custom',
logFileName: 'someLog.txt',
},
(provider: any) => {
arbitraryPact(provider);
},
);
});
});
describe('with only logFileName', () => {
pactWith(
{
consumer: 'MyConsumer',
provider: 'pactWith2',
logFileName: 'someOtherLog.txt',
},
(provider: any) => {
arbitraryPact(provider);
},
);
});
});

0 comments on commit 2d61354

Please sign in to comment.