Skip to content

Commit 3afa73c

Browse files
committed
Add tests
1 parent 1c4a852 commit 3afa73c

File tree

6 files changed

+440
-2861
lines changed

6 files changed

+440
-2861
lines changed

package.json

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"scripts": {
55
"lint": "eslint src/**",
66
"prepare": "tsc",
7-
"test": "jest"
7+
"test": "mocha --require ts-node/register ./test/*.test.ts"
88
},
99
"version": "2.0.0",
1010
"main": "out/index.js",
@@ -16,27 +16,18 @@
1616
},
1717
"devDependencies": {
1818
"@google-cloud/logging": "^9.2.1",
19-
"@types/jest": "^24.0.16",
19+
"@types/mocha": "^8.2.2",
2020
"@types/node": "^12.6.8",
2121
"@typescript-eslint/eslint-plugin": "^4.22.0",
2222
"@typescript-eslint/parser": "^4.22.0",
2323
"eslint": "^7.25.0",
2424
"eslint-config-prettier": "^8.3.0",
2525
"eslint-plugin-import": "^2.22.1",
2626
"eslint-plugin-prettier": "^3.4.0",
27-
"jest": "^24.8.0",
27+
"mocha": "^8.3.2",
2828
"prettier": "^2.2.1",
29-
"ts-jest": "^24.0.2",
29+
"ts-node": "^9.1.1",
3030
"typescript": "^3.5.3"
3131
},
32-
"jest": {
33-
"moduleFileExtensions": [
34-
"js",
35-
"ts"
36-
],
37-
"transform": {
38-
"^.+\\.tsx?$": "ts-jest"
39-
},
40-
"testRegex": ".*\\.test\\.ts"
41-
}
32+
"dependencies": {}
4233
}

src/extender.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const createStackdriverExtender = (projectId: string, logId: string): Ext
1515
return gcl.protos.google.logging.type.LogSeverity.DEBUG
1616
case Level.Info:
1717
return gcl.protos.google.logging.type.LogSeverity.INFO
18-
case Level.Warning:
18+
case Level.Warn:
1919
return gcl.protos.google.logging.type.LogSeverity.WARNING
2020
case Level.Error:
2121
return gcl.protos.google.logging.type.LogSeverity.ERROR

src/logger.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export enum Level {
55
Trace,
66
Debug,
77
Info,
8-
Warning,
8+
Warn,
99
Error,
1010
}
1111

@@ -103,7 +103,7 @@ export function doLog(level: Level, ...args: Argument): void {
103103
return console.debug(...args)
104104
case Level.Info:
105105
return console.info(...args)
106-
case Level.Warning:
106+
case Level.Warn:
107107
return console.warn(...args)
108108
case Level.Error:
109109
return console.error(...args)
@@ -160,7 +160,7 @@ export abstract class Formatter {
160160
if (Array.isArray(arg) && arg.every((a) => a instanceof Field)) {
161161
return void this.message.fields.push(...arg)
162162
}
163-
if (this.colors) {
163+
if (this.colors && (color || weight)) {
164164
this.message.format += `${this.formatType}${this.getType(arg)}${this.formatType}`
165165
this.message.args.push(this.style(color, weight), arg, this.reset())
166166
} else {
@@ -325,7 +325,7 @@ export class Logger {
325325
this.level = Level.Info
326326
break
327327
case "warn":
328-
this.level = Level.Warning
328+
this.level = Level.Warn
329329
break
330330
case "error":
331331
this.level = Level.Error
@@ -367,7 +367,7 @@ export class Logger {
367367
message,
368368
fields,
369369
tagColor: "#FF9D00",
370-
level: Level.Warning,
370+
level: Level.Warn,
371371
})
372372
}
373373

test/logger.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as assert from "assert"
2+
import { Argument, logger } from "../src/logger"
3+
4+
function testLog(level: "trace" | "debug" | "info" | "warn" | "error", skip = false): void {
5+
const original = console[level]
6+
const fn = logger[level].bind(logger)
7+
8+
let called = 0
9+
const args: Argument[] = []
10+
const message = `log: ${level}`
11+
12+
console[level] = (...a: Argument[]) => {
13+
++called
14+
args.push(...a)
15+
}
16+
17+
try {
18+
fn(message)
19+
} finally {
20+
console[level] = original
21+
}
22+
23+
if (skip) {
24+
return assert.strictEqual(0, called)
25+
}
26+
27+
// Colors are only used for TTYs.
28+
const colors = !!process.stdout.isTTY
29+
30+
assert.strictEqual(1, called)
31+
32+
const expectedLength = colors ? 6 : 4
33+
assert.strictEqual(args.length, expectedLength)
34+
35+
// The first argument contains all the formats for the arguments.
36+
let i = 0
37+
assert.strictEqual(args[i++], "[%s] " + "%s".repeat(expectedLength - 2))
38+
39+
// The next part is the date.
40+
assert.strictEqual(true, /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/.test(args[i++]))
41+
42+
// Then the log level color.
43+
if (colors) {
44+
assert.strictEqual(true, args[i++].startsWith("\x1B["))
45+
}
46+
47+
// The log level.
48+
assert.strictEqual(args[i++].trim(), level)
49+
50+
// Reset colors back to base.
51+
if (colors) {
52+
assert.strictEqual(true, args[i++].startsWith("\x1B[0m"))
53+
}
54+
55+
// The actual message.
56+
assert.strictEqual(args[i++], message)
57+
}
58+
59+
describe("Logger", () => {
60+
it("should *NOT* log a trace message", () => {
61+
testLog("trace", true)
62+
})
63+
64+
it("should *NOT* log a debug message", () => {
65+
testLog("debug", true)
66+
})
67+
68+
it("should log an info message", () => {
69+
testLog("info")
70+
})
71+
72+
it("should log a warning message", () => {
73+
testLog("warn")
74+
})
75+
76+
it("should log an error message", () => {
77+
testLog("error")
78+
})
79+
})

tsconfig.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"compilerOptions": {
3-
"declaration": true,
4-
"module": "commonjs",
5-
"moduleResolution": "node",
6-
"outDir": "out",
7-
"sourceMap": true,
8-
"strict": true,
9-
"target": "esnext"
10-
},
11-
"include": [
12-
"src/*.ts"
13-
],
14-
"exclude": [
15-
"src/*test.ts"
16-
]
2+
"compilerOptions": {
3+
"declaration": true,
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"outDir": "out",
7+
"sourceMap": true,
8+
"strict": true,
9+
"target": "es2017"
10+
},
11+
"include": [
12+
"src/*.ts"
13+
],
14+
"exclude": [
15+
"src/*test.ts"
16+
]
1717
}

0 commit comments

Comments
 (0)