22 * SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors
33 * SPDX-License-Identifier: GPL-3.0-or-later
44 */
5- import { afterEach , describe , expect , it , test , vi } from 'vitest'
6- import { ConsoleLogger , buildConsoleLogger } from '../lib/ConsoleLogger'
5+
6+ import { beforeEach , describe , expect , it , test , vi } from 'vitest'
7+ import { buildConsoleLogger , ConsoleLogger } from '../lib/ConsoleLogger.ts'
78
89// Dummy Error
910class MyError extends Error {
10-
1111 constructor ( msg : string ) {
1212 super ( msg )
1313 this . name = 'MyError'
1414 }
15-
1615}
1716
18- afterEach ( ( ) => {
17+ beforeEach ( ( ) => {
18+ delete window . __NC_LOGGER_DEBUG__
1919 vi . resetAllMocks ( )
2020} )
2121
@@ -33,8 +33,6 @@ test('building the console logger', () => {
3333} )
3434
3535describe ( 'ConsoleLogger' , ( ) => {
36- afterEach ( ( ) => { vi . resetAllMocks ( ) } )
37-
3836 it ( 'logs debug messages' , ( ) => {
3937 const logger = new ConsoleLogger ( )
4038 const debug = vi . spyOn ( window . console , 'debug' ) . mockImplementation ( ( ) => { } )
@@ -94,10 +92,10 @@ describe('ConsoleLogger', () => {
9492 const logger = new ConsoleLogger ( { one : 1 , two : 2 } )
9593 const debug = vi . spyOn ( window . console , 'debug' ) . mockImplementation ( ( ) => { } )
9694
97- logger . debug ( 'Should be logged' , { two : 3 } )
95+ logger . debug ( 'Should be logged' , { three : 3 } )
9896 expect ( debug ) . toHaveBeenCalledTimes ( 1 )
9997 expect ( debug . mock . calls [ 0 ] [ 0 ] ) . toBe ( '[DEBUG] Should be logged' )
100- expect ( debug . mock . calls [ 0 ] [ 1 ] ) . toEqual ( { one : 1 , two : 3 } )
98+ expect ( debug . mock . calls [ 0 ] [ 1 ] ) . toEqual ( { one : 1 , two : 2 , three : 3 } )
10199 } )
102100
103101 it ( 'allows extending empty global context' , ( ) => {
@@ -119,6 +117,20 @@ describe('ConsoleLogger', () => {
119117 expect ( debug ) . toHaveBeenCalledTimes ( 0 )
120118 } )
121119
120+ it ( 'respects the runtime debug configuration' , ( ) => {
121+ const logger = new ConsoleLogger ( { app : 'test' , level : 2 } )
122+
123+ const debug = vi . spyOn ( window . console , 'debug' )
124+ debug . mockImplementationOnce ( ( ) => { } )
125+
126+ logger . debug ( 'Should not be logged' )
127+ expect ( debug ) . toHaveBeenCalledTimes ( 0 )
128+
129+ window . __NC_LOGGER_DEBUG__ = [ 'files' , 'test' ]
130+ logger . debug ( 'Should be logged now' )
131+ expect ( debug ) . toHaveBeenCalledTimes ( 1 )
132+ } )
133+
122134 it ( 'logs Error objects' , ( ) => {
123135 const error = new MyError ( 'some message' )
124136 const logger = new ConsoleLogger ( { } )
@@ -128,10 +140,9 @@ describe('ConsoleLogger', () => {
128140
129141 logger . warn ( error )
130142 expect ( warn ) . toHaveBeenCalledTimes ( 1 )
131- expect ( console [ 0 ] [ 0 ] ) . toContain ( 'MyError' )
132- expect ( console [ 0 ] [ 0 ] ) . toContain ( 'some message' )
133- expect ( console [ 0 ] [ 0 ] ) . not . toContain ( 'Stack trace' )
143+ expect ( console [ 0 ] [ 0 ] ) . toMatch ( 'MyError: some message' )
134144 expect ( console [ 0 ] [ 1 ] ) . toHaveProperty ( 'error' , error )
145+ expect ( console [ 0 ] [ 1 ] ) . not . toHaveProperty ( 'stacktrace' , error . stack )
135146 } )
136147
137148 it ( 'logs Error objects and stack trace on debug' , ( ) => {
@@ -143,9 +154,9 @@ describe('ConsoleLogger', () => {
143154
144155 logger . debug ( error )
145156 expect ( debug ) . toHaveBeenCalledTimes ( 1 )
146- expect ( console [ 0 ] [ 0 ] ) . toContain ( 'MyError' )
147- expect ( console [ 0 ] [ 0 ] ) . toContain ( 'some message' )
148- expect ( console [ 0 ] [ 0 ] ) . toContain ( 'Stack trace:' )
157+ expect ( console [ 0 ] [ 0 ] ) . toContain ( 'MyError: some message ' )
158+ expect ( console [ 0 ] [ 1 ] ) . toHaveProperty ( 'error' , error )
159+ expect ( console [ 0 ] [ 1 ] ) . toHaveProperty ( 'stacktrace' , error . stack )
149160 } )
150161
151162 it ( 'logs Error objects and does not override context' , ( ) => {
@@ -159,6 +170,8 @@ describe('ConsoleLogger', () => {
159170 expect ( warn ) . toHaveBeenCalledTimes ( 1 )
160171 expect ( console [ 0 ] [ 0 ] ) . toContain ( 'MyError' )
161172 expect ( console [ 0 ] [ 0 ] ) . toContain ( 'some message' )
162- expect ( console [ 0 ] [ 1 ] ) . toHaveProperty ( 'error' , 'none' )
173+ expect ( console [ 0 ] [ 1 ] ) . toHaveProperty ( 'error' )
174+ // @ts -expect-error - We know error is an array here
175+ expect ( console [ 0 ] [ 1 ] ! . error ) . toEqual ( [ 'none' , error ] )
163176 } )
164177} )
0 commit comments