(ctx: ContextRPC)
| 12 | * Should be called as early as possible when worker/process has been set up. |
| 13 | */ |
| 14 | export function setupInspect(ctx: ContextRPC) { |
| 15 | const config = ctx.config |
| 16 | const isEnabled = config.inspector.enabled |
| 17 | |
| 18 | if (isEnabled) { |
| 19 | inspector = __require('node:inspector') |
| 20 | // Inspector may be open already if "isolate: false" is used |
| 21 | const isOpen = inspector.url() !== undefined |
| 22 | |
| 23 | if (!isOpen) { |
| 24 | inspector.open( |
| 25 | config.inspector.port, |
| 26 | config.inspector.host, |
| 27 | config.inspector.waitForDebugger, |
| 28 | ) |
| 29 | |
| 30 | if (config.inspectBrk) { |
| 31 | const firstTestFile = typeof ctx.files[0] === 'string' |
| 32 | ? ctx.files[0] |
| 33 | : ctx.files[0].filepath |
| 34 | |
| 35 | // Stop at first test file |
| 36 | if (firstTestFile) { |
| 37 | session = new inspector.Session() |
| 38 | session.connect() |
| 39 | |
| 40 | session.post('Debugger.enable') |
| 41 | session.post('Debugger.setBreakpointByUrl', { |
| 42 | lineNumber: 0, |
| 43 | url: pathToFileURL(firstTestFile), |
| 44 | }) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | const keepOpen = shouldKeepOpen(config) |
| 51 | |
| 52 | return function cleanup(): void { |
| 53 | if (isEnabled && !keepOpen && inspector) { |
| 54 | inspector.close() |
| 55 | session?.disconnect() |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | export function closeInspector(config: SerializedConfig): void { |
| 61 | const keepOpen = shouldKeepOpen(config) |
no test coverage detected