Use transpileOnly for ts-node register
Summary
This will skip running TypeScript type checks when loading config files, and will just remove the TypeScript annotations so the code can be read by node directly. This makes Jest startup much more efficient.
Documentation:
https://typestrong.org/ts-node/docs/options/#transpileonly
This could be made even faster by adding the following config, which will use the much faster swc compiler to to the transpilation:
transpiler: 'ts-node/transpilers/swc-experimental'
However, that will require additional dependencies, so it should probably be done on an opt-in basis via a new configuration field, and I do not want to take that on at this time.
Fixes #11337
Test plan
I'm relying on CI to verify this change, please let me know if you would like me to be more thorough.
It looks like there are some tests that expect for type checking to be running here. I'm happy to remove those tests if we want to go in this direction, just let me know!
Why to use TS if type check is skipped? I mean, one could simply go for JS. That will be even faster and will not require dependencies. Or I missed something?
We run the type checking outside of the critical path for our tooling. This gives us the safety of TypeScript without slowing down our tools very much.
In CI this means that we have a job that is dedicated to run type checking for the whole repo. Also TypeScript will give you feedback in editor as you are iterating on the config.
In this setup, having type checking run as part of the start up process of another tool is unnecessary and redundant.
Very good points. What is missing in:
// jest.config.mjs
/** @type {import('jest').Config} */
export default {
reporters: ['default', 'github-actions'],
};
I'm terribly sorry but I don't think I understand what you are asking. Can you please clarify?
Sorry if that was unclear.
I was wondering if you considered using a config file in JS as shown? It can be type checked and provides autocompletion. Also there is no need to involve ts-node, which means better performance. Jest config in Jest repo is setup this way. (There is no type check step, but it is easy to add it.)
Thanks for clarifying, that was very helpful!
Oh yeah, for sure using the JSDoc type annotations is a worthwhile option. However, they are not as expressive as the TypeScript annotations, so we prefer to use the TypeScript syntax wherever possible. Also, we use TypeScript syntax everywhere else, and it is nice to have consistency in our codebase.
I think the kind of change I'm proposing in this PR will help people who prefer to use the TypeScript syntax everywhere instead of the JSDoc annotations without having to stumble into a big performance penalty. Helping to ensure that Jest is fast by default for everyone is a good goal IMO.
Wouldn't something like https://github.com/facebook/jest/pull/13742 be better in that case? or just enabling swc instead of transpileOnly?
Regardless, I'd rather make this configurable than changing our default (and as mentioned in that PR, stop doing anything by default and requiring people to install some sort of loader. That's a future thing, tho).
This PR is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This PR was closed because it has been stalled for 30 days with no activity. Please open a new PR if the issue is still relevant, linking to this one.
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.