Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Wait for all handled requests to resolve via .flush() #75

Merged
merged 7 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/@pollyjs/adapter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,27 @@ export default class Adapter {
}
}

handleRequest() {
return this[REQUEST_HANDLER](...arguments);
async handleRequest(request) {
const pollyRequest = this.polly.registerRequest(request);

await pollyRequest.setup();

try {
const response = await this[REQUEST_HANDLER](pollyRequest);

pollyRequest.promise.resolve(response);

return response;
} catch (error) {
pollyRequest.promise.reject(error);
throw error;
}
}

async [REQUEST_HANDLER](request) {
async [REQUEST_HANDLER](pollyRequest) {
const { mode } = this.polly;
const pollyRequest = this.polly.registerRequest(request);
let interceptor;

await pollyRequest.setup();

if (pollyRequest.shouldIntercept) {
interceptor = new Interceptor();
const response = await this.intercept(pollyRequest, interceptor);
Expand Down
3 changes: 2 additions & 1 deletion packages/@pollyjs/core/src/-private/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import NormalizeRequest from '../utils/normalize-request';
import removeHostFromUrl from '../utils/remove-host-from-url';
import serializeRequestBody from '../utils/serialize-request-body';
import isAbsoluteUrl from 'is-absolute-url';
import { assert, timestamp } from '@pollyjs/utils';
import { assert, timestamp, DeferredPromise } from '@pollyjs/utils';
import HTTPBase from './http-base';

const { keys, freeze } = Object;
Expand All @@ -29,6 +29,7 @@ export default class PollyRequest extends HTTPBase {
this.recordingName = polly.recordingName;
this.recordingId = polly.recordingId;
this.requestArguments = freeze(request.requestArguments || []);
this.promise = DeferredPromise();
this[POLLY] = polly;

/*
Expand Down
8 changes: 8 additions & 0 deletions packages/@pollyjs/core/src/polly.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ export default class Polly {
}
}

async flush() {
const NOOP = () => {};

await Promise.all(
this._requests.map(r => Promise.resolve(r.promise).then(NOOP, NOOP))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the role of the noops here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the promise can be resolved or rejected, the NOOP just catches both to make sure the Promise.resolve doesnt receive an uncaught promise.

);
}

/**
* @param {String|Function} nameOrFactory
* @public
Expand Down
1 change: 1 addition & 0 deletions packages/@pollyjs/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { default as assert } from './utils/assert';
export { default as timeout } from './utils/timeout';
export { default as timestamp } from './utils/timestamp';
export { default as buildUrl } from './utils/build-url';
export { default as DeferredPromise } from './utils/deferred-promise';

export { default as XHR } from './utils/xhr';
export { default as Fetch } from './utils/fetch';
17 changes: 17 additions & 0 deletions packages/@pollyjs/utils/src/utils/deferred-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Create a deferred promise with `resolve` and `reject` methods.
*
* @returns {Promise}
*/
export default function DeferredPromise() {
let resolve, reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});

promise.resolve = resolve;
promise.reject = reject;

return promise;
}
23 changes: 23 additions & 0 deletions packages/@pollyjs/utils/tests/unit/utils/deferred-promise-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import DeferredPromise from '../../../src/utils/deferred-promise';

describe('Unit | Utils | DeferredPromise', function() {
it('should exist', function() {
expect(DeferredPromise).to.be.a('function');
expect(DeferredPromise().resolve).to.be.a('function');
expect(DeferredPromise().reject).to.be.a('function');
});

it('should resolve when calling .resolve()', async function() {
const promise = DeferredPromise();

promise.resolve(42);
expect(await promise).to.equal(42);
});

it('should reject when calling .reject()', async function() {
const promise = DeferredPromise();

promise.reject(new Error('42'));
expect(async () => await promise).to.throw(Error, /42/);
});
});