UNPKG

2.82 kBJavaScriptView Raw
1const expect = require('chai').expect;
2const loglevel = require('loglevel');
3const other = require('loglevel-plugin-mock');
4const sinon = require('sinon');
5const prefix = require('../lib/loglevel-plugin-prefix');
6
7const spy = sinon.spy();
8
9loglevel.enableAll();
10
11describe('API', () => {
12 it('Methods', () => {
13 expect(prefix).to.have.property('apply').with.be.a('function');
14 expect(prefix).to.have.property('reg').with.be.a('function');
15 expect(prefix).not.to.have.property('noConflict');
16 });
17
18 it('Reg: empty arguments', () => {
19 expect(prefix.reg).to.throw(TypeError, 'Argument is not a root logger');
20 });
21
22 it('Reg: incorrect argument', () => {
23 expect(() => prefix.reg('logger')).to.throw(TypeError, 'Argument is not a root logger');
24 });
25
26 it('Apply: empty arguments', () => {
27 expect(prefix.apply).to.throw(TypeError, 'Argument is not a logger');
28 });
29
30 it('Apply: incorrect argument', () => {
31 expect(() => prefix.apply('logger')).to.throw(TypeError, 'Argument is not a logger');
32 });
33});
34
35describe('Prefix', () => {
36 other.apply(loglevel, { method: spy });
37
38 beforeEach(() => {
39 spy.reset();
40 });
41
42 it('Root applying', () => {
43 expect(() => prefix.apply(loglevel)).to.not.throw();
44 });
45
46 it('Root reapplyng', () => {
47 prefix.apply(loglevel, { template: '%l (%n):' });
48 loglevel.info('test');
49 expect(spy.calledWith('INFO (root): test')).to.be.true;
50 });
51
52 it('Format', () => {
53 prefix.apply(loglevel, {
54 format: (level, logger) => `${level} (${logger}):`
55 });
56
57 loglevel.info('test');
58
59 expect(spy.calledWith('INFO (root): test')).to.be.true;
60 });
61
62 it('Unformat', () => {
63 prefix.apply(loglevel, { template: '%l:' });
64
65 loglevel.info('test');
66
67 expect(spy.calledWith('INFO: test')).to.be.true;
68 });
69
70 it('The prefix must be combined with the first argument, if it is a string', () => {
71 prefix.apply(loglevel, { template: '%l:' });
72
73 loglevel.info('foo %s', 'bar');
74 expect(spy.calledWith('INFO: foo %s', 'bar')).to.be.true;
75 });
76
77 it('All methods of the previous plugin should be called', () => {
78 prefix.apply(loglevel);
79
80 // for warn in apply
81 spy.reset();
82
83 loglevel.trace();
84 loglevel.debug();
85 loglevel.info();
86 loglevel.warn();
87 loglevel.error();
88 expect(spy.callCount).to.equal(5);
89 });
90
91 it('Child applying', () => {
92 const child = loglevel.getLogger('child');
93 prefix.apply(child, { template: '%l (%n):' });
94 child.info('test');
95 expect(spy.calledWith('INFO (child): test')).to.be.true;
96 });
97
98 it('Child reapplyng', () => {
99 const child = loglevel.getLogger('child');
100 prefix.apply(child, {
101 levelFormatter(level) {
102 return level;
103 }
104 });
105 child.info('test');
106 expect(spy.calledWith('info (child): test')).to.be.true;
107 });
108});