diff --git a/.gitignore b/.gitignore index 15be2208..6ff0eb6f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ npm-debug.log yarn-error.log /dist/ /coverage/ +.nyc_output/ # lockfiles yarn.lock diff --git a/karma.conf.js b/karma.conf.js index 6e835858..e9fc611a 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -14,7 +14,7 @@ module.exports = function (config) { // Test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter - reporters: ['progress'], + reporters: ['mocha'], // Web server port port: 9876, diff --git a/package.json b/package.json index 643af019..b885f3a2 100644 --- a/package.json +++ b/package.json @@ -25,9 +25,12 @@ "license": "MIT", "scripts": { "lint": "xo", - "test": "npm run test:node && npm run test:browser", - "test:node": "istanbul cover _mocha -- test.js", + "pretest": "rm -rf .nyc_output", + "test": "npm --env=node run test:node && npm --env=electron run test:electron && npm run test:browser", "test:browser": "karma start --single-run", + "test:node": "nyc --no-clean mocha --exit", + "test:electron": "nyc --no-clean mocha --exit", + "posttest": "nyc report --reporter lcov", "test:coverage": "cat ./coverage/lcov.info | coveralls" }, "dependencies": { @@ -37,13 +40,14 @@ "brfs": "^2.0.1", "browserify": "^16.2.3", "coveralls": "^3.0.2", - "istanbul": "^0.4.5", "karma": "^3.1.4", "karma-browserify": "^6.0.0", "karma-chrome-launcher": "^2.2.0", "karma-mocha": "^1.3.0", + "karma-mocha-reporter": "^2.2.5", "mocha": "^5.2.0", "mocha-lcov-reporter": "^1.2.0", + "nyc": "^13.1.0", "xo": "^0.23.0" }, "main": "./src/index.js", diff --git a/src/browser.js b/src/browser.js index ac3f7e13..8f976be7 100644 --- a/src/browser.js +++ b/src/browser.js @@ -10,6 +10,14 @@ exports.load = load; exports.useColors = useColors; exports.storage = localstorage(); +/** + * Are we in Electron? + * @returns {boolean} + */ +function isElectron() { + return typeof process !== 'undefined' && (process.type === 'renderer' || process.__nwjs); +} + /** * Colors. */ @@ -106,7 +114,7 @@ function useColors() { // NB: In an Electron preload script, document will be defined but not fully // initialized. Since we know we're in Chrome, we'll just detect this case // explicitly - if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) { + if (isElectron()) { return true; } @@ -204,20 +212,12 @@ function save(namespaces) { * @api private */ function load() { - let r; try { - r = exports.storage.getItem('debug'); + return exports.storage.getItem('debug'); } catch (error) { // Swallow // XXX (@Qix-) should we be logging these? } - - // If debug isn't set in LS, and we're in Electron, try to load $DEBUG - if (!r && typeof process !== 'undefined' && 'env' in process) { - r = process.env.DEBUG; - } - - return r; } /** @@ -242,18 +242,23 @@ function localstorage() { } } -module.exports = require('./common')(exports); +if (isElectron()) { + module.exports = exports; + module.exports.humanize = require('ms'); +} else { + module.exports = require('./common')(exports); -const {formatters} = module.exports; + const {formatters} = module.exports; -/** - * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. - */ + /** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ -formatters.j = function (v) { - try { - return JSON.stringify(v); - } catch (error) { - return '[UnexpectedJSONParseError]: ' + error.message; - } -}; + formatters.j = function (v) { + try { + return JSON.stringify(v); + } catch (error) { + return '[UnexpectedJSONParseError]: ' + error.message; + } + }; +} diff --git a/src/electron.js b/src/electron.js new file mode 100644 index 00000000..1360207c --- /dev/null +++ b/src/electron.js @@ -0,0 +1,28 @@ +exports = require('./browser'); + +const browserLoad = exports.load; + +// If debug isn't set in LS, and we're in Electron, try to load $DEBUG +exports.load = function () { + let r = browserLoad(); + if (!r && 'env' in process) { + r = process.env.DEBUG; + } + return r; +}; + +module.exports = require('./common')(exports); + +const {formatters} = module.exports; + +/** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ + +formatters.j = function (v) { + try { + return JSON.stringify(v); + } catch (error) { + return '[UnexpectedJSONParseError]: ' + error.message; + } +}; diff --git a/src/index.js b/src/index.js index bf4c57f2..e8c73b13 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,11 @@ */ if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) { - module.exports = require('./browser.js'); + if (process.browser === true) { + module.exports = require('./browser.js'); + } else { + module.exports = require('./electron.js'); + } } else { module.exports = require('./node.js'); } diff --git a/test.js b/test.js index f61e079b..2e1d71e4 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,24 @@ /* eslint-env mocha */ +/** pre-load conditions */ +let env = 'browser'; +if (process.env.npm_config_env === 'electron') { + // Force Electron mode + process.type = 'renderer'; + env = 'Electron'; +} else if (process.env.npm_config_env === 'node') { + env = 'node'; +} +env = ' (' + env + ')'; +process.env.DEBUG_YES = 'yes'; +process.env.DEBUG_NO = 'no'; +process.env.DEBUG_NULL = 'null'; +process.env.DEBUG_NUM = 111; + const assert = require('assert'); const debug = require('./src'); -describe('debug', () => { +describe('debug' + env, () => { it('passes a basic sanity check', () => { const log = debug('test'); log.enabled = true; @@ -30,6 +45,7 @@ describe('debug', () => { }); it('uses custom log function', () => { + debug.useColors = () => false; const log = debug('test'); log.enabled = true; @@ -39,8 +55,13 @@ describe('debug', () => { log('using custom log function'); log('using custom log function again'); log('%O', 12345); + log('%o', 12345); + log('%j', {abc: 12345}); + assert.deepStrictEqual(messages.length, 5); - assert.deepStrictEqual(messages.length, 3); + const numInstance = debug.instances.length; + assert(log.destroy()); + assert.strictEqual(debug.instances.length, numInstance - 1); }); describe('extend namespace', () => { @@ -118,4 +139,16 @@ describe('debug', () => { assert.deepStrictEqual(oldSkips.map(String), debug.skips.map(String)); }); }); + + if (env === 'node') { + describe('inspectOpts', () => { + it('handles env vars', () => { + const d = debug.inspectOpts; + assert.strictEqual(d.no, false); + assert.strictEqual(d.null, null); + assert.strictEqual(d.num, 111); + assert.strictEqual(d.yes, true); + }); + }); + } });