Skip to content

Commit

Permalink
fix: Creator cleanup and persister assertion (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored Jul 17, 2018
1 parent 82ebd09 commit 19fee5a
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 120 deletions.
5 changes: 5 additions & 0 deletions packages/@pollyjs/adapter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export default class Adapter {
return this.passthrough(pollyRequest);
}

this.assert(
'A persister must be configured in order to record and replay requests.',
!!this.persister
);

if (mode === MODES.RECORD) {
return this.record(pollyRequest);
}
Expand Down
68 changes: 33 additions & 35 deletions packages/@pollyjs/core/src/polly.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ export default class Polly {
/**
* Package version.
*
* Gets populated on build time via rollup-plugin-replace
*
* @readonly
* @public
* @memberof Polly
*/
get VERSION() {
static get VERSION() {
return version;
}

Expand Down Expand Up @@ -104,6 +102,38 @@ export default class Polly {
this.config.mode = mode;
}

static once(eventName, listener) {
EVENT_EMITTER.once(eventName, listener);

return this;
}

static off(eventName, listener) {
EVENT_EMITTER.off(eventName, listener);

return this;
}

static register(Factory) {
if (!FACTORY_REGISTRATION.has(Factory)) {
FACTORY_REGISTRATION.set(Factory, container =>
container.register(Factory)
);
}

this.on('register', FACTORY_REGISTRATION.get(Factory));

return this;
}

static unregister(Factory) {
if (FACTORY_REGISTRATION.has(Factory)) {
this.off('register', FACTORY_REGISTRATION.get(Factory));
}

return this;
}

/**
* @param {Object} [config={}]
* @public
Expand Down Expand Up @@ -153,38 +183,6 @@ export default class Polly {
return this;
}

static once(eventName, listener) {
EVENT_EMITTER.once(eventName, listener);

return this;
}

static off(eventName, listener) {
EVENT_EMITTER.off(eventName, listener);

return this;
}

static register(Factory) {
if (!FACTORY_REGISTRATION.has(Factory)) {
FACTORY_REGISTRATION.set(Factory, container =>
container.register(Factory)
);
}

this.on('register', FACTORY_REGISTRATION.get(Factory));

return this;
}

static unregister(Factory) {
if (FACTORY_REGISTRATION.has(Factory)) {
this.off('register', FACTORY_REGISTRATION.get(Factory));
}

return this;
}

/**
* @public
* @memberof Polly
Expand Down
4 changes: 4 additions & 0 deletions packages/@pollyjs/core/tests/unit/polly-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('Unit | Polly', function() {
expect(Polly).to.be.a('function');
});

it('should have a version', function() {
expect(Polly.VERSION).to.be.a('string');
});

it('should instantiate without throwing', async function() {
await expect(async function() {
const polly = new Polly('recording name');
Expand Down
15 changes: 6 additions & 9 deletions packages/@pollyjs/persister/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,19 @@ export default class Persister {
}

const promises = [];
const creator = {
name: CREATOR_NAME,
version: this.polly.constructor.VERSION,
comment: `${this.constructor.type}:${this.constructor.name}`
};

for (const [recordingId, { name, requests }] of this.pending) {
const entries = [];
const recording = await this.find(recordingId);
let har;

if (!recording) {
har = new HAR({
log: {
creator: {
name: CREATOR_NAME,
version: this.polly.VERSION
},
_recordingName: name
}
});
har = new HAR({ log: { creator, _recordingName: name } });
} else {
har = new HAR(recording);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/persister-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ export default function persisterTests() {
expect(await validate.har(await persister.find(recordingId))).to.be.true;
});

it('should have the correct metadata', async function() {
const { recordingId, recordingName, persister } = this.polly;

this.polly.record();
await this.fetch('/api/db/foo');
await persister.persist();

const har = await persister.find(recordingId);
const { _recordingName, creator, entries } = har.log;
const entry = entries[0];

expect(_recordingName).to.equal(recordingName);

expect(creator.name).to.equal('Polly.JS');
expect(creator.version).to.equal(Polly.VERSION);
expect(creator.comment).to.equal(
`${persister.constructor.type}:${persister.constructor.name}`
);

expect(entry).to.be.an('object');
expect(entry._id).to.a('string');
expect(entry._order).to.equal(0);
});

it('should add new entries to an existing recording', async function() {
const { recordingId, recordingName, config } = this.polly;
let { persister } = this.polly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"version": "67.0"
},
"creator": {
"comment": "persister:rest",
"name": "Polly.JS",
"version": "0.5.0"
},
Expand Down Expand Up @@ -46,7 +47,7 @@
},
{
"name": "date",
"value": "Sun, 15 Jul 2018 21:14:34 GMT"
"value": "Mon, 16 Jul 2018 22:55:22 GMT"
},
{
"name": "x-powered-by",
Expand All @@ -59,16 +60,16 @@
"status": 404,
"statusText": "Not Found"
},
"startedDateTime": "2018-07-15T21:14:34.685Z",
"time": 18,
"startedDateTime": "2018-07-16T22:55:22.075Z",
"time": 16,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 18
"wait": 16
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"version": "67.0"
},
"creator": {
"comment": "persister:rest",
"name": "Polly.JS",
"version": "0.5.0"
},
Expand Down Expand Up @@ -46,7 +47,7 @@
},
{
"name": "date",
"value": "Sun, 15 Jul 2018 21:14:34 GMT"
"value": "Mon, 16 Jul 2018 22:55:21 GMT"
},
{
"name": "x-powered-by",
Expand All @@ -59,16 +60,16 @@
"status": 404,
"statusText": "Not Found"
},
"startedDateTime": "2018-07-15T21:14:34.177Z",
"time": 100,
"startedDateTime": "2018-07-16T22:55:21.444Z",
"time": 55,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 100
"wait": 55
}
},
{
Expand Down Expand Up @@ -121,7 +122,7 @@
},
{
"name": "date",
"value": "Sun, 15 Jul 2018 21:14:34 GMT"
"value": "Mon, 16 Jul 2018 22:55:21 GMT"
},
{
"name": "etag",
Expand All @@ -138,16 +139,16 @@
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2018-07-15T21:14:34.310Z",
"time": 73,
"startedDateTime": "2018-07-16T22:55:21.503Z",
"time": 82,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 73
"wait": 82
}
},
{
Expand Down Expand Up @@ -191,7 +192,7 @@
},
{
"name": "date",
"value": "Sun, 15 Jul 2018 21:14:34 GMT"
"value": "Mon, 16 Jul 2018 22:55:21 GMT"
},
{
"name": "etag",
Expand All @@ -208,16 +209,16 @@
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2018-07-15T21:14:34.384Z",
"time": 50,
"startedDateTime": "2018-07-16T22:55:21.587Z",
"time": 73,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 50
"wait": 73
}
},
{
Expand Down Expand Up @@ -256,7 +257,7 @@
},
{
"name": "date",
"value": "Sun, 15 Jul 2018 21:14:34 GMT"
"value": "Mon, 16 Jul 2018 22:55:21 GMT"
},
{
"name": "x-powered-by",
Expand All @@ -269,16 +270,16 @@
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2018-07-15T21:14:34.440Z",
"time": 23,
"startedDateTime": "2018-07-16T22:55:21.666Z",
"time": 25,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 23
"wait": 25
}
},
{
Expand Down Expand Up @@ -317,7 +318,7 @@
},
{
"name": "date",
"value": "Sun, 15 Jul 2018 21:14:34 GMT"
"value": "Mon, 16 Jul 2018 22:55:21 GMT"
},
{
"name": "x-powered-by",
Expand All @@ -330,16 +331,16 @@
"status": 404,
"statusText": "Not Found"
},
"startedDateTime": "2018-07-15T21:14:34.463Z",
"time": 43,
"startedDateTime": "2018-07-16T22:55:21.693Z",
"time": 17,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 43
"wait": 17
}
}
],
Expand Down
Loading

0 comments on commit 19fee5a

Please sign in to comment.