diff --git a/src/exports/events.d.ts b/src/exports/events.d.ts index 995fd7b..57190c7 100644 --- a/src/exports/events.d.ts +++ b/src/exports/events.d.ts @@ -3,11 +3,7 @@ import { EventEmitter } from 'node:events'; export const EVENT_TYPES: { BEFORE_REQUEST: 'BEFORE_REQUEST'; AFTER_RESPONSE: 'AFTER_RESPONSE'; + AFTER_RESPONSE_ERROR: 'AFTER_RESPONSE_ERROR'; }; export const pactumEvents: EventEmitter; - -/** - * @deprecated - */ -export const events: EventEmitter; diff --git a/src/exports/events.js b/src/exports/events.js index bcfcb4c..7a5d201 100644 --- a/src/exports/events.js +++ b/src/exports/events.js @@ -1,19 +1,14 @@ const { EventEmitter } = require('events'); -/** - * @deprecated - */ -const events = new EventEmitter(); - const pactumEvents = new EventEmitter(); const EVENT_TYPES = { BEFORE_REQUEST: "BEFORE_REQUEST", AFTER_RESPONSE: "AFTER_RESPONSE", + AFTER_RESPONSE_ERROR: "AFTER_RESPONSE_ERROR", } module.exports = { - events, pactumEvents, EVENT_TYPES } \ No newline at end of file diff --git a/src/models/Tosser.js b/src/models/Tosser.js index 01a265b..4750875 100644 --- a/src/models/Tosser.js +++ b/src/models/Tosser.js @@ -10,7 +10,7 @@ const mock = require('../exports/mock'); const request = require('../exports/request'); const config = require('../config'); const hr = require('../helpers/handler.runner'); -const { events, pactumEvents, EVENT_TYPES } = require('../exports/events'); +const { pactumEvents, EVENT_TYPES } = require('../exports/events'); class Tosser { @@ -86,29 +86,34 @@ class Tosser { const strategy = options.strategy; const status = options.status; for (let i = 0; i < count; i++) { - let noRetry = true; + let err = null; + let shouldRetry = false; const ctx = { req: this.request, res: this.response }; if (typeof strategy === 'function') { - noRetry = strategy(ctx); + shouldRetry = !strategy(ctx); } else if (typeof strategy === 'string') { - noRetry = hr.retry(strategy, ctx); + shouldRetry = !hr.retry(strategy, ctx); } else if (status) { if (Array.isArray(status)) { - noRetry = !(status.includes(this.response.statusCode)); + shouldRetry = status.includes(this.response.statusCode); } else { - noRetry = !(status === ctx.res.statusCode); + shouldRetry = status === ctx.res.statusCode; } - } - else { + } else { try { await this.validateResponse(); } catch (error) { - noRetry = false; + err = error; + shouldRetry = true; } } - if (!noRetry) { + if (shouldRetry) { const scale = delay === 1000 ? 'second' : 'seconds'; - log.info(`Request retry initiated, waiting ${delay / 1000} ${scale} for attempt ${i + 1} of ${count}`); + if (err) { + log.info(`Request retry initiated, waiting ${delay / 1000} ${scale} for attempt ${i + 1} of ${count} due to error: ${err.message}`); + } else { + log.info(`Request retry initiated, waiting ${delay / 1000} ${scale} for attempt ${i + 1} of ${count}`); + } await helper.sleep(delay); this.response = await getResponse(this); this.spec._response = this.response; @@ -221,6 +226,7 @@ class Tosser { this.spec.failure = error.toString(); this.runReport(); this.printRequestAndResponse(); + pactumEvents.emit(EVENT_TYPES.AFTER_RESPONSE_ERROR, { request: this.request, response: this.response, error }); throw error; } } @@ -298,7 +304,6 @@ async function getResponse(tosser) { let res = {}; const requestStartTime = Date.now(); try { - events.emit(EVENT_TYPES.BEFORE_REQUEST, request); pactumEvents.emit(EVENT_TYPES.BEFORE_REQUEST, { request }); log.debug(`${request.method} ${request.url}`); res = await phin(request); @@ -316,7 +321,6 @@ async function getResponse(tosser) { res = error; } finally { res.responseTime = Date.now() - requestStartTime; - events.emit(EVENT_TYPES.AFTER_RESPONSE, res); pactumEvents.emit(EVENT_TYPES.AFTER_RESPONSE, { request, response: res }); } return res; diff --git a/test/component/events.spec.js b/test/component/events.spec.js index 2efaa46..d41108a 100644 --- a/test/component/events.spec.js +++ b/test/component/events.spec.js @@ -1,15 +1,23 @@ const { spec } = require('../../src'); const { pactumEvents, EVENT_TYPES } = require('../../src').events; -describe('events', () => { +describe.only('events', () => { before(() => { pactumEvents.on(EVENT_TYPES.BEFORE_REQUEST, (r) => { + console.log('BEFORE_REQUEST'); console.log(r); }); pactumEvents.on(EVENT_TYPES.AFTER_RESPONSE, (r) => { + console.log('AFTER_RESPONSE'); console.log(r.response.body); }); + pactumEvents.on(EVENT_TYPES.AFTER_RESPONSE_ERROR, (r) => { + console.log('AFTER_RESPONSE_ERROR'); + console.log(r.request); + console.log(r.response.body); + console.log(r.error); + }); }); after(() => { @@ -22,4 +30,16 @@ describe('events', () => { .get('http://localhost:9393/default/get') .expectStatus(200); }); + + it('get error', async () => { + try { + await spec() + .useInteraction('default get') + .get('http://localhost:9393/default/get') + .expectStatus(400); + } catch (error) { + + } + + }); }); \ No newline at end of file