Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Does not appear to handle all Utils.format() options like standard console. #34

Open
robross0606 opened this issue May 1, 2024 · 3 comments

Comments

@robross0606
Copy link

robross0606 commented May 1, 2024

I have a printf-style formatter using console.log() which works when going to regular console, but does not produce expected output when mocked with this library.

const AUDIT_LOG_TAG = '[[[AUDIT]]]';
const AUDIT_LOG_FORMAT = '%s %s %s %j';

const timestamp = new Date();
const description = 'Example log entry.';
const otherData = {
  more: 'data',
  to: 'output',
  as: 'json',
};

console.log(AUDIT_LOG_FORMAT, timestamp, AUDIT_LOG_TAG, description, otherData);

Outputs:

2024-05-01T14:53:39.439Z [[[AUDIT]]] Example log entry. { more: 'data', to: 'output', as: 'json' }

However, once mocked with this library, the results are:

2024-05-01T14:53:39.439Z [[[AUDIT]]] Example log entry. {}

It seems the library has some issues with certain Utils.format() placeholders like "%j" and "%o".

@robross0606
Copy link
Author

robross0606 commented May 1, 2024

Another example of this disparity is how the console behaves when you supply more arguments than what is in your format pattern:

const AUDIT_LOG_TAG = '[[[AUDIT]]]';
const AUDIT_LOG_FORMAT = '%s %s %s';

const timestamp = new Date();
const description = 'Example log entry.';
const otherData = {
  more: 'data',
  to: 'output',
  as: 'json',
};

console.log(AUDIT_LOG_FORMAT, timestamp, AUDIT_LOG_TAG, description, otherData);

In this case, the regular console does as described in documentation and outputs the extra otherData like this:

2024-05-01T16:40:54.291Z [[[AUDIT]]] Performed create of study object. {
  id: 'b09cd660-14b1-406b-800d-11fed3b0ffc7',
  eventType: 'study',
  action: 'create',
  objectId: 'XfWVsvkHKCkw5dQNUKD1D',
  objectType: 'study',
  copiedFromId: undefined,
  requestId: 'request123',
  userId: 'user456',
  organization: 'test-org',
  studyId: 'JNQVkMXpTSgASkAG5HIox',
  sectionId: 'NnqibM0ZmzSXd1SmAxPro'
}

Here is what this library outputs:

2024-05-01T16:37:56.019Z [[[AUDIT]]] Performed create of study object. Object {
  "action": "create",
  "copiedFromId": undefined,
  "eventType": "study",
  "id": "72d5c390-caa0-410e-a43d-a4123afb00cf",
  "objectId": "mGcj1Yq6N5KuXfSFH7QhF",
  "objectType": "study",
  "organization": "Herzog - Gulgowski",
  "requestId": "q6xIChiIbYOF5hro9twEe",
  "sectionId": "oOBU4KDJvxQ-57b4ATkEd",
  "studyId": "29ZurS6epFiJekt6I2qWe",
  "userId": "Daphne99",
}

Note how console-testing-library adds "Object" in front of the open curly brace ({). The same thing happens if you explicitly use "%o" or "%O".

@robross0606
Copy link
Author

robross0606 commented May 1, 2024

I'm not exactly positive, but looks like the problems may be due to how the library is using pretty-format. This does not put the arguments into a state that can be properly processed by console to match standard behavior when the console is not mocked. It is unclear to me, even with the inline comment, why this re-formatting is necessary.

@robross0606
Copy link
Author

robross0606 commented May 1, 2024

I entirely removed the prettiedArguments re-map and the results came out perfectly with the mock.

With "%j":

2024-05-01T17:07:10.821Z [[[AUDIT]]] Performed create of study object. {"id":"dd8fb67f-55ff-4537-be6b-414920ba8ee6","eventType":"study","action":"create","objectId":"I99iNO9yXzpVzBUrTCg6m","objectType":"study","requestId":"MbsUEnPIPNXQAudzmyisi","userId":"Reid.Upton","organization":"Hickle Inc","studyId":"IcxgEmWdl8Xm5Y81-cBCc","sectionId":"nJ4ZUCH3OmsF6J-Sc9DyL"}

With "%o", "%O" or an extra argument:

2024-05-01T17:10:56.277Z [[[AUDIT]]] Performed create of study object. {
  id: '82ff2ce7-e9d7-4eee-9964-d48b55780922',
  eventType: 'study',
  action: 'create',
  objectId: 'lns_HQ5UmKz3ZB5tiT5GG',
  objectType: 'study',
  copiedFromId: undefined,
  requestId: 'lRaMK0StT4vikOExWw4pq',
  userId: 'Carlie77',
  organization: 'Gusikowski - Bogisich',
  studyId: 'P3HeUsUKoH8cfbs4KTKV7',
  sectionId: 'sbik44vFxRgYkmnjx1mNT'
}

What purpose does forcibly running object arguments through prettyFormat() serve?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant