Repository: pactumjs/pactum
Pull request #54 (2021-04-08 16:08:54)

3.0.10
- HTTP Methods - trace, options
- Spec Methods - withMethod, withPath
- Support Custom Logger
- Export Logger


Patch:
diff --git a/package-lock.json b/package-lock.json
index de618d8..d31bdcd 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "pactum",
-  "version": "3.0.9",
+  "version": "3.0.10",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index 7e2fe21..969b38b 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "pactum",
-  "version": "3.0.9",
+  "version": "3.0.10",
   "description": "REST API Testing Tool for all levels in a Test Pyramid",
   "main": "./src/index.js",
   "types": "./src/index.d.ts",
diff --git a/src/exports/logger.d.ts b/src/exports/logger.d.ts
new file mode 100644
index 0000000..d448751
--- /dev/null
+++ b/src/exports/logger.d.ts
@@ -0,0 +1,1 @@
+export function get(): any;
\ No newline at end of file
diff --git a/src/helpers/logger.js b/src/exports/logger.js
similarity index 93%
rename from src/helpers/logger.js
rename to src/exports/logger.js
index 2d0c2e7..3cc0261 100644
--- a/src/helpers/logger.js
+++ b/src/exports/logger.js
@@ -1,4 +1,4 @@
-const { options, magenta, blue, green, yellow, red } = require('./colors');
+const { options, magenta, blue, green, yellow, red } = require('../helpers/colors');
 
 const LEVEL_TRACE = 3;
 const LEVEL_DEBUG = 4;
@@ -94,4 +94,14 @@ function getMessage(msg) {
   }
 }
 
-module.exports = new Logger();
\ No newline at end of file
+module.exports = {
+  logger: new Logger(),
+
+  get() {
+    return this.logger;
+  },
+
+  set(lgr) {
+    this.logger = lgr;
+  }
+};
\ No newline at end of file
diff --git a/src/exports/mock.d.ts b/src/exports/mock.d.ts
index bb22ba7..054f789 100644
--- a/src/exports/mock.d.ts
+++ b/src/exports/mock.d.ts
@@ -1,4 +1,4 @@
-export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
+export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'TRACE';
 
 export interface GraphQLRequest {
   query: string;
diff --git a/src/exports/settings.d.ts b/src/exports/settings.d.ts
index 24b5edf..44c3ca2 100644
--- a/src/exports/settings.d.ts
+++ b/src/exports/settings.d.ts
@@ -5,6 +5,7 @@ export type LogLevel = 'TRACE'|'DEBUG'|'INFO'|'WARN'|'ERROR';
   * @env PACTUM_LOG_LEVEL
 */
 export function setLogLevel(level: LogLevel): void;
+export function setLogger(logger: object): void;
 
 export interface Strategy {
   starts?: string;
diff --git a/src/exports/settings.js b/src/exports/settings.js
index f97363a..4954fe8 100644
--- a/src/exports/settings.js
+++ b/src/exports/settings.js
@@ -1,10 +1,14 @@
 const config = require('../config');
-const logger = require('../helpers/logger');
+const logger = require('./logger');
 
 const settings = {
 
   setLogLevel(level) {
-    logger.setLevel(level);
+    logger.get().setLevel(level);
+  },
+
+  setLogger(lgr) {
+    logger.set(lgr);
   },
 
   setAssertHandlerStrategy(strategy) {
diff --git a/src/exports/stash.js b/src/exports/stash.js
index 836560e..9ac2d9d 100644
--- a/src/exports/stash.js
+++ b/src/exports/stash.js
@@ -1,6 +1,6 @@
 const fs = require('fs');
 const pt = require('path');
-const log = require('../helpers/logger');
+const log = require('../exports/logger').get();
 const { PactumConfigurationError } = require('../helpers/errors');
 const config = require('../config');
 
diff --git a/src/helpers/compare.js b/src/helpers/compare.js
index f537b5d..216d4d4 100644
--- a/src/helpers/compare.js
+++ b/src/helpers/compare.js
@@ -1,5 +1,5 @@
 const config = require('../config');
-const log = require('./logger');
+const log = require('../exports/logger').get();
 const handler = require('../exports/handler');
 const helper = require('../helpers/helper');
 
diff --git a/src/helpers/dataProcessor.js b/src/helpers/dataProcessor.js
index 92cbba8..00d85d1 100644
--- a/src/helpers/dataProcessor.js
+++ b/src/helpers/dataProcessor.js
@@ -3,7 +3,7 @@ const jq = require('json-query');
 
 const stash = require('../exports/stash');
 const handler = require('../exports/handler');
-const logger = require('./logger');
+const log = require('../exports/logger').get();
 const config = require('../config');
 
 const DATA_REF_PATTERN = /(\$\w\{[^\}]+\})/g;
@@ -63,7 +63,7 @@ const dataProcessor = {
           data = this.processDataTemplates(data);
         }
       } else {
-        logger.warn(`Template Not Found - ${templateName}`);
+        log.warn(`Template Not Found - ${templateName}`);
       }
     } else {
       for (const prop in data) {
diff --git a/src/helpers/handler.runner.js b/src/helpers/handler.runner.js
index cb0dfc9..acce788 100644
--- a/src/helpers/handler.runner.js
+++ b/src/helpers/handler.runner.js
@@ -1,5 +1,5 @@
 const handler = require('../exports/handler');
-const log = require('./logger');
+const log = require('../exports/logger').get();
 
 const hr = {
 
diff --git a/src/helpers/utils.js b/src/helpers/utils.js
index 40dff65..e98c97d 100644
--- a/src/helpers/utils.js
+++ b/src/helpers/utils.js
@@ -1,7 +1,7 @@
 const { compare } = require('pactum-matchers').utils;
 const graphQL = require('./graphQL');
 
-const log = require('./logger');
+const log = require('../exports/logger').get();
 const helper = require('./helper');
 
 const utils = {
diff --git a/src/index.d.ts b/src/index.d.ts
index 809a993..4523652 100644
--- a/src/index.d.ts
+++ b/src/index.d.ts
@@ -11,6 +11,7 @@ export * as request from './exports/request';
 export * as settings from './exports/settings';
 export * as stash from './exports/stash';
 export * as state from './exports/state';
+export * as logger from './exports/logger';
 
 /**
  * returns an instance of a spec
diff --git a/src/models/FuzzTosser.js b/src/models/FuzzTosser.js
index f88185e..890d24c 100644
--- a/src/models/FuzzTosser.js
+++ b/src/models/FuzzTosser.js
@@ -1,6 +1,6 @@
 const phin = require('phin');
 const fuzzCore = require('openapi-fuzzer-core');
-const log = require('../helpers/logger');
+const log = require('../exports/logger').get();
 const rp = require('../helpers/requestProcessor');
 const helper = require('../helpers/helper');
 const mock = require('../exports/mock');
diff --git a/src/models/Spec.js b/src/models/Spec.js
index 4ad8c0b..9ed65fe 100644
--- a/src/models/Spec.js
+++ b/src/models/Spec.js
@@ -5,7 +5,7 @@ const Tosser = require('./Tosser');
 const Expect = require('./expect');
 const State = require('./State');
 const helper = require('../helpers/helper');
-const log = require('../helpers/logger');
+const log = require('../exports/logger').get();
 const th = require('../helpers/toss.helper');
 const { PactumRequestError } = require('../helpers/errors');
 const responseExpect = require('../exports/expect');
diff --git a/src/models/Step.js b/src/models/Step.js
index d361884..1a7b454 100644
--- a/src/models/Step.js
+++ b/src/models/Step.js
@@ -1,5 +1,5 @@
 const Spec = require('./Spec');
-const log = require('../helpers/logger');
+const log = require('../exports/logger').get();
 const helper = require('../helpers/helper');
 const rlc = require('../helpers/reporter.lifeCycle');
 
diff --git a/src/models/Tosser.js b/src/models/Tosser.js
index 7963fcd..c93e731 100644
--- a/src/models/Tosser.js
+++ b/src/models/Tosser.js
@@ -1,6 +1,6 @@
 const phin = require('phin');
 const helper = require('../helpers/helper');
-const log = require('../helpers/logger');
+const log = require('../exports/logger').get();
 const rlc = require('../helpers/reporter.lifeCycle');
 const requestProcessor = require('../helpers/requestProcessor');
 const th = require('../helpers/toss.helper');
@@ -143,6 +143,7 @@ class Tosser {
       this.spec.status = 'ERROR';
       this.spec.failure = this.response.toString();
       this.runReport();
+      utils.printReqAndRes(this.request, this.response);
       this.expect.fail(this.response);
     }
   }
@@ -171,7 +172,7 @@ async function getResponse(req) {
     res = await phin(req);
     res.json = helper.getJson(res.body);
   } catch (error) {
-    log.warn('Error performing request', error);
+    log.error('Error performing request', error);
     res = error;
   }
   res.responseTime = Date.now() - requestStartTime;
diff --git a/src/models/expect.js b/src/models/expect.js
index cf658a8..82719fe 100644
--- a/src/models/expect.js
+++ b/src/models/expect.js
@@ -2,7 +2,7 @@ const assert = require('assert');
 const jqy = require('json-query');
 
 const file = require('../helpers/file.utils');
-const log = require('../helpers/logger');
+const log = require('../exports/logger').get();
 const Compare = require('../helpers/compare');
 const processor = require('../helpers/dataProcessor');
 const handler = require('../exports/handler');
diff --git a/src/models/server.js b/src/models/server.js
index bfde56d..df7f0e6 100644
--- a/src/models/server.js
+++ b/src/models/server.js
@@ -3,7 +3,7 @@ const polka = require('polka');
 const Interaction = require('./Interaction.model');
 const helper = require('../helpers/helper');
 const utils = require('../helpers/utils');
-const log = require('../helpers/logger');
+const log = require('../exports/logger').get();
 const hr = require('../helpers/handler.runner');
 const rlc = require('../helpers/reporter.lifeCycle');
 const th = require('../helpers/toss.helper');
@@ -19,7 +19,6 @@ class Server {
   }
 
   start() {
-    log.trace(`Starting mock server on port ${config.mock.port}`);
     return new Promise((resolve) => {
       if (!this.app) {
         this.app = polka();
@@ -37,7 +36,6 @@ class Server {
   }
 
   stop() {
-    log.trace(`Stopping mock server on port ${config.mock.port}`);
     return new Promise((resolve) => {
       if (this.app) {
         this.app.server.close(() => {
@@ -326,7 +324,6 @@ function bodyParser(req, res, next) {
   });
   req.on('end', () => {
     req.body = helper.getJson(body);
-    log.trace('Request Body', req.body);
     next();
   });
 }


Test patch:
diff --git a/src/models/E2E.js b/src/models/E2E.js
index a03b963..6d52f88 100644
--- a/src/models/E2E.js
+++ b/src/models/E2E.js
@@ -1,5 +1,5 @@
 const Step = require('./Step');
-const log = require('../helpers/logger');
+const log = require('../exports/logger').get();
 const helper = require('../helpers/helper');
 const rlc = require('../helpers/reporter.lifeCycle');
 const hr = require('../helpers/handler.runner');
diff --git a/test/component/request.spec.js b/test/component/request.spec.js
index f154e5e..cc5d0b1 100644
--- a/test/component/request.spec.js
+++ b/test/component/request.spec.js
@@ -62,7 +62,7 @@ describe('Request', () => {
       .expectStatus(200);
   });
 
-  it('HEAD with baseurl', async () => {
+  it('withMethod & withPath', async () => {
     request.setBaseUrl('http://localhost:9393');
     await pactum
       .spec()
diff --git a/test/unit/logger.spec.js b/test/unit/logger.spec.js
index ad3efab..3fdb0b8 100644
--- a/test/unit/logger.spec.js
+++ b/test/unit/logger.spec.js
@@ -1,6 +1,6 @@
 const expect = require('chai').expect;
 const sandbox = require('sinon').createSandbox();
-const logger = require('../../src/helpers/logger');
+const logger = require('../../src/exports/logger').get();
 
 describe('Logger', () => {
 
diff --git a/test/unit/settings.spec.js b/test/unit/settings.spec.js
index 235dee8..036f1e2 100644
--- a/test/unit/settings.spec.js
+++ b/test/unit/settings.spec.js
@@ -2,6 +2,7 @@ const expect = require('chai').expect;
 
 const settings = require('../../src/exports/settings');
 const config = require('../../src/config');
+const logger = require('../../src/exports/logger');
 
 describe('Settings', () => {
 
@@ -10,6 +11,10 @@ describe('Settings', () => {
     expect(config.snapshot.dir).equals('new/path');
   });
 
+  it('setLogger', () => {
+    settings.setLogger(logger.get());
+  });
+
   after(() => {
     settings.setSnapshotDirectoryPath('.pactum/snapshots');
   });