UNPKG

2.27 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
9describe("", () => {
10 beforeEach(() => {
11 spy.reset();
12 });
13
14 describe("API", () => {
15 it("Methods", () => {
16 expect(prefix).to.have.property("apply").with.be.a("function");
17 expect(prefix).not.to.have.property("noConflict");
18 });
19
20 it("Empty arguments", () => {
21 expect(prefix.apply).to.throw(TypeError, "Argument is not a logger");
22 });
23
24 it("Incorrect argument", () => {
25 expect(() => prefix.apply("logger")).to.throw(TypeError, "Argument is not a logger");
26 });
27
28 it("Applying", () => {
29 expect(() => prefix.apply(loglevel)).to.not.throw();
30 });
31
32 it("Reapplying", () => {
33 expect(() => prefix.apply(loglevel)).to.not.throw();
34 });
35 });
36
37 describe("Prefix", () => {
38 other.apply(loglevel, { method: spy });
39 const child = loglevel.getLogger("child");
40 child.enableAll();
41
42 it("All methods of the previous plugin should be called", () => {
43 prefix.apply(loglevel);
44
45 loglevel.enableAll();
46 loglevel.trace();
47 loglevel.debug();
48 loglevel.info();
49 loglevel.warn();
50 loglevel.error();
51 expect(spy.callCount).to.equal(5);
52 });
53
54 it("Child logger", () => {
55 prefix.apply(child, { template: "%l (%n):" });
56 child.info("test");
57 expect(spy.calledWith("INFO (child): test")).to.be.true;
58 });
59
60 it("Child reapplyng", () => {
61 prefix.apply(child, {
62 levelFormatter: function(level) {
63 return level;
64 }
65 });
66 child.info("test");
67 expect(spy.calledWith("info (child): test")).to.be.true;
68 });
69
70 it("Root reapplyng", () => {
71 prefix.apply(loglevel, { template: "%l (%n):" });
72 loglevel.info("test");
73 expect(spy.calledWith("INFO (root): test")).to.be.true;
74 });
75
76 it("The prefix must be combined with the first argument, if it is a string", () => {
77 prefix.apply(loglevel, { template: "%l:" });
78
79 loglevel.warn("foo %s", "bar");
80 expect(spy.calledWith("WARN: foo %s", "bar")).to.be.true;
81 });
82 });
83});