@jsenv/test 3.7.35 → 3.7.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,6 +45,7 @@ export const createRuntimeUsingPlaywright = ({
45
45
  onConsole,
46
46
  onRuntimeStarted,
47
47
  onRuntimeStopped,
48
+ onAllocatedMsRequested = () => {},
48
49
  teardownCallbackSet,
49
50
  isTestPlan,
50
51
 
@@ -166,6 +167,11 @@ ${webServer.rootDirectoryUrl}`);
166
167
  }
167
168
 
168
169
  const page = await browserContext.newPage();
170
+ // the only thing the page can tell node before it is done executing;
171
+ // see runtime_browsers/client/request_allocated_ms.js
172
+ await page.exposeFunction("__jsenv_request_allocated_ms__", (ms) => {
173
+ onAllocatedMsRequested(ms);
174
+ });
169
175
  if (!isBrowserDedicatedToExecution) {
170
176
  page.on("close", () => {
171
177
  onRuntimeStopped();
@@ -55,6 +55,7 @@ export const nodeChildProcess = ({
55
55
  onConsole,
56
56
  onRuntimeStarted,
57
57
  onRuntimeStopped,
58
+ onAllocatedMsRequested = () => {},
58
59
 
59
60
  measureMemoryUsage,
60
61
  onMeasureMemoryAvailable,
@@ -162,6 +163,15 @@ export const nodeChildProcess = ({
162
163
  onRuntimeStopped();
163
164
  }),
164
165
  );
166
+ cleanupCallbackSet.add(
167
+ onChildProcessMessage(
168
+ childProcess,
169
+ "allocated-ms-request",
170
+ ({ ms }) => {
171
+ onAllocatedMsRequested(ms);
172
+ },
173
+ ),
174
+ );
165
175
 
166
176
  const removeOutputListener = installChildProcessOutputListener(
167
177
  childProcess,
@@ -57,6 +57,7 @@ export const nodeWorkerThread = ({
57
57
  onConsole,
58
58
  onRuntimeStarted,
59
59
  onRuntimeStopped,
60
+ onAllocatedMsRequested = () => {},
60
61
 
61
62
  measureMemoryUsage,
62
63
  onMeasureMemoryAvailable,
@@ -143,6 +144,15 @@ export const nodeWorkerThread = ({
143
144
  onRuntimeStopped();
144
145
  }),
145
146
  );
147
+ cleanupCallbackSet.add(
148
+ onWorkerThreadMessage(
149
+ workerThread,
150
+ "allocated-ms-request",
151
+ ({ ms }) => {
152
+ onAllocatedMsRequested(ms);
153
+ },
154
+ ),
155
+ );
146
156
 
147
157
  const stop = memoize(async () => {
148
158
  // read all stdout before terminating
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Asks the test runner for more time from a test file, when the amount is
3
+ * computed rather than known in advance (it depends on the platform, on how
4
+ * many fixtures were found...). A fixed amount belongs in a directive instead,
5
+ * which the runner reads without executing the file:
6
+ *
7
+ * "jsenv:allocate 90s";
8
+ *
9
+ * The request is sent to the process running the test plan, which restarts the
10
+ * timeout with the requested duration and remembers it: a file asking for more
11
+ * time than the others is also a file worth starting early when parallelizing.
12
+ *
13
+ * Node.js runtimes only; a browser has no channel to reach the test plan while
14
+ * the file is executing.
15
+ */
16
+
17
+ import { parentPort } from "node:worker_threads";
18
+
19
+ export const requestAllocatedMs = (ms) => {
20
+ if (typeof ms !== "number") {
21
+ throw new TypeError(`requestAllocatedMs expects a number, got ${ms}`);
22
+ }
23
+ const message = {
24
+ __jsenv__: "allocated-ms-request",
25
+ data: JSON.stringify({ ms }),
26
+ };
27
+ if (parentPort) {
28
+ parentPort.postMessage(message);
29
+ return;
30
+ }
31
+ if (process.send && process.connected) {
32
+ process.send(message);
33
+ return;
34
+ }
35
+ // file executed on its own (node ./file.test.mjs): there is no allocated time
36
+ };