Skip to content

Commit

Permalink
feat(core): Add flushRequestsOnStop configuration option (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored May 18, 2020
1 parent 48ea1d7 commit ab4a2e1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ polly.configure({
});
```

## flushRequestsOnStop

_Type_: `Boolean`
_Default_: `false`

Await any unresolved requests handled by the polly instance
(via [flush](api#flush)) when [stop](api#stop) is called.

**Example**

```js
polly.configure({
flushRequestsOnStop: true
});
```

## expiresIn

_Type_: `String`
Expand Down
1 change: 1 addition & 0 deletions packages/@pollyjs/core/src/defaults/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
},

logging: false,
flushRequestsOnStop: false,

recordIfMissing: true,
recordFailedRequests: false,
Expand Down
4 changes: 4 additions & 0 deletions packages/@pollyjs/core/src/polly.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ export default class Polly {
*/
async stop() {
if (this.mode !== MODES.STOPPED) {
if (this.config.flushRequestsOnStop) {
await this.flush();
}

this.disconnect();
this.logger.disconnect();
await (this.persister && this.persister.persist());
Expand Down
29 changes: 29 additions & 0 deletions packages/@pollyjs/core/tests/unit/polly-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@ describe('Unit | Polly', function() {
expect(recognizedOptions.context).to.equal(fakeContext);
});

it('calls flush when flushRequestsOnStop is enabled', async function() {
let polly = new Polly('squawk', { flushRequestsOnStop: false });
let flushCalled = false;

polly.flush = async () => {
flushCalled = true;
};

await polly.stop();
expect(flushCalled).to.be.false;

polly = new Polly('squawk', { flushRequestsOnStop: true });
flushCalled = false;

polly.flush = async () => {
flushCalled = true;
};

await polly.stop();
expect(flushCalled).to.be.true;
});

describe('configure', function() {
setupPolly();

Expand Down Expand Up @@ -481,6 +503,13 @@ describe('Unit | Polly', function() {
expect(this.polly.disconnect());
expect(disconnects.length).to.equal(2);
});

it('.flush()', async function() {
const promise = this.polly.flush();

expect(promise).to.be.a('promise');
await promise;
});
});

describe('Class Methods & Events', function() {
Expand Down

0 comments on commit ab4a2e1

Please sign in to comment.