Skip to content

Exploratory PR for backwards-compatible changes & cross-env alignment: timeFormat #915

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds tests for timeFormat property. Demonstrates how debug output can…
… be tested against both browser & NodeJS envs
  • Loading branch information
webketje committed Nov 3, 2022
commit eedb474f3549a22b32753c11c647487fbe19d18c
82 changes: 82 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert');
const debug = require('./src');
const isBrowser = !!window;

describe('debug', () => {
it('passes a basic sanity check', () => {
Expand Down Expand Up @@ -43,6 +44,87 @@ describe('debug', () => {
assert.deepStrictEqual(messages.length, 3);
});

it('exposes public members per instance', () => {
const log = debug('foo');
assert.deepStrictEqual(Object.keys(log), [
'namespace',
'useColors',
'timeFormat',
'color',
'extend',
'destroy',
'enabled'
].concat(isBrowser ? [] : ['inspectOpts']))
});

describe('timeFormat', () => {
const regex = isBrowser ? {
def: /%cfoo %chello%c \+0ms/,
iso: /%cfoo %c\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z hello%c \+0ms/,
localized: /%cfoo %c\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3} \+\d{2}:\d{2} hello%c /,
diff: /%cfoo %chello%c \+0ms,color: #99CC00,color: inherit,color: #99CC00/,
none: /%cfoo %chello%c /
} : {
def: /\x1B\[36;1mfoo \x1B\[0mhello\x1B\[36m \+0ms\x1B\[0m/,
iso: /\x1B\[36;1mfoo \x1B\[0m\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z hello/,
localized: /\x1B\[36;1mfoo \x1B\[0m\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3} \+\d{2}:\d{2} hello/,
diff: /\x1B\[36;1mfoo \x1B\[0mhello\x1B\[36m \+0ms\x1B\[0m/,
none: /\x1B\[36;1mfoo \x1B\[0mhello/
}

it('defaults to \'iso\' when non-TTY in Node, \'diff\' in browser', () => {
const log = debug('foo');
log.enabled = true;
let result = '';

log.log = str => result += str;
log('hello');
const match = regex.def.test(result);
console.log(result)
assert.strictEqual(match, true);
});

it('accepts value of \'localized\'', () => {
const log = debug('foo');
log.enabled = true;
log.timeFormat = 'localized';
let result = '';

log.log = str => result += str;
log('hello');
console.log(result)
const match = regex.localized.test(result);
assert.strictEqual(match, true);
});

it('accepts value of \'diff\'', () => {
const log = debug('foo');
log.enabled = true;
log.timeFormat = 'diff';
let result;

log.log = (...args) => {
result = args.join(',');
}
log('hello');
const match = regex.diff.test(result);
assert.strictEqual(match, true);
});

it('accepts value of \'none\'', () => {
const log = debug('foo');
log.enabled = true;
log.timeFormat = 'none';
let result = '';

log.log = str => result += str;
log('hello');
console.log(result)
const match = regex.none.test(result);
assert.strictEqual(match, true);
});
});

describe('extend namespace', () => {
it('should extend namespace', () => {
const log = debug('foo');
Expand Down