6
6
*/
7
7
'use strict' ;
8
8
const assert = require ( 'assert' ) ;
9
+ const ObjectID = require ( 'mongodb' ) . ObjectID ;
9
10
const helpers = require ( '../lib/helpers' ) ;
10
11
11
12
class CustomError extends Error {
@@ -15,33 +16,131 @@ class CustomError extends Error {
15
16
}
16
17
}
17
18
18
- const originalData = {
19
- customDate : new Date ( ) ,
20
- standardError : new Error ( 'some error' ) ,
21
- customError : new CustomError ( )
22
- } ;
23
-
24
19
describe ( 'winston-mongodb-helpers' , function ( ) {
25
20
describe ( '#prepareMetaData()' , function ( ) {
26
- let preparedData = helpers . prepareMetaData ( originalData ) ;
27
21
it ( 'should preserve Date instances' , function ( ) {
22
+ const originalData = { customDate : new Date ( ) } ;
23
+
24
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
25
+
28
26
assert ( preparedData . customDate instanceof Date ) ;
29
27
assert . strictEqual ( + preparedData . customDate , + originalData . customDate ) ;
30
28
} ) ;
31
29
it ( 'should store Error objects' , function ( ) {
30
+ const originalData = { standardError : new Error ( 'some error' ) } ;
31
+
32
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
33
+
32
34
assert ( preparedData . standardError instanceof Object ) ;
33
35
assert ( ! ( preparedData . standardError instanceof Error ) ) ;
34
36
assert . strictEqual ( preparedData . standardError . message , originalData . standardError . message ) ;
35
37
assert . strictEqual ( preparedData . standardError . name , originalData . standardError . name ) ;
36
38
assert . strictEqual ( preparedData . standardError . stack , originalData . standardError . stack ) ;
37
39
} ) ;
38
40
it ( 'should store extra fields for custom Error objects' , function ( ) {
41
+ const originalData = { customError : new CustomError ( ) } ;
42
+
43
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
44
+
39
45
assert ( preparedData . customError instanceof Object ) ;
40
46
assert ( ! ( preparedData . customError instanceof Error ) ) ;
41
47
assert . strictEqual ( preparedData . customError . message , originalData . customError . message ) ;
42
48
assert . strictEqual ( preparedData . customError . name , originalData . customError . name ) ;
43
49
assert . strictEqual ( preparedData . customError . stack , originalData . customError . stack ) ;
44
50
assert . strictEqual ( preparedData . customError . testField , originalData . customError . testField ) ;
45
51
} ) ;
52
+ it ( 'should preserve ObjectIds' , function ( ) {
53
+ const originalData = { objectId : new ObjectID ( ) } ;
54
+
55
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
56
+
57
+ assert . strictEqual ( preparedData . objectId , originalData . objectId ) ;
58
+ } ) ;
59
+ it ( 'should preserve Buffers' , function ( ) {
60
+ const originalData = { buffer : new Buffer . from ( 'test' ) } ;
61
+
62
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
63
+
64
+ assert . strictEqual ( preparedData . buffer , originalData . buffer ) ;
65
+ } ) ;
66
+ it ( 'should handle objects containing all kinds of values, including arrays, nested objects and functions' , function ( ) {
67
+ const originalData = {
68
+ undefinedValue : undefined ,
69
+ nullValue : null ,
70
+ booleanValue : true ,
71
+ numberValue : 1 ,
72
+ bigIntValue : BigInt ( 9007199254740991 ) ,
73
+ stringValue : 'test' ,
74
+ symbolValue : Symbol ( ) ,
75
+ arrayValue : [ 'this' , 'is' , 'an' , 'array' ] ,
76
+ nestedObjectValue : { objectKey : true } ,
77
+ functionValue : ( a , b ) => a + b
78
+ } ;
79
+
80
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
81
+
82
+ const expected = { ...originalData , functionValue : { } }
83
+ assert . deepStrictEqual ( preparedData , expected ) ;
84
+ } ) ;
85
+ it ( 'should handle arrays containing all kinds of values, including objects, nested arrays and functions' , function ( ) {
86
+ const originalData = [
87
+ undefined ,
88
+ null ,
89
+ true ,
90
+ 1 ,
91
+ BigInt ( 9007199254740991 ) ,
92
+ 'test' ,
93
+ Symbol ( ) ,
94
+ { objectKey : true } ,
95
+ [ 'this' , 'is' , 'an' , 'array' ] ,
96
+ ( a , b ) => a + b
97
+ ] ;
98
+
99
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
100
+
101
+ const expected = [ ...originalData ] ;
102
+ expected [ expected . length - 1 ] = { } ; // function gets converted to empty object
103
+ assert . deepStrictEqual ( preparedData , expected ) ;
104
+ } ) ;
105
+ it ( 'should replace dots and dollar signs in object keys' , function ( ) {
106
+ const originalData = { 'key.with.dots' : true , '$test$' : true } ;
107
+
108
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
109
+
110
+ const expected = { 'key[dot]with[dot]dots' : true , '[$]test[$]' : true } ;
111
+ assert . deepStrictEqual ( preparedData , expected ) ;
112
+ } ) ;
113
+ it ( 'should break circular dependencies' , function ( ) {
114
+ const originalData = { } ;
115
+ originalData . nestedObjectValue = { nestedKey : originalData } ;
116
+ originalData . arrayValue = [ originalData , 'test' , { nestedKey : originalData } ] ;
117
+
118
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
119
+
120
+ const expected = {
121
+ nestedObjectValue : { nestedKey : '[Circular]' } ,
122
+ arrayValue : [ '[Circular]' , 'test' , { nestedKey : '[Circular]' } ]
123
+ } ;
124
+
125
+ assert . deepStrictEqual ( preparedData , expected ) ;
126
+ } ) ;
127
+ it ( 'should handle objects with null prototype' , function ( ) {
128
+ const originalData = Object . create ( null ) ;
129
+ originalData [ 'key.with.dots' ] = true ;
130
+ originalData [ '$test$' ] = true ;
131
+ originalData . nestedObjectValue = { nestedKey : originalData } ;
132
+ originalData . arrayValue = [ originalData , 'test' , { nestedKey : originalData } ] ;
133
+
134
+ const preparedData = helpers . prepareMetaData ( originalData ) ;
135
+
136
+ const expected = {
137
+ 'key[dot]with[dot]dots' : true ,
138
+ '[$]test[$]' : true ,
139
+ nestedObjectValue : { nestedKey : '[Circular]' } ,
140
+ arrayValue : [ '[Circular]' , 'test' , { nestedKey : '[Circular]' } ]
141
+ } ;
142
+
143
+ assert . deepStrictEqual ( preparedData , expected ) ;
144
+ } ) ;
46
145
} ) ;
47
146
} ) ;
0 commit comments