diff --git a/packages/@pollyjs/core/src/-private/request.js b/packages/@pollyjs/core/src/-private/request.js index 57aacfa2..dc69258a 100644 --- a/packages/@pollyjs/core/src/-private/request.js +++ b/packages/@pollyjs/core/src/-private/request.js @@ -265,13 +265,14 @@ export default class PollyRequest extends HTTPBase { // Guid is a string representation of the identifiers this.id = md5(stringify(this.identifiers)); - // Order is calculated on other requests with the same id + // Order is calculated on other requests with the same id and recording id // Only requests before this current one are taken into account. this.order = matchRequestsBy.order && !this.shouldPassthrough && !this.shouldIntercept ? requests .slice(0, requests.indexOf(this)) - .filter(r => r.id === this.id).length + .filter(r => r.id === this.id && r.recordingId === this.recordingId) + .length : 0; } } diff --git a/tests/integration/adapter-tests.js b/tests/integration/adapter-tests.js index 7f7ba8ed..6d79b58a 100644 --- a/tests/integration/adapter-tests.js +++ b/tests/integration/adapter-tests.js @@ -3,28 +3,68 @@ import { ACTIONS } from '@pollyjs/utils'; export default function adapterTests() { it('should respect request order', async function() { - let res = await this.fetchRecord(); + const testOrder = async () => { + let res = await this.fetchRecord(); - expect(res.status).to.equal(404); + expect(res.status).to.equal(404); - res = await this.fetchRecord({ - method: 'POST', - body: JSON.stringify({ foo: 'bar' }), - headers: { 'Content-Type': 'application/json' } - }); + res = await this.fetchRecord({ + method: 'POST', + body: JSON.stringify({ foo: 'bar' }), + headers: { 'Content-Type': 'application/json' } + }); - expect(res.status).to.equal(200); + expect(res.status).to.equal(200); - res = await this.fetchRecord(); - const json = await res.json(); + res = await this.fetchRecord(); + const json = await res.json(); - expect(json).to.deep.equal({ foo: 'bar' }); + expect(json).to.deep.equal({ foo: 'bar' }); - res = await this.fetchRecord({ method: 'DELETE' }); - expect(res.status).to.equal(200); + res = await this.fetchRecord({ method: 'DELETE' }); + expect(res.status).to.equal(200); + + res = await this.fetchRecord(); + expect(res.status).to.equal(404); + }; + + this.polly.configure({ recordIfMissing: false }); + + const { recordingName, config } = this.polly; + + this.polly.record(); + await testOrder(); + await this.polly.stop(); + + this.polly = new Polly(recordingName, config); + this.polly.replay(); + await testOrder(); + }); + + it('should respect request order across multiple recordings', async function() { + const recordingName = this.polly.recordingName; + const otherRecordingName = `${this.polly.recordingName}-other`; + const order = { + [recordingName]: [], + [otherRecordingName]: [] + }; + + this.polly.server.any(this.recordUrl()).on('beforeResponse', req => { + order[req.recordingName].push(req.order); + }); + + await this.fetchRecord(); + await this.fetchRecord(); + + this.polly.server.any(this.recordUrl()).recordingName(otherRecordingName); + await this.fetchRecord(); + await this.fetchRecord(); + + this.polly.server.any(this.recordUrl()).recordingName(); + await this.fetchRecord(); - res = await this.fetchRecord(); - expect(res.status).to.equal(404); + expect(order[recordingName]).to.have.ordered.members([0, 1, 2]); + expect(order[otherRecordingName]).to.have.ordered.members([0, 1]); }); it('should properly handle 204 status code response', async function() {