-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose an id
for concurrent test runners (like JEST_WORKER_ID
)
#55842
Comments
We could add an environment variable fairly easily, but I have a question: if (process.env.STAGE === "test" && process.env.JEST_WORKER_ID) {
process.env.DB_DATABASE = `myapp_tests_${process.env.JEST_WORKER_ID}`;
} There is already a |
The problem is that, as an external step before the test runner is launched, I:
I don't want to do this for each test file because the migration logic is quite slow. It's much faster to migrate once and have a fast So a UUID, for my use case, would not be appropriate. Having a sequential integer between 1 and |
Got it. Thanks for the explanation. I think we should add support for this. |
I can work on this! will pick this up! |
Thanks @cu8code. Please be sure to handle both cases of test isolation. When |
Hi @cu8code, are you working on this? If not I can give it a try. |
I am working on this 👍 |
Is |
We discussed |
Ahh. The same discussion in multiple places 😅 Is that truly a requirement though? Why is sequentiality actually required? Do you need to be able to guess ids relative to each other? If so, my spidy-senses are tingling. It seems merely having a unique and consistently available id to derive the table name is all you need:
|
I described this in the issue description. TLDR; My test suite interacts with a database. Recreating the database (with all tables) for each test suite is very slow because I'd need to re-run all migrations, which takes several seconds. Therefore, before the test suite runs, I prepare The presence of this feature in Jest, Mocha, and Vtest suggests that many users interested in switching to the node native test runner will want this. |
You can use a "global" |
I suppose that could work; it'd just require some refactoring of how I'm doing things now. With this strategy, though, it seems possible to end up with many orphaned test databases. For example, if I was using a debugging session for a test (very common) and "CTRL+C"-ed out of the process, skipping the global In general, to make it easy for people to switch from the top three most popular other test runners, we should provide the static |
It definitely does work. I've done it. |
@JakobJingleheimer adding more data points from our use case, our test suite has 1,250 individual test files ( Today with Jest, we run (Granted, 5-10 minutes is not great--ofc faster is always better, which is why we're looking into If we did not have determinism in the Granted, this would be only a ~10-20% cost increase in overall As Ben has said, being able to have a deterministic/logical worker id (vs. the physical thread id) is key to letting our Per your musings in #56091 about your I'm always happy to use the latest/best idea for a problem instead of "whatever we did before", but I'm not seeing how "create a new |
Unless I'm missing something, I think this is not new beyond what I understood (or assumed). You must pay a setup cost at some point. At the start of each thread seems an appropriate time (my suggestion). I did something similar at my last job (mongodb in memory) with the same seeding and migrations. For ~450 tests, total run-time was 7.8s, of which ~2.5s was setup. I expect your use-case is not uncommon, so I'd be happy to lay out how to do this in an article. |
Right, the difference is whether the setup cost is once per "change to the migrations/schema" (after which the engineer manually runs Granted, in CI this doesn't matter, because CI has to Appreciate the offer to write up an article, but we don't need to be shown how to do per-worker schema setup/tear down--we could do that if we wanted, we just don't want to, for the reasons we've outlined. :-) |
Do you have some time this week to jump on a quick call? Specifically regarding "change to the migrations/schema". |
Hi @JakobJingleheimer ; we genuinely appreciate the offer to VC! If we were stuck on some super-intricate Node internals issue, it would be invaluable. That said, I don't think there's a lot of unknowns/ambiguities at this point to resolve via discussion; we've described our use case (avoiding per-test/per-worker setup costs), and as Ben lightly alluded to, we think so many other major test frameworks supporting this feature makes it unlikely the ~2-3 of us will come up with a novel solution to something that many others have attempted to solve, and seem to have settled on "stable worker ids" as a suitable solution. It just doesn't seem controversial to us. 🤷 Granted, it's the |
The reason I suggested the call is to understand |
We use node-pg-migrate to manage our db schema, so each time we need a new column / new table / etc, we write a So what If the engineer hasn't changed any While we happen to use But fundamentally engineers "change the schema" (the migration SQL files, the prisma model file, their entity's annotations) an order-or-two-magnitude less often than they run So we'd prefer for the test workers to just assume the |
What is the problem this feature will solve?
When running tests concurrently (the default setting in the Node test runner), it's common practice to split concurrent test runs across multiple local resources (like locally running servers, databases, etc).
Many other popular test runners offer this feature via an environment variable:
jest
:JEST_WORKER_ID
mocha
:MOCHA_WORKER_ID
vitest
:VITEST_POOL_ID
This makes it very easy to set up the proper database connection. For example, here's a snippet from my codebase:
Then, in my database docker container, I create
n
databases, one for each worker. For example, if I runjest
with--maxWorkers 8
, I createmyapp_tests_1
->myapp_tests_8
. During tests, the parallel test suites talk to a different database to avoid deadlocks.What is the feature you are proposing to solve the problem?
The simplest solution for users coming from other frameworks would be to provide an environment variable just like the other test runners (e.g.,
NODE_TEST_WORKER_ID
).However, if that's not feasible, adding a
workerId
to theTestContext
could also be a good solution. This solution is not as ideal as the environment variable because people would need to pass the test context to whatever establishes their db/server/etc connection. In my case, at least, this would require refactoring of code. Right now, I rely on that variable being set at file import time.What alternatives have you considered?
I considered trying to find a solution based on
pid
, but it's not reliable enough. @redyetidev mentioned in Slack that there might be a solution usinghooks
. I have not dug into this yet.The text was updated successfully, but these errors were encountered: