From 0ef563978d93b416f88accbf02546b0df55a6358 Mon Sep 17 00:00:00 2001 From: calvintwr Date: Fri, 2 Sep 2022 16:16:54 +0800 Subject: [PATCH 1/2] fix debug format options ignored --- package.json | 5 +++-- src/node.js | 4 ++-- test.node.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test.node.js diff --git a/package.json b/package.json index 3bcdc242..22ac19d8 100644 --- a/package.json +++ b/package.json @@ -26,12 +26,13 @@ "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" }, "dependencies": { - "ms": "2.1.2" + "ms": "2.1.2", + "sinon": "^14.0.0" }, "devDependencies": { "brfs": "^2.0.1", 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(); + }); + }); +}); From 6fd1faad269116cee0701a62bd0a7fc66ca0f3d1 Mon Sep 17 00:00:00 2001 From: calvintwr Date: Fri, 2 Sep 2022 16:18:45 +0800 Subject: [PATCH 2/2] moved sinon to devDependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 22ac19d8..5da3cfeb 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,7 @@ "test:coverage": "cat ./coverage/lcov.info | coveralls" }, "dependencies": { - "ms": "2.1.2", - "sinon": "^14.0.0" + "ms": "2.1.2" }, "devDependencies": { "brfs": "^2.0.1", @@ -45,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": {