Skip to content

Commit 53d143b

Browse files
authored
test(logger): use included test runner dependencies (#2425)
1 parent e43f2ee commit 53d143b

File tree

7 files changed

+158
-178
lines changed

7 files changed

+158
-178
lines changed

packages/logger/package.json

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,17 @@
3232
"docs": "npx typedoc --plugin typedoc-plugin-markdown",
3333
"lint": "npx @biomejs/biome check .",
3434
"lint:fix": "npx @biomejs/biome check --write .",
35-
"mocha": "mocha --config ./test/.mocharc.json src/*.spec.js",
36-
"test:unit": "npm run build && npm run mocha",
37-
"test": "npm run lint && npm run coverage",
38-
"coverage": "npm run build && c8 --config ./test/.c8rc.json npm run mocha"
35+
"test": "npm run lint && npm run test:unit",
36+
"test:unit": "npm run build && node --import tsx --test src/index.test.ts"
3937
},
4038
"dependencies": {
4139
"@types/node": ">=18.0.0"
4240
},
4341
"devDependencies": {
4442
"@biomejs/biome": "^2.0.5",
45-
"@types/chai": "^4.3.5",
46-
"@types/mocha": "^10.0.1",
47-
"c8": "^10.1.2",
48-
"chai": "^4.3.8",
49-
"mocha": "^11.0.1",
50-
"mocha-junit-reporter": "^2.2.1",
51-
"mocha-multi-reporters": "^1.5.1",
5243
"shx": "^0.4.0",
53-
"sinon": "^21.0.0",
5444
"source-map-support": "^0.5.21",
55-
"ts-node": "^10.9.2",
45+
"tsx": "^4.20.6",
5646
"typedoc": "^0.28.7",
5747
"typedoc-plugin-markdown": "^4.7.1",
5848
"typescript": "^5.5.4"

packages/logger/src/index.spec.js

Lines changed: 0 additions & 143 deletions
This file was deleted.

packages/logger/src/index.test.ts

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import assert from 'node:assert/strict';
2+
import { after, beforeEach, describe, it, mock } from 'node:test';
3+
import { ConsoleLogger, LogLevel } from './index.js';
4+
5+
describe('Logger', () => {
6+
describe('ConsoleLogger', () => {
7+
let logger: ConsoleLogger;
8+
9+
/**
10+
* A collection of included console loggers to restore later.
11+
*/
12+
const output = {
13+
debug: console.debug,
14+
info: console.info,
15+
warn: console.warn,
16+
error: console.error,
17+
};
18+
19+
/**
20+
* The set of loggers to mock for checking outputs.
21+
*/
22+
const mocks = {
23+
debug: mock.fn(),
24+
info: mock.fn(),
25+
warn: mock.fn(),
26+
error: mock.fn(),
27+
};
28+
29+
beforeEach(() => {
30+
console.debug = mocks.debug = mock.fn();
31+
console.info = mocks.info = mock.fn();
32+
console.warn = mocks.warn = mock.fn();
33+
console.error = mocks.error = mock.fn();
34+
logger = new ConsoleLogger();
35+
});
36+
37+
after(() => {
38+
console.debug = output.debug;
39+
console.info = output.info;
40+
console.warn = output.warn;
41+
console.error = mocks.error;
42+
});
43+
44+
describe('getLevel', () => {
45+
it('should have the default LogLevel', () => {
46+
assert.strictEqual(logger.getLevel(), LogLevel.INFO);
47+
});
48+
49+
it('should get LogLevel corrrectly', () => {
50+
for (const level of [LogLevel.DEBUG, LogLevel.ERROR, LogLevel.WARN, LogLevel.INFO]) {
51+
logger.setLevel(level);
52+
assert.strictEqual(logger.getLevel(), level);
53+
}
54+
});
55+
});
56+
57+
describe('setLevel', () => {
58+
it('should set the log level', () => {
59+
logger.setLevel(LogLevel.DEBUG);
60+
61+
logger.debug('i am debug');
62+
logger.info('i am info');
63+
64+
assert.strictEqual(mocks.debug.mock.callCount(), 1);
65+
assert.strictEqual(mocks.info.mock.callCount(), 1);
66+
67+
logger.setLevel(LogLevel.INFO);
68+
69+
logger.debug('i am debug');
70+
logger.info('i am info');
71+
72+
assert.strictEqual(mocks.debug.mock.callCount(), 1);
73+
assert.strictEqual(mocks.info.mock.callCount(), 2);
74+
});
75+
});
76+
77+
describe('setName', () => {
78+
it('should set the name', () => {
79+
logger.setName('foobles');
80+
81+
logger.info('test');
82+
83+
assert.strictEqual(mocks.info.mock.callCount(), 1);
84+
assert.deepEqual(mocks.info.mock.calls[0].arguments, ['[INFO] ', 'foobles', 'test']);
85+
});
86+
});
87+
88+
describe('log levels', () => {
89+
describe('debug', () => {
90+
it('should write debug level messages', () => {
91+
logger.setLevel(LogLevel.DEBUG);
92+
93+
logger.debug('debug');
94+
logger.info('info');
95+
logger.warn('warn');
96+
logger.error('error');
97+
98+
assert.strictEqual(mocks.debug.mock.callCount(), 1);
99+
assert.strictEqual(mocks.info.mock.callCount(), 1);
100+
assert.strictEqual(mocks.warn.mock.callCount(), 1);
101+
assert.strictEqual(mocks.error.mock.callCount(), 1);
102+
});
103+
});
104+
105+
describe('info', () => {
106+
it('should write info level messages', () => {
107+
logger.setLevel(LogLevel.INFO);
108+
109+
logger.debug('debug');
110+
logger.info('info');
111+
logger.warn('warn');
112+
logger.error('error');
113+
114+
assert.strictEqual(mocks.debug.mock.callCount(), 0);
115+
assert.strictEqual(mocks.info.mock.callCount(), 1);
116+
assert.strictEqual(mocks.warn.mock.callCount(), 1);
117+
assert.strictEqual(mocks.error.mock.callCount(), 1);
118+
});
119+
});
120+
121+
describe('warn', () => {
122+
it('should write warn level messages', () => {
123+
logger.setLevel(LogLevel.WARN);
124+
125+
logger.debug('debug');
126+
logger.info('info');
127+
logger.warn('warn');
128+
logger.error('error');
129+
130+
assert.strictEqual(mocks.debug.mock.callCount(), 0);
131+
assert.strictEqual(mocks.info.mock.callCount(), 0);
132+
assert.strictEqual(mocks.warn.mock.callCount(), 1);
133+
assert.strictEqual(mocks.error.mock.callCount(), 1);
134+
});
135+
});
136+
137+
describe('error', () => {
138+
it('should write error level messages', () => {
139+
logger.setLevel(LogLevel.ERROR);
140+
141+
logger.debug('debug');
142+
logger.info('info');
143+
logger.warn('warn');
144+
logger.error('error');
145+
146+
assert.strictEqual(mocks.debug.mock.callCount(), 0);
147+
assert.strictEqual(mocks.info.mock.callCount(), 0);
148+
assert.strictEqual(mocks.warn.mock.callCount(), 0);
149+
assert.strictEqual(mocks.error.mock.callCount(), 1);
150+
});
151+
});
152+
});
153+
});
154+
});

packages/logger/test/.c8rc.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/logger/test/.mocharc.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/logger/test/.reports.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/logger/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// "resolveJsonModule": true,
2626
},
2727
"include": ["src/**/*"],
28-
"exclude": ["src/**/*.spec.*"],
28+
"exclude": ["src/**/*.test.*"],
2929
"jsdoc": {
3030
"out": "support/jsdoc",
3131
"access": "public"

0 commit comments

Comments
 (0)