Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Sep 11, 2018
1 parent ba8a424 commit 833b6f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@
"karma-chai": "^0.1.0",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.2",
"karma-sinon": "^1.0.5",
"mocha": "^5.2.0",
"mocha-lcov-reporter": "^1.2.0",
"rimraf": "^2.5.4",
"sinon": "^1.17.6",
"sinon-chai": "^2.8.0",
"xo": "^0.23.0"
},
"main": "./src/index.js",
Expand Down
84 changes: 38 additions & 46 deletions test/debug-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe, it, context, beforeEach */
/* eslint-env mocha */
'use strict';

let chai;
Expand All @@ -7,84 +7,76 @@ let expect;

let debug;

let sinon;

let sinonChai;

if (typeof module !== 'undefined') {
chai = require('chai');
expect = chai.expect;

debug = require('../src');
sinon = require('sinon');
sinonChai = require('sinon-chai');
chai.use(sinonChai);
}

describe('debug', () => {
let log = debug('test');

log.log = sinon.stub();

it('passes a basic sanity check', () => {
expect(log('hello world')).to.not.throw();
const log = debug('test');
log.enabled = true;
log.log = () => {};

expect(() => log('hello world')).to.not.throw();
});

it('allows namespaces to be a non-string value', () => {
expect(debug.enable(true)).to.not.throw();
});
const log = debug('test');
log.enabled = true;
log.log = () => {};

context('with log function', () => {
beforeEach(() => {
debug.enable('test');
log = debug('test');
});
expect(() => debug.enable(true)).to.not.throw();
});

it('uses it', () => {
log.log = sinon.stub();
log('using custom log function');
it('honors global debug namespace enable calls', () => {
expect(debug('test:12345').enabled).to.equal(false);
expect(debug('test:67890').enabled).to.equal(false);

expect(log.log).to.have.been.calledOnce();
});
debug.enable('test:12345');
expect(debug('test:12345').enabled).to.equal(true);
expect(debug('test:67890').enabled).to.equal(false);
});

describe('custom functions', () => {
let log;
it('uses custom log function', () => {
const log = debug('test');
log.enabled = true;

beforeEach(() => {
debug.enable('test');
log = debug('test');
});
const messages = [];
log.log = (...args) => messages.push(args);

context('with log function', () => {
it('uses it', () => {
log.log = sinon.spy();
log('using custom log function');
log('using custom log function');
log('using custom log function again');
log('%O', 12345);

expect(log.log).to.have.been.calledOnce();
});
});
expect(messages.length).to.equal(3);
});

describe('extend namespace', () => {
let log;

beforeEach(() => {
debug.enable('foo');
log = debug('foo');
});

it('should extend namespace', () => {
const log = debug('foo');
log.enabled = true;
log.log = () => {};

const logBar = log.extend('bar');
expect(logBar.namespace).to.be.equal('foo:bar');
});

it('should extend namespace with custom delimiter', () => {
const log = debug('foo');
log.enabled = true;
log.log = () => {};

const logBar = log.extend('bar', '--');
expect(logBar.namespace).to.be.equal('foo--bar');
});

it('should extend namespace with empty delimiter', () => {
const log = debug('foo');
log.enabled = true;
log.log = () => {};

const logBar = log.extend('bar', '');
expect(logBar.namespace).to.be.equal('foobar');
});
Expand Down

0 comments on commit 833b6f8

Please sign in to comment.