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(persister): Add disableSortingHarEntries option #321

Merged
merged 3 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me know if you have a better name for this option @jasonmit

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a good name for the option 👍


_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));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Due to the way we are adding entries, we add new ones to the beginning of the array and then keep only the unique ones (due to how _.uniqWith works), new entries will be at the top of the file instead of at the bottom.

expect(har.log.entries[2].request.url).to.include(orderedRecordUrl(1));
expect(har.log.entries[3].request.url).to.include(orderedRecordUrl(2));
});
}