-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Description
🚀 Feature Proposal
Jest provides once-global setup and teardown, and per-file setup and teardown, but conspicuously absent is per-worker setup and teardown.
Motivation
One worker's set of tests runs serially, and is ideally suited to reusing an exclusive resource like a database or redis connection.
Example
Sue has a test suite that uses a database. She can create multiple databases, but there is a time cost associated with spinning one up before it can be used.
From her test suite she'd like to get maximum concurrency with minimum database setup time.
Currently possible approaches:
- One database, run all the tests serially.
- This is unacceptably slow. Concurrency is one of Jest's killer features and it's sad to lose it.
- Multiple databases, created on fly at runtime, per-file. (in e.g.
beforeAll()
)- This pays the database setup cost many more times than is necessary, slowing down the test suite.
- Multiple databases, created upfront. (e.g., make 20 databases, once, in
globalSetup
)- This requires you to choose a constant (e.g. 20) according to how many workers you want to run.
- Choose the wrong number for
-w
(or get the wrong number inferred by your CPU core count), and it will either do too much work or not have enough databases. - Also it's painful to wait on a large upfront cost when you're running, say, just one test.
Ideally, instead:
- Multiple databases, created once per-worker
- This is the best-case for minimizing database setup and maximizing reuse, and doesn't require you to premeditate the number of databases.
- You can use whatever
-w
you want (or let Jest infer it), and you'd get the exact right number of databases. - When you run just one test, you'll get just one database.
Pitch
Why does this feature belong in the Jest core platform?
I'm not sure where the boundary of core platform is, but it stands to reason that beforeWorker
and afterWorker
should be provided by the same system that provides beforeAll
, beforeEach
, and globalSetup
.
Alternatives
I googled around but couldn't come up with another way to achieve this. Is there another (perhaps undocumented) way to hook into worker setup/teardown?