Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Allow usage of tsm instead of ts-node? #11989

Open
karlhorky opened this issue Oct 22, 2021 · 18 comments
Open

[Feature]: Allow usage of tsm instead of ts-node? #11989

karlhorky opened this issue Oct 22, 2021 · 18 comments

Comments

@karlhorky
Copy link
Contributor

馃殌 Feature Proposal

Jest allows for configuration in TypeScript via ts-node:

https://github.com/facebook/jest/blob/7f39f0a589703191a0cd3c6db558920281f7195f/docs/Configuration.md?plain=1#L37

It would be great to also enable support for tsm

Motivation

  • tsm is lighter weight and faster than ts-node
  • if projects are already using tsm for other tasks, it would be nice to not have to also install ts-node only for Jest

Example

No response

Pitch

Not entirely sure I understand this field, but maybe:

It fits Jest's ideology of being fast and enabling modern techniques

@SimenB
Copy link
Member

SimenB commented Oct 22, 2021

PR very much welcome!

@jensmeindertsma
Copy link

jensmeindertsma commented Nov 2, 2021

The disadvantage to using tsm is that there is no programmatic way to invoke it. This is a problem because then we need to spawn a child process in which we run node --loader tsm jest.config.ts to import the config file. Then we must pass back the imported config to Jest. This can't be done across process boundaries without sacrificing support for function config files.

What I propose instead:

  1. Use @swc/cli in a child process like this: npx swc jest.config.,ts -o /node_modules/jest-config/_jest.config.js where /node_modules/jest-config/_jest.config.js is a temporary file.
  2. require("/node_modules/jest-config/_jest.config.js") which gives us the config object.
  3. Delete the temporary file.
    -> Now we have the config object available.

I chose swc because of the stability. I know there is also esbuild which tsm uses, and they are similar in size.

@SimenB would you accept a PR implementing this approach?

@SimenB
Copy link
Member

SimenB commented Nov 2, 2021

would that work if said file imported another ts file? or will swc bundle?

@jensmeindertsma
Copy link

jensmeindertsma commented Nov 2, 2021

If said file imported another TS file, it wouldn't work. I know esbuild can do bundling, that might be an option. I don't know many cases in which you might import from other files though, but yeah, I'd be happy to use esbuild instead and do bundling. Let me know what you prefer 鈽猴笍

@SimenB
Copy link
Member

SimenB commented Nov 2, 2021

@swc/core has a bundle export: https://www.runpkg.com/?@swc/core@1.2.106/index.js#229

(I don't care if swc or esbuild is chosen, I've never used either and haven't cared to check any of them out)

@jensmeindertsma
Copy link

jensmeindertsma commented Nov 2, 2021

Ah, okay. I'll look into it. Just to confirm, you are open to a PR removing ts-node and implementing support for jest.config.ts files with swc or esbuild?

@SimenB
Copy link
Member

SimenB commented Nov 2, 2021

I don't think we should remove ts-node (but as it would be a breaking change, but also since I don't think any of the alternatives does any type checking?), but making it configurable in some way (--config-loader=blah or some such?) sounds reasonable

@jensmeindertsma
Copy link

What would be breaking about removing ts-node? Depending on it's type-checking features would be one? I can do a CLI option

@SimenB
Copy link
Member

SimenB commented Nov 2, 2021

For people currently using ts-node it would stop working - it's a peer dependency, not a dependency

@jensmeindertsma
Copy link

Ah okay. I will implement esbuild under a flag

@jensmeindertsma
Copy link

What about a fallback mechanism where Jest will try to parse the config file with esbuild if ts-node is not installed?

@milesj
Copy link

milesj commented Nov 4, 2021

If config loading happens within a CJS context, something like this would work just fine: https://boostlib.dev/docs/module#commonjs-requires

@github-actions
Copy link

github-actions bot commented Nov 4, 2022

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Nov 4, 2022
@karlhorky
Copy link
Contributor Author

Not stale? There is also now another alternative called tsx

@github-actions github-actions bot removed the Stale label Nov 5, 2022
@SimenB
Copy link
Member

SimenB commented Nov 7, 2022

Current thinking: #13143 (comment)

I.e. allowing a user to specify which loader to use.

@LinusU
Copy link
Contributor

LinusU commented Feb 1, 2023

Both tsm and tsx support would be provided by #13521 馃帀

Copy link

github-actions bot commented Feb 1, 2024

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 1, 2024
@karlhorky
Copy link
Contributor Author

Not stale

@github-actions github-actions bot removed the Stale label Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants