|
| 1 | +import {afterEach, describe, expect, test} from '@jest/globals'; |
| 2 | + |
| 3 | +import {configureStdioTransportLogging, ensureLoggerWritesToStderr, log} from '../logger.js'; |
| 4 | +import {isStdioTransportLoggingConfigured, markStdioTransportLoggingConfigured} from '../stdio-logging-state.js'; |
| 5 | +import {QUIET_WEBDRIVER_LOG_LEVEL, withQuietWebDriverLogging} from '../utils/webdriver-client-options.js'; |
| 6 | + |
| 7 | +describe('stdio transport logging', () => { |
| 8 | + const originalWdioLogLevel = process.env.WDIO_LOG_LEVEL; |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + if (originalWdioLogLevel === undefined) { |
| 12 | + delete process.env.WDIO_LOG_LEVEL; |
| 13 | + } else { |
| 14 | + process.env.WDIO_LOG_LEVEL = originalWdioLogLevel; |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + test('ensureLoggerWritesToStderr only replaces stdout', () => { |
| 19 | + log.unwrap().stream = process.stdout; |
| 20 | + ensureLoggerWritesToStderr(); |
| 21 | + expect(log.unwrap().stream).toBe(process.stderr); |
| 22 | + }); |
| 23 | + |
| 24 | + test('ensureLoggerWritesToStderr leaves stderr and custom sinks in place', () => { |
| 25 | + log.unwrap().stream = process.stderr; |
| 26 | + ensureLoggerWritesToStderr(); |
| 27 | + expect(log.unwrap().stream).toBe(process.stderr); |
| 28 | + |
| 29 | + const custom = {write: () => {}} as unknown as NodeJS.WriteStream; |
| 30 | + log.unwrap().stream = custom; |
| 31 | + ensureLoggerWritesToStderr(); |
| 32 | + expect(log.unwrap().stream).toBe(custom); |
| 33 | + }); |
| 34 | + |
| 35 | + test('withQuietWebDriverLogging is a no-op until stdio logging is configured', () => { |
| 36 | + expect(isStdioTransportLoggingConfigured()).toBe(false); |
| 37 | + expect( |
| 38 | + withQuietWebDriverLogging({ |
| 39 | + hostname: '127.0.0.1', |
| 40 | + port: 4723, |
| 41 | + }), |
| 42 | + ).toEqual({ |
| 43 | + hostname: '127.0.0.1', |
| 44 | + port: 4723, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + test('configureStdioTransportLogging quiets info logs and WDIO when unset', () => { |
| 49 | + delete process.env.WDIO_LOG_LEVEL; |
| 50 | + log.unwrap().stream = process.stdout; |
| 51 | + |
| 52 | + configureStdioTransportLogging(); |
| 53 | + |
| 54 | + expect(isStdioTransportLoggingConfigured()).toBe(true); |
| 55 | + expect(log.unwrap().stream).toBe(process.stderr); |
| 56 | + expect(log.level).toBe('warn'); |
| 57 | + expect(process.env.WDIO_LOG_LEVEL).toBe('warn'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('withQuietWebDriverLogging sets warn after stdio logging is configured', () => { |
| 61 | + markStdioTransportLoggingConfigured(); |
| 62 | + expect( |
| 63 | + withQuietWebDriverLogging({ |
| 64 | + hostname: '127.0.0.1', |
| 65 | + port: 4723, |
| 66 | + }), |
| 67 | + ).toEqual({ |
| 68 | + hostname: '127.0.0.1', |
| 69 | + port: 4723, |
| 70 | + logLevel: QUIET_WEBDRIVER_LOG_LEVEL, |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments