fallback to json dance if structureClone fails
[squeep-logger-json-console] / test / lib / logger.js
index a2efb537fd5e2eb0b00e4d72274f0567ce5488cc..939c61f4f25960fbe19790e492db0dd7db91fd6f 100644 (file)
@@ -11,6 +11,18 @@ const asyncLocalStorage = new AsyncLocalStorage();
 describe('Logger', function () {
   let config, logger, commonObject, scope, message;
 
+  function sanitizeCredential(data, sanitize = true) {
+    let unclean = false;
+    const credentialLength = data?.ctx?.parsedBody?.credential?.length;
+    if (credentialLength) {
+      unclean = true;
+    }
+    if (unclean && sanitize) {
+      data.ctx.parsedBody.credential = '*'.repeat(credentialLength);
+    }
+    return unclean;
+  }
+
   beforeEach(function () {
     config = {};
     commonObject = {
@@ -76,17 +88,7 @@ describe('Logger', function () {
   });
 
   it('sanitizes', function () {
-    logger.dataSanitizers.push((data, sanitize = true) => {
-      let unclean = false;
-      const credentialLength = data?.ctx?.parsedBody?.credential?.length;
-      if (credentialLength) {
-        unclean = true;
-      }
-      if (unclean && sanitize) {
-        data.ctx.parsedBody.credential = '*'.repeat(credentialLength);
-      }
-      return unclean;
-    });
+    logger.dataSanitizers.push(sanitizeCredential);
     logger.info(scope, message, {
       ctx: {
         parsedBody: {
@@ -157,4 +159,19 @@ describe('Logger', function () {
     assert(logger.backend.info.called);
     assert(logger.backend.info.args[0][0].includes('[Circular]'));
   });
+
+  it('covers non-serializable objects', function () {
+    logger.dataSanitizers.push(sanitizeCredential);
+    const data = {
+      ctx: {
+        parsedBody: {
+          credential: 'foo',
+        },
+      },
+      foo: new WeakMap(),
+    };
+    logger.info(scope, message, data);
+    assert(logger.backend.info.called);
+  });
+
 }); // Logger
\ No newline at end of file