11const fs = require ( "fs" ) ;
22const path = require ( "path" ) ;
3- const Logger = require ( "../../" ) ;
3+ const _ = require ( "lodash" ) ;
4+ const Logger = require ( "../logger" ) ;
45
56const sleep = ms => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
67
@@ -10,14 +11,14 @@ jest.mock("fs");
1011
1112describe ( "Logger module" , ( ) => {
1213 it ( "instance method" , ( ) => {
13- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
14+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
1415 expect ( typeof logger . info ) . toBe ( "function" ) ;
1516 expect ( typeof logger . error ) . toBe ( "function" ) ;
1617 expect ( typeof logger . logger ) . toBe ( "function" ) ;
1718 } ) ;
1819
1920 it ( "info method" , ( ) => {
20- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
21+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
2122 logger . info ( "hello" ) ;
2223 expect ( fs . appendFileSync . mock . calls . length ) . toBe ( 1 ) ;
2324 const [ file , line ] = fs . appendFileSync . mock . calls [ 0 ] ;
@@ -26,7 +27,7 @@ describe("Logger module", () => {
2627 } ) ;
2728
2829 it ( "info method, has extra" , ( ) => {
29- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
30+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
3031 logger . info ( "hello" , "world" ) ;
3132 expect ( fs . appendFileSync . mock . calls . length ) . toBe ( 2 ) ;
3233 const [ file , line ] = fs . appendFileSync . mock . calls [ 1 ] ;
@@ -36,7 +37,7 @@ describe("Logger module", () => {
3637 } ) ;
3738
3839 it ( "error method" , ( ) => {
39- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
40+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
4041 logger . error ( Error ( "hello" ) , "world" ) ;
4142 expect ( fs . appendFileSync . mock . calls . length ) . toBe ( 3 ) ;
4243 const [ file , line ] = fs . appendFileSync . mock . calls [ 2 ] ;
@@ -46,7 +47,7 @@ describe("Logger module", () => {
4647 } ) ;
4748
4849 it ( "error method, has extra" , ( ) => {
49- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
50+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
5051 logger . error ( Error ( "nihao" ) ) ;
5152 expect ( fs . appendFileSync . mock . calls . length ) . toBe ( 4 ) ;
5253 const [ file , line ] = fs . appendFileSync . mock . calls [ 3 ] ;
@@ -56,7 +57,7 @@ describe("Logger module", () => {
5657 } ) ;
5758
5859 it ( "error method, no stack, has extra" , ( ) => {
59- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
60+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
6061 logger . error ( { code : "errorcode" , message : "nihao" } ) ;
6162 expect ( fs . appendFileSync . mock . calls . length ) . toBe ( 5 ) ;
6263 const [ file , line ] = fs . appendFileSync . mock . calls [ 4 ] ;
@@ -66,7 +67,7 @@ describe("Logger module", () => {
6667 } ) ;
6768
6869 it ( "logger sync method, fn exec success" , ( ) => {
69- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
70+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
7071 const fn = jest . fn ( ) ;
7172 fn . mockReturnValueOnce ( 10 ) ;
7273 const fnLog = logger . logger ( fn , "testing" , false ) ;
@@ -81,7 +82,7 @@ describe("Logger module", () => {
8182 } ) ;
8283
8384 it ( "logger sync method, fn exec faild" , ( ) => {
84- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
85+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
8586 const calls = [ ] ;
8687 const fn = ( ...args ) => {
8788 calls . push ( args ) ;
@@ -97,7 +98,7 @@ describe("Logger module", () => {
9798 } ) ;
9899
99100 it ( "logger async method, fn exec success" , async ( ) => {
100- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
101+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
101102 const calls = [ ] ;
102103 const ret = "I am function return value" ;
103104 const fn = async ( ...args ) => {
@@ -119,7 +120,7 @@ describe("Logger module", () => {
119120 } ) ;
120121
121122 it ( "logger async method, fn exec faild" , async ( ) => {
122- const logger = new Logger ( { infoLogPath, errorLogPath } ) ;
123+ const logger = new Logger ( { infoLogPath, errorLogPath } , { _ } ) ;
123124 const calls = [ ] ;
124125 const fn = async ( ...args ) => {
125126 calls . push ( args ) ;
@@ -137,4 +138,13 @@ describe("Logger module", () => {
137138 expect ( fs . appendFileSync . mock . calls . length ) . toBe ( 13 ) ;
138139 expect ( fs . appendFileSync . mock . calls [ 12 ] [ 1 ] ) . toMatch ( "wrong" ) ;
139140 } ) ;
141+
142+ it ( "ignore error" , async ( ) => {
143+ const logger = new Logger ( { infoLogPath, errorLogPath, ignoreErrors : [ "ignored" ] } , { _ } ) ;
144+ const error = Error ( "wrong" ) ;
145+ error . code = "ignored" ;
146+ logger . error ( error ) ;
147+
148+ expect ( fs . appendFileSync . mock . calls . length ) . toBe ( 13 ) ;
149+ } ) ;
140150} ) ;
0 commit comments