Skip to content

Commit

Permalink
Merge pull request #319 from pactumjs/318-support-event-emitter
Browse files Browse the repository at this point in the history
feat: added events
  • Loading branch information
ASaiAnudeep authored Mar 12, 2024
2 parents a1e5c6d + c506b75 commit 2d54cf0
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 204 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
.pactum/
pacts/
coverage/
reports/
reports/
data.txt.br
330 changes: 134 additions & 196 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.6.1",
"version": "3.6.2",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down Expand Up @@ -52,7 +52,7 @@
"author": "Anudeep <[email protected]>",
"license": "MIT",
"dependencies": {
"@exodus/schemasafe": "^1.2.3",
"@exodus/schemasafe": "^1.3.0",
"deep-override": "^1.0.2",
"form-data-lite": "^1.0.3",
"json-query": "^2.2.2",
Expand All @@ -65,8 +65,8 @@
"polka": "^0.5.2"
},
"devDependencies": {
"chai": "^4.3.7",
"mocha": "^10.2.0",
"chai": "^4.4.1",
"mocha": "^10.3.0",
"nyc": "^15.1.0",
"sinon": "^15.2.0"
},
Expand Down
8 changes: 8 additions & 0 deletions src/exports/events.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EventEmitter } from 'node:events';

export const EVENT_TYPES: {
BEFORE_REQUEST: 'BEFORE_REQUEST';
AFTER_RESPONSE: 'AFTER_RESPONSE';
};

export const events: EventEmitter;
13 changes: 13 additions & 0 deletions src/exports/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { EventEmitter } = require('events');

const events = new EventEmitter();

const EVENT_TYPES = {
BEFORE_REQUEST: "BEFORE_REQUEST",
AFTER_RESPONSE: "AFTER_RESPONSE",
}

module.exports = {
events,
EVENT_TYPES
}
5 changes: 3 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as Spec from './models/Spec';
import * as Fuzz from './models/Fuzz';
import * as E2E from './models/E2E';
import * as Fuzz from './models/Fuzz';
import * as Spec from './models/Spec';

import expect from './exports/expect';
export { expect };

export * as events from './exports/events';
export * as handler from './exports/handler';
export * as mock from './exports/mock';
export * as reporter from './exports/reporter';
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const state = require('./exports/state');
const stash = require('./exports/stash');
const expect = require('./exports/expect');
const reporter = require('./exports/reporter');
const events = require('./exports/events');

const processor = require('./helpers/dataProcessor');

Expand All @@ -41,6 +42,7 @@ const pactum = {
stash,
expect,
reporter,
events,

spec(name, data) {
return new Spec(name, data);
Expand Down
6 changes: 5 additions & 1 deletion src/models/Tosser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const mock = require('../exports/mock');
const request = require('../exports/request');
const config = require('../config');
const hr = require('../helpers/handler.runner');
const { events, EVENT_TYPES } = require('../exports/events');

class Tosser {

Expand Down Expand Up @@ -253,6 +254,7 @@ async function getResponse(tosser) {
let res = {};
const requestStartTime = Date.now();
try {
events.emit(EVENT_TYPES.BEFORE_REQUEST, request);
log.debug(`${request.method} ${request.url}`);
res = await phin(request);
res.buffer = res.body;
Expand All @@ -267,8 +269,10 @@ async function getResponse(tosser) {
log.error('Error performing request', error);
}
res = error;
} finally {
res.responseTime = Date.now() - requestStartTime;
events.emit(EVENT_TYPES.AFTER_RESPONSE, res);
}
res.responseTime = Date.now() - requestStartTime;
return res;
}

Expand Down
25 changes: 25 additions & 0 deletions test/component/events.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { spec } = require('../../src');
const { events, EVENT_TYPES } = require('../../src').events;

describe('events', () => {

before(() => {
events.on(EVENT_TYPES.BEFORE_REQUEST, (r) => {
console.log(r);
});
events.on(EVENT_TYPES.AFTER_RESPONSE, (r) => {
console.log(r.body);
});
});

after(() => {
events.removeAllListeners();
});

it('get', async () => {
await spec()
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expectStatus(200);
});
});

0 comments on commit 2d54cf0

Please sign in to comment.