forked from redis/ioredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.ts
80 lines (64 loc) · 2.29 KB
/
debug.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import rDebug = require("debug");
import * as sinon from "sinon";
import { expect } from "chai";
import debug, {
getStringValue,
MAX_ARGUMENT_LENGTH
} from "../../lib/utils/debug";
describe("utils/debug", function() {
afterEach(function() {
rDebug.enable(process.env.DEBUG || "");
});
describe(".exports.getStringValue", function() {
it("should return a string or undefined", function() {
expect(getStringValue(true)).to.be.undefined;
expect(getStringValue(undefined)).to.be.undefined;
expect(getStringValue(null)).to.be.undefined;
expect(getStringValue(false)).to.be.undefined;
expect(getStringValue(1)).to.be.undefined;
expect(getStringValue(1.1)).to.be.undefined;
expect(getStringValue(-1)).to.be.undefined;
expect(getStringValue(-1.1)).to.be.undefined;
expect(getStringValue("abc")).to.be.a("string");
expect(
getStringValue(Buffer.from ? Buffer.from("abc") : Buffer.from("abc"))
).to.be.a("string");
expect(getStringValue(new Date())).to.be.a("string");
expect(getStringValue({ foo: { bar: "qux" } })).to.be.a("string");
});
});
describe(".exports", function() {
it("should return a function", function() {
expect(debug("test")).to.be.a("function");
});
it("should output to console if DEBUG is set", function() {
var dbgNS = "ioredis:debugtest";
rDebug.enable(dbgNS);
var logspy = sinon.spy();
var fn = debug("debugtest");
// @ts-ignore
fn.log = logspy;
// @ts-ignore
expect(fn.enabled).to.equal(true);
// @ts-ignore
expect(fn.namespace).to.equal(dbgNS);
var data = [],
i = 0;
while (i < 1000) {
data.push(String(i));
i += 1;
}
var datastr = JSON.stringify(data);
fn("my message %s", { json: data });
expect(logspy.called).to.equal(true);
var args = logspy.getCall(0).args;
var wantedArglen =
30 + // " ... <REDACTED full-length="">"
MAX_ARGUMENT_LENGTH + // max-length of redacted string
datastr.length.toString().length; // length of string of string length (inception much?)
expect(args.length).to.be.above(1);
expect(args[1]).to.be.a("string");
expect(args[1].length).to.equal(wantedArglen);
});
});
});