aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/vitest/dist/chunks/inspector.CvyFGlXm.js
blob: 99fb8437a02c2a70c0eb79b641f550220203b902 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { createRequire } from 'node:module';
import { pathToFileURL } from 'node:url';

const __require = createRequire(import.meta.url);
let inspector;
let session;
/**
* Enables debugging inside `worker_threads` and `child_process`.
* Should be called as early as possible when worker/process has been set up.
*/
function setupInspect(ctx) {
	const config = ctx.config;
	const isEnabled = config.inspector.enabled;
	if (isEnabled) {
		inspector = __require("node:inspector");
		if (!(inspector.url() !== void 0)) {
			inspector.open(config.inspector.port, config.inspector.host, config.inspector.waitForDebugger);
			if (config.inspectBrk) {
				const firstTestFile = typeof ctx.files[0] === "string" ? ctx.files[0] : ctx.files[0].filepath;
				// Stop at first test file
				if (firstTestFile) {
					session = new inspector.Session();
					session.connect();
					session.post("Debugger.enable");
					session.post("Debugger.setBreakpointByUrl", {
						lineNumber: 0,
						url: pathToFileURL(firstTestFile)
					});
				}
			}
		}
	}
	const keepOpen = shouldKeepOpen(config);
	return function cleanup() {
		if (isEnabled && !keepOpen && inspector) {
			inspector.close();
			session?.disconnect();
		}
	};
}
function closeInspector(config) {
	const keepOpen = shouldKeepOpen(config);
	if (inspector && !keepOpen) {
		inspector.close();
		session?.disconnect();
	}
}
function shouldKeepOpen(config) {
	// In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
	return config.watch && config.isolate === false && config.maxWorkers === 1;
}

export { closeInspector as c, setupInspect as s };