RFC: Leverage --incremental
See original GitHub issueThis is a feature request.
Issue
Our team is working on a large codebase and we are currently having issues with the time our CI takes. We are looking at all the ways to make the build faster. One of the bottlenecks is the time it takes to run the tests. We investigated and most of time taken to run unit tests is not to actually run the tests but to compile the TS files on-the-fly.
Expected behavior
The thing is that TS files have already been compiled as part of the build step. We would love for ts-jest to leverage this and rely on them.
One idea is to have ts-jest write the compiled files on disk and leverage the incremental
option. As ts-jest attempts to recompile the code, tsc will see that most of the work was already done.
We understand this is a large undertaking and I am ready to help! From a quick glance I already identified ts-jest performs additional transformation to hoist jest.mock()
and that could be a challenge.
Alternatives
We found none of the speed-up alternatives to be satisfying. They are:
- Compile the test files ahead of time. It works great but we lose snapshots and it’s a poor developer experience. Not a good fit.
- Compile with babel instead of ts-jest. It’s still slow, it doesn’t support all of TypeScript features, the TDD experience is not as good and the code that is tested in not the one that actually runs in production. Not a good fit.
We would largely prefer to stick with ts-jest but performance improvements are really necessary for us. Again, I’m ready to work on it.
What do you think about this feature, and would you support a PR introducing it?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:45
- Comments:27 (2 by maintainers)
Top GitHub Comments
FYI, perhaps later we can learn from Angular CLI regarding to this topic.
@yacinehmito I managed somehow to use create incremental program for
ts-jest
but I’m not sure if I did it correctly. If you have time, would you please help me to check my experiment ? You can find it on this branch. I made a folder calledcompiler
and you can look intoprogram.ts
.To run the experiment codes,
ts-jest
config needs to haveand
tsconfig
needs to haveincremental: true
I did after you told me about this. Thank you for the info.
It confirms the level of coupling. I did a PoC of a runner with support for
--incremental
and it turned out to be a bit slower thants-jest
. Withts-jest
, what we lose in speed by not using--incremental
, we gained thanks to its aggressive caching.The issue is that it’s this very caching that makes us stop using
ts-jest
. We have quite a large TypeScript project (almost 300k lines of code) andts-jest
completely chokes on it. Even with a v8 set at 4GB of RAM it blows up. The culprit is the in-memory cache. We are relying on our PoC instead.The PoC is less smart than
ts-jest
and a bit more hacky: it’s a runner combined with a transformer. The runner first compiles the project on disk with--incremental
. Because it’s a runner it happens only once; there are no concurrent compilation. Then the transformer, instead of compiling the TS files, will just read the compiled JS file from disk, so it’s quite fast.In the end we ended up with something that is just slightly slower than ts-jest but simpler to reason about and with no pressure on the RAM.
The biggest challenge for the introduction of incremental in
ts-jest
is the TypeScript transformer to hoist calls ofjest.mock
. If we implement it naïvely, we might end up with running test files that were already on disk because they didn’t build as part of a jest run, hence not having the hoistedjest.mock
calls.Takeaway: I don’t think I’ll be working on that feature for now. Sorry.