From cac39b1c5b018b0fe93a53a05f084eee543d17f5 Mon Sep 17 00:00:00 2001 From: calvintwr Date: Fri, 31 May 2024 19:34:49 +0800 Subject: [PATCH] Fix/debug depth (#926) * fix debug format options ignored * moved sinon to devDependencies --- package.json | 3 ++- src/node.js | 4 ++-- test.node.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 test.node.js diff --git a/package.json b/package.json index 3bcdc242..5da3cfeb 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "scripts": { "lint": "xo", "test": "npm run test:node && npm run test:browser && npm run lint", - "test:node": "istanbul cover _mocha -- test.js", + "test:node": "istanbul cover _mocha -- test.js test.node.js", "test:browser": "karma start --single-run", "test:coverage": "cat ./coverage/lcov.info | coveralls" }, @@ -44,6 +44,7 @@ "karma-mocha": "^1.3.0", "mocha": "^5.2.0", "mocha-lcov-reporter": "^1.2.0", + "sinon": "^14.0.0", "xo": "^0.23.0" }, "peerDependenciesMeta": { diff --git a/src/node.js b/src/node.js index 79bc085c..715560a4 100644 --- a/src/node.js +++ b/src/node.js @@ -187,11 +187,11 @@ function getDate() { } /** - * Invokes `util.format()` with the specified arguments and writes to stderr. + * Invokes `util.formatWithOptions()` with the specified arguments and writes to stderr. */ function log(...args) { - return process.stderr.write(util.format(...args) + '\n'); + return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n'); } /** diff --git a/test.node.js b/test.node.js new file mode 100644 index 00000000..4cc3c051 --- /dev/null +++ b/test.node.js @@ -0,0 +1,40 @@ +/* eslint-env mocha */ + +const assert = require('assert'); +const util = require('util'); +const sinon = require('sinon'); +const debug = require('./src/node'); + +const formatWithOptionsSpy = sinon.spy(util, 'formatWithOptions'); +beforeEach(() => { + formatWithOptionsSpy.resetHistory(); +}); + +describe('debug node', () => { + describe('formatting options', () => { + it('calls util.formatWithOptions', () => { + debug.enable('*'); + const stdErrWriteStub = sinon.stub(process.stderr, 'write'); + const log = debug('formatting options'); + log('hello world'); + assert(util.formatWithOptions.callCount === 1); + stdErrWriteStub.restore(); + }); + + it('calls util.formatWithOptions with inspectOpts', () => { + debug.enable('*'); + const options = { + hideDate: true, + colors: true, + depth: 10, + showHidden: true + }; + Object.assign(debug.inspectOpts, options); + const stdErrWriteStub = sinon.stub(process.stderr, 'write'); + const log = debug('format with inspectOpts'); + log('hello world2'); + assert.deepStrictEqual(util.formatWithOptions.getCall(0).args[0], options); + stdErrWriteStub.restore(); + }); + }); +});