Skip to content

Commit 42bffa2

Browse files
committed
test: add missing tests
1 parent 5592cda commit 42bffa2

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

src/LogMessage.spec.ts

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,16 @@ describe('LogMessage', () => {
134134
}
135135
});
136136

137-
expect(msg.meta).toBeEmptyObject();
137+
expect(msg.meta).toHaveProperty('dynamic', 'meta');
138138
});
139139

140140
it('"meta" should skip dynamicMeta if function does not return an object', () => {
141141
const msg = new LogMessage({ ...logData.info }, {
142142
...defaultOpts,
143-
dynamicMeta: false
143+
dynamicMeta: () => false
144144
});
145145

146-
expect(msg.meta).toHaveProperty('dynamic', 'meta');
146+
expect(msg.meta).toBeEmptyObject();
147147
});
148148

149149
it('"meta" should convert errors in metadata to object', () => {
@@ -156,17 +156,15 @@ describe('LogMessage', () => {
156156
}
157157
});
158158

159-
expect(typeof msg.meta.prop).toBe('object');
159+
expect(msg.meta.prop).toBeInstanceOf(Error);
160160
expect(msg.meta.foo).toBe('bar');
161-
expect(typeof msg.meta.obj).toBe('object');
161+
expect(msg.meta.obj).toBeObject();
162162
});
163163

164164
it('"tags" should return tags array', () => {
165165
const msg = new LogMessage({ ...logData.withTags }, defaultOpts);
166166

167-
expect(Array.isArray(msg.tags)).toBe(true);
168-
expect(msg.tags).toHaveLength(1);
169-
expect(msg.tags).toContain('test');
167+
expect(msg.tags).toBeArrayIncludingOnly(['test']);
170168
});
171169

172170
it('"tags" should combine all tags', () => {
@@ -176,16 +174,15 @@ describe('LogMessage', () => {
176174
});
177175

178176
expect(msg.tags).toHaveLength(2);
179-
expect(msg.tags).toContain('test');
180-
expect(msg.tags).toContain('global');
177+
expect(msg.tags).toBeArrayIncludingOnly(['test', 'global']);
181178
});
182179

183180
it('"tags" should execute functions', () => {
184181
const msg = new LogMessage({ ...logData.info }, {
185182
...defaultOpts,
186183
tags: [function (this: typeof LogMessage, props) {
187184
expect(this).toBeInstanceOf(LogMessage);
188-
expect(typeof props).toBe('object');
185+
expect(props).toBeObject();
189186
expect(props).toHaveProperty('level', 'info');
190187
expect(props).toHaveProperty('meta', {});
191188
expect(props).toHaveProperty('options');
@@ -200,6 +197,19 @@ describe('LogMessage', () => {
200197
expect(tags).toContain('dynamic');
201198
});
202199

200+
it('"tags" should skip values that are falsy', () => {
201+
const msg = new LogMessage({ ...logData.info }, {
202+
...defaultOpts,
203+
tags: [function () {
204+
return false;
205+
}]
206+
});
207+
208+
const { tags } = msg;
209+
210+
expect(tags).toHaveLength(0);
211+
});
212+
203213
it('"tags" should replace <<level>> variable with log level', () => {
204214
const msg = new LogMessage({ ...logData.info }, {
205215
...defaultOpts,
@@ -209,9 +219,16 @@ describe('LogMessage', () => {
209219
const { tags } = msg;
210220

211221
expect(tags).toHaveLength(3);
212-
expect(tags).toContain('<notAVariable>');
213-
expect(tags).toContain('info');
214-
expect(tags).toContain('<<variable>>');
222+
expect(tags).toBeArrayIncludingOnly(['<notAVariable>', 'info', '<<variable>>']);
223+
});
224+
225+
it('"tags" should not combine global tags if not an array', () => {
226+
const msg = new LogMessage({ ...logData.withTags }, {
227+
...defaultOpts,
228+
tags: 'invalid' as any
229+
});
230+
231+
expect(msg.tags).toBeArrayIncludingOnly(['test']);
215232
});
216233

217234
it('"value" should return log object', () => {
@@ -390,8 +407,7 @@ describe('LogMessage', () => {
390407
msg.tags = ['another-tag'];
391408

392409
expect(msg.tags).toHaveLength(2);
393-
expect(msg.tags).toContain('test');
394-
expect(msg.tags).toContain('another-tag');
410+
expect(msg.tags).toBeArrayIncludingOnly(['another-tag', 'test']);
395411
});
396412
});
397413

@@ -412,8 +428,7 @@ describe('LogMessage', () => {
412428
});
413429

414430
it('should return log in JSON format', () => {
415-
expect(typeof msg.toJSON()).toBe('string');
416-
expect(msg.toJSON()).toMatch(/^\{.*\}$/);
431+
expect(msg.toJSON()).toBeJsonString();
417432
});
418433

419434
it('should run replacer function', () => {
@@ -438,7 +453,7 @@ describe('LogMessage', () => {
438453
it('should return log in string format', () => {
439454
const msg = new LogMessage({ ...logData.info }, defaultOpts);
440455

441-
expect(typeof msg.toString()).toBe('string');
456+
expect(msg.toString()).toBeString();
442457
});
443458

444459
it('should format as json with onFormat set to `json`', () => {
@@ -447,7 +462,7 @@ describe('LogMessage', () => {
447462
onFormat: 'json'
448463
});
449464

450-
expect(msg.toString()).toMatch(/^\{.*\}$/);
465+
expect(msg.toString()).toBeJsonString();
451466
});
452467

453468
it('should format with "clean" template with onFormat set to `clean`', () => {

src/utils.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ describe('utils', () => {
5959
const error = stubError(e);
6060
expect(error.toJSON!.toString()).toBe(noop.toString());
6161
});
62+
63+
it('should skip over keys that are not on the Error instance', () => {
64+
const e = new Error('test');
65+
delete e.stack;
66+
const err = stubError(e);
67+
expect(err).not.toHaveProperty('stack');
68+
});
6269
});
6370

6471
describe('toBool()', () => {

0 commit comments

Comments
 (0)