Skip to content

Commit

Permalink
feat(persister): Add disableSortingHarEntries option (#321)
Browse files Browse the repository at this point in the history
When disabled, entries in the the final HAR will be sorted by the request's timestamp.
This is done by default to satisfy the HAR 1.2 spec but can be enabled to improve
diff readability when committing recordings to git.
  • Loading branch information
offirgolan authored Mar 31, 2020
1 parent cc46bb4 commit 0003c0e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 7 deletions.
19 changes: 19 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,25 @@ polly.configure({
});
```

### disableSortingHarEntries

_Type_: `Boolean`
_Default_: `false`

When disabled, entries in the the final HAR will be sorted by the request's timestamp.
This is done by default to satisfy the HAR 1.2 spec but can be enabled to improve
diff readability when committing recordings to git.

**Example**

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

## timing

_Type_: `Function`
Expand Down
3 changes: 2 additions & 1 deletion packages/@pollyjs/core/src/defaults/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default {

persister: null,
persisterOptions: {
keepUnusedRequests: false
keepUnusedRequests: false,
disableSortingHarEntries: false
},

logging: false,
Expand Down
2 changes: 0 additions & 2 deletions packages/@pollyjs/persister/src/har/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export default class Log {
[...entries, ...this.entries],
(a, b) => a._id === b._id && a._order === b._order
);

this.sortEntries();
}

sortEntries() {
Expand Down
4 changes: 4 additions & 0 deletions packages/@pollyjs/persister/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export default class Persister {

har.log.addEntries(entries);

if (!this.polly.config.persisterOptions.disableSortingHarEntries) {
har.log.sortEntries();
}

if (!this.polly.config.persisterOptions.keepUnusedRequests) {
this._removeUnusedEntries(recordingId, har);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/@pollyjs/persister/tests/unit/har-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Unit | HAR', function() {
).to.be.true;
});

it('addEntries: Entries should be unique & sorted', async function() {
it('addEntries: Entries should be unique', async function() {
const now = new Date().getTime();
const log = new Log({
entries: [
Expand Down Expand Up @@ -72,10 +72,10 @@ describe('Unit | HAR', function() {
expect(
log.entries.map(({ _id, _order, _new }) => ({ _id, _order, _new }))
).to.include.deep.ordered.members([
{ _id: 'ghi', _order: 0, _new: true },
{ _id: 'def', _order: 0, _new: false },
{ _id: 'abc', _order: 0, _new: true },
{ _id: 'abc', _order: 1, _new: true }
{ _id: 'abc', _order: 1, _new: true },
{ _id: 'ghi', _order: 0, _new: true },
{ _id: 'def', _order: 0, _new: false }
]);
});

Expand Down
82 changes: 82 additions & 0 deletions tests/integration/persister-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,86 @@ export default function persisterTests() {
expect(har.log.entries[0].request.url).to.include(orderedRecordUrl(1));
expect(har.log.entries[1].request.url).to.include(orderedRecordUrl(3));
});

it('should sort the entries by date', async function() {
this.polly.configure({
persisterOptions: {
keepUnusedRequests: true
}
});
const { recordingName, recordingId, config } = this.polly;

const orderedRecordUrl = order => `${this.recordUrl()}?order=${order}`;

await this.fetch(orderedRecordUrl(1));
await this.fetch(orderedRecordUrl(2));
await this.polly.persister.persist();

let har = await this.polly.persister.find(recordingId);

expect(har).to.be.an('object');
expect(har.log.entries).to.have.lengthOf(2);
expect(har.log.entries[0].request.url).to.include(orderedRecordUrl(1));
expect(har.log.entries[1].request.url).to.include(orderedRecordUrl(2));

await this.polly.stop();

this.polly = new Polly(recordingName, config);
this.polly.record();

await this.fetch(orderedRecordUrl(3));
await this.fetch(orderedRecordUrl(4));
await this.fetch(orderedRecordUrl(2));
await this.polly.persister.persist();

har = await this.polly.persister.find(recordingId);

expect(har).to.be.an('object');
expect(har.log.entries).to.have.lengthOf(4);
expect(har.log.entries[0].request.url).to.include(orderedRecordUrl(1));
expect(har.log.entries[1].request.url).to.include(orderedRecordUrl(3));
expect(har.log.entries[2].request.url).to.include(orderedRecordUrl(4));
expect(har.log.entries[3].request.url).to.include(orderedRecordUrl(2));
});

it('should not sort the entries by date if `disableSortingHarEntries` is true', async function() {
this.polly.configure({
persisterOptions: {
keepUnusedRequests: true,
disableSortingHarEntries: true
}
});
const { recordingName, recordingId, config } = this.polly;

const orderedRecordUrl = order => `${this.recordUrl()}?order=${order}`;

await this.fetch(orderedRecordUrl(1));
await this.fetch(orderedRecordUrl(2));
await this.polly.persister.persist();

let har = await this.polly.persister.find(recordingId);

expect(har).to.be.an('object');
expect(har.log.entries).to.have.lengthOf(2);
expect(har.log.entries[0].request.url).to.include(orderedRecordUrl(1));
expect(har.log.entries[1].request.url).to.include(orderedRecordUrl(2));

await this.polly.stop();

this.polly = new Polly(recordingName, config);
this.polly.replay();

await this.fetch(orderedRecordUrl(3));
await this.fetch(orderedRecordUrl(4));
await this.polly.persister.persist();

har = await this.polly.persister.find(recordingId);

expect(har).to.be.an('object');
expect(har.log.entries).to.have.lengthOf(4);
expect(har.log.entries[0].request.url).to.include(orderedRecordUrl(3));
expect(har.log.entries[1].request.url).to.include(orderedRecordUrl(4));
expect(har.log.entries[2].request.url).to.include(orderedRecordUrl(1));
expect(har.log.entries[3].request.url).to.include(orderedRecordUrl(2));
});
}

0 comments on commit 0003c0e

Please sign in to comment.