|
| 1 | +import {spawn} from 'node:child_process'; |
| 2 | +import {existsSync} from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import {fileURLToPath} from 'node:url'; |
| 5 | + |
| 6 | +import {describe, expect, test} from '@jest/globals'; |
| 7 | + |
| 8 | +const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); |
| 9 | +const fixturePath = path.join(rootDir, 'dist/tests/fixtures/programmatic-stdio-wdio-child.js'); |
| 10 | + |
| 11 | +function isJsonRpcLine(line: string): boolean { |
| 12 | + if (!line.trim()) { |
| 13 | + return true; |
| 14 | + } |
| 15 | + try { |
| 16 | + const message = JSON.parse(line); |
| 17 | + return typeof message === 'object' && message !== null && 'jsonrpc' in message; |
| 18 | + } catch { |
| 19 | + return false; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe('programmatic stdio WDIO logging', () => { |
| 24 | + test('keeps non-JSON-RPC output off stdout after core stdio start', async () => { |
| 25 | + if (!existsSync(fixturePath)) { |
| 26 | + throw new Error(`Compiled fixture missing at ${fixturePath}. Run npm run build first.`); |
| 27 | + } |
| 28 | + |
| 29 | + const {WDIO_LOG_LEVEL: _wdioLogLevel, ...env} = process.env; |
| 30 | + const child = spawn(process.execPath, [fixturePath], { |
| 31 | + cwd: rootDir, |
| 32 | + env, |
| 33 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 34 | + }); |
| 35 | + |
| 36 | + let stdout = ''; |
| 37 | + let stderr = ''; |
| 38 | + child.stdout.on('data', (chunk: Buffer) => { |
| 39 | + stdout += chunk.toString(); |
| 40 | + }); |
| 41 | + child.stderr.on('data', (chunk: Buffer) => { |
| 42 | + stderr += chunk.toString(); |
| 43 | + }); |
| 44 | + |
| 45 | + const exitCode = await new Promise<number>((resolve, reject) => { |
| 46 | + const timeout = setTimeout(() => { |
| 47 | + child.kill('SIGTERM'); |
| 48 | + reject(new Error(`child process timed out\nstdout: ${stdout}\nstderr: ${stderr}`)); |
| 49 | + }, 15_000); |
| 50 | + |
| 51 | + child.on('error', reject); |
| 52 | + child.on('close', (code) => { |
| 53 | + clearTimeout(timeout); |
| 54 | + resolve(code ?? 1); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + expect(exitCode).toBe(0); |
| 59 | + expect(stderr).toContain('child-done'); |
| 60 | + |
| 61 | + const nonEmptyStdoutLines = stdout.split('\n').filter((line) => line.trim().length > 0); |
| 62 | + for (const line of nonEmptyStdoutLines) { |
| 63 | + expect(isJsonRpcLine(line)).toBe(true); |
| 64 | + } |
| 65 | + |
| 66 | + expect(stdout).not.toMatch(/INFO\s+@wdio\/utils:/); |
| 67 | + expect(stdout).not.toMatch(/INFO\s+webdriver:/); |
| 68 | + }, 20_000); |
| 69 | +}); |
0 commit comments