|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | 3 | const path = require("path"); |
| 4 | +const fs = require("graceful-fs"); |
| 5 | +const rimraf = require("rimraf"); |
4 | 6 | const ProfilingPlugin = require("../lib/debug/ProfilingPlugin"); |
5 | 7 |
|
6 | 8 | describe("Profiling Plugin", () => { |
@@ -52,3 +54,124 @@ describe("Profiling Plugin", () => { |
52 | 54 | return profiler.destroy(); |
53 | 55 | }); |
54 | 56 | }); |
| 57 | + |
| 58 | +// Optional dependency: the browser end-to-end check only runs where puppeteer-core |
| 59 | +// (and a Chrome it can launch) are present. puppeteer-core needs Node >= 18, and |
| 60 | +// loading it under the memory-limited Bun/Deno workers is wasteful, so only require |
| 61 | +// it where the check can actually run; it self-skips everywhere else. See #17234. |
| 62 | +const globalScope = /** @type {{ Bun?: unknown, Deno?: unknown }} */ ( |
| 63 | + globalThis |
| 64 | +); |
| 65 | +const onBunOrDeno = Boolean(globalScope.Bun) || Boolean(globalScope.Deno); |
| 66 | +const nodeMajor = Number.parseInt(process.versions.node, 10); |
| 67 | + |
| 68 | +/** @type {typeof import("puppeteer-core") | undefined} */ |
| 69 | +let puppeteer; |
| 70 | +if (!onBunOrDeno && nodeMajor >= 18) { |
| 71 | + try { |
| 72 | + const name = "puppeteer-core"; |
| 73 | + puppeteer = require(name); |
| 74 | + } catch (_err) { |
| 75 | + puppeteer = undefined; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @typedef {{ frame: string, parent?: string }} TraceFrame |
| 81 | + * @typedef {{ name: string, args: { data: { frames: TraceFrame[] } } }} TraceEvent |
| 82 | + */ |
| 83 | + |
| 84 | +describe("ProfilingPlugin in real Chrome", () => { |
| 85 | + /** @type {import("puppeteer-core").Browser | undefined} */ |
| 86 | + let browser; |
| 87 | + |
| 88 | + beforeAll(async () => { |
| 89 | + if (!puppeteer || onBunOrDeno || nodeMajor < 18) return; |
| 90 | + try { |
| 91 | + /** @type {import("puppeteer-core").LaunchOptions} */ |
| 92 | + const launchOptions = { |
| 93 | + headless: true, |
| 94 | + args: ["--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage"] |
| 95 | + }; |
| 96 | + // Use an explicit binary when provided, otherwise the installed Chrome. |
| 97 | + if (process.env.PUPPETEER_EXECUTABLE_PATH) { |
| 98 | + launchOptions.executablePath = process.env.PUPPETEER_EXECUTABLE_PATH; |
| 99 | + } else { |
| 100 | + launchOptions.channel = "chrome"; |
| 101 | + } |
| 102 | + browser = await puppeteer.launch(launchOptions); |
| 103 | + } catch (_err) { |
| 104 | + // No usable Chrome in this environment — the test self-skips below. |
| 105 | + browser = undefined; |
| 106 | + } |
| 107 | + }, 120000); |
| 108 | + |
| 109 | + afterAll(async () => { |
| 110 | + if (browser) await browser.close(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should generate a trace Chrome DevTools can load (#17234)", (done) => { |
| 114 | + if (!browser) { |
| 115 | + console.warn("Skipping: could not launch Chrome via puppeteer-core."); |
| 116 | + return done(); |
| 117 | + } |
| 118 | + |
| 119 | + // Narrowed handle so the type survives into the callbacks below. |
| 120 | + const activeBrowser = browser; |
| 121 | + |
| 122 | + const webpack = require(".."); |
| 123 | + |
| 124 | + const outputPath = path.join(__dirname, "js/profiling-chrome"); |
| 125 | + const eventsPath = path.join(outputPath, "events.json"); |
| 126 | + |
| 127 | + rimraf(outputPath, () => { |
| 128 | + const compiler = webpack({ |
| 129 | + context: __dirname, |
| 130 | + entry: "./fixtures/a.js", |
| 131 | + output: { path: path.join(outputPath, "dist") }, |
| 132 | + plugins: [new webpack.debug.ProfilingPlugin({ outputPath: eventsPath })] |
| 133 | + }); |
| 134 | + compiler.run(async (err) => { |
| 135 | + if (err) return done(err); |
| 136 | + try { |
| 137 | + /** @type {TraceEvent[]} */ |
| 138 | + const events = JSON.parse(fs.readFileSync(eventsPath, "utf8")); |
| 139 | + const page = await activeBrowser.newPage(); |
| 140 | + // Run Chrome DevTools' trace bootstrap (MetaHandler) in the real |
| 141 | + // browser: iterate the TracingStartedInBrowser frames and pick the |
| 142 | + // parent-less main frame. A missing `frames` array threw |
| 143 | + // "frames is not iterable" and the whole trace failed to load. |
| 144 | + const result = await page.evaluate( |
| 145 | + (/** @type {TraceEvent[]} */ evs) => { |
| 146 | + const event = evs.find( |
| 147 | + (e) => e && e.name === "TracingStartedInBrowser" |
| 148 | + ); |
| 149 | + if (!event) return { ok: false, threw: null, mainFrame: null }; |
| 150 | + /** @type {string | null} */ |
| 151 | + let threw = null; |
| 152 | + /** @type {string | null} */ |
| 153 | + let mainFrame = null; |
| 154 | + try { |
| 155 | + for (const frame of event.args.data.frames) { |
| 156 | + if (!frame.parent) mainFrame = frame.frame; |
| 157 | + } |
| 158 | + } catch (err_) { |
| 159 | + threw = err_ instanceof Error ? err_.message : String(err_); |
| 160 | + } |
| 161 | + return { ok: threw === null, threw, mainFrame }; |
| 162 | + }, |
| 163 | + events |
| 164 | + ); |
| 165 | + await page.close(); |
| 166 | + |
| 167 | + expect(result.threw).toBeNull(); |
| 168 | + expect(result.ok).toBe(true); |
| 169 | + expect(typeof result.mainFrame).toBe("string"); |
| 170 | + done(); |
| 171 | + } catch (err_) { |
| 172 | + done(err_); |
| 173 | + } |
| 174 | + }); |
| 175 | + }); |
| 176 | + }, 120000); |
| 177 | +}); |
0 commit comments