1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | 'use strict';
|
8 | const assert = require('assert');
|
9 | const helpers = require('../lib/helpers');
|
10 |
|
11 | class CustomError extends Error {
|
12 | constructor() {
|
13 | super();
|
14 | this.testField = 'custom error';
|
15 | }
|
16 | }
|
17 |
|
18 | const originalData = {
|
19 | customDate: new Date(),
|
20 | standardError: new Error('some error'),
|
21 | customError: new CustomError()
|
22 | };
|
23 |
|
24 | describe('winston-mongodb-helpers', function() {
|
25 | describe('#prepareMetaData()', function() {
|
26 | let preparedData = helpers.prepareMetaData(originalData);
|
27 | it('should preserve Date instances', function() {
|
28 | assert(preparedData.customDate instanceof Date);
|
29 | assert.strictEqual(+preparedData.customDate, +originalData.customDate);
|
30 | });
|
31 | it('should store Error objects', function() {
|
32 | assert(preparedData.standardError instanceof Object);
|
33 | assert(!(preparedData.standardError instanceof Error));
|
34 | assert.strictEqual(preparedData.standardError.message, originalData.standardError.message);
|
35 | assert.strictEqual(preparedData.standardError.name, originalData.standardError.name);
|
36 | assert.strictEqual(preparedData.standardError.stack, originalData.standardError.stack);
|
37 | });
|
38 | it('should store extra fields for custom Error objects', function() {
|
39 | assert(preparedData.customError instanceof Object);
|
40 | assert(!(preparedData.customError instanceof Error));
|
41 | assert.strictEqual(preparedData.customError.message, originalData.customError.message);
|
42 | assert.strictEqual(preparedData.customError.name, originalData.customError.name);
|
43 | assert.strictEqual(preparedData.customError.stack, originalData.customError.stack);
|
44 | assert.strictEqual(preparedData.customError.testField, originalData.customError.testField);
|
45 | });
|
46 | });
|
47 | });
|