Uh oh!
There was an error while loading. Please reload this page.
test_runner: add getTestContext() public API - #62501
Conversation
nodejs-github-bot
commented
Mar 30, 2026
Review requested:
|
5aaa29d to
3e9232bCompareUh oh!
There was an error while loading. Please reload this page.
3e9232b to
28d1af6Comparenodejs-github-bot
commented
Mar 30, 2026
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@## main #62501 +/- ##
==========================================
+ Coverage 89.69% 89.71% +0.01%
==========================================
Files 692 692 Lines 214039 214061 +22 Branches 41064 41066 +2 ==========================================
+ Hits 191985 192037 +52 + Misses 14134 14098 -36 - Partials 7920 7926 +6
🚀 New features to boost your workflow:
|
ljharb
left a comment
There was a problem hiding this comment.
I think this could be used by malicious code to behave well in tests, and then behave badly in prod. it'd be different if it was like t.getContext(), though, because malicious code wouldn't have access to t. As it is, it feels like an implicit global communications channel, and those are not advisable.
Only requesting changes to ensure that with multiple stamps, it doesn't land until this item is discussed.
benjamingr
commented
Mar 31, 2026
@ljharb wdym? It's for tracing so you can get a consistent trace how is that related to behavior change compared to prod? It's just the "frameworkness" of a test-runner |
MoLow
commented
Mar 31, 2026
@ljharb not sure who uses
|
ljharb
commented
Mar 31, 2026
I'm not saying they'd use node:test in prod. I'm saying that this allows code to behave differently in tests versus not in tests because it creates the capability to determine if you're in tests or not.
right - that's internal state of the test runner that I don't think is safe to expose implicitly. I'm perfectly happy with the motivating use case - just not granting all code in the node process the ability to intercept it. Could this information be exposed in a more limited fashion, like in a loader or something? |
MoLow
commented
Mar 31, 2026
As I said this is already possible in user land tody, it just makes it less verbose. there is no new vector, just better DX. import{createHook,executionAsyncId}from'node:async_hooks';consttestResources=newMap<number,any>();functiongetTestContext(){returntestResources.get(executionAsyncId());}createHook({init(asyncId,type,triggerAsyncId,resource: any){if(type==='Test'&&'diagnostic'inresource&&typeofresource.diagnostic==='function'){testResources.set(asyncId,resource);return;}constparent=testResources.get(triggerAsyncId);if(parent){testResources.set(asyncId,parent);}},}).enable();this PR just simplifies this and makes it more accurate (uses instanceof instead of |
ljharb
commented
Mar 31, 2026
I wasn't aware of this - if you're saying that async_hooks already did the thing I think is a very bad idea, then rather than improve the DX for it, i'd prefer to see that capability removed from async_hooks. Code should not be able to undeniably distinguish whether it's running in tests or not. I suppose an alternative would be to keep the capability but disable it by default, or provide an env var that can disable it? |
MoLow
commented
Mar 31, 2026
I dont think it is easy to disable, this is just how async hooks work in node. frankly I also dont understand the concern about being able to determing you are inside test mode (there are a dozen other easier ways to know that, such as enviroment variables we inject, |
benjamingr
commented
Mar 31, 2026
You can already do that though? (Even without internal (but existant) stuff like NODE_TEST_CONTEXT, or parsing the stack trace - a user can easily define an environment variable (or any global or module value really) in a This actually has (some) legitimate use cases - typically in realms of telemetry (no visible side effect) code |
benjamingr
commented
Mar 31, 2026
I suspect it would be a big break - tooling like ide extensions already relies on this (which is reasonable imo) |
ljharb
commented
Mar 31, 2026
env vars and process properties can be altered by me before running my test suite if i want to prevent my deps from distinguishing. My concern isn't about the test author being able to enable this - that's fine! It's about the test author being unable to prevent it. All the "proper" use cases of this are of course legitimate - but it would be nice if we didn't have to enable a new category of attack vector just to satisfy some legit use cases. |
MoLow
commented
Mar 31, 2026
@ljharb the concerns you are rasing seem unrelated to this PR then, it does not enable anything new, juts makes it more accesible |
ljharb
commented
Mar 31, 2026
That turns out to be the case, yes. However, making a harmful thing more accessible is usually not a good thing either :-/ Certainly consider my block removed, but I strongly urge you to reconsider this. |
Uh oh!
There was an error while loading. Please reload this page.
nodejs-github-bot
commented
Apr 1, 2026
Landed in c5a83ab |
PR-URL: #62501 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This makes |
PR-URL: #62501 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
PR-URL: #62501 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
PR-URL: #62501 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
PR-URL: #62501 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Exposes
getTestContext()function to access test context information from within tests and async operations.My use case was exposing a pino logger that writes to test diagnostics, but there can be many other usecases: