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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add jest transformer wrapper #142

Open
jasonkuhrt opened this issue Oct 1, 2021 · 10 comments
Open

[Feature] Add jest transformer wrapper #142

jasonkuhrt opened this issue Oct 1, 2021 · 10 comments

Comments

@jasonkuhrt
Copy link

Currently the best way I know how but happy to simplify:

https://github.com/jasonkuhrt/template-typescript-lib/blob/main/jest.config.ts

@nonara
Copy link
Collaborator

nonara commented Oct 1, 2021

Thanks for the suggestion! I had a look, and it looks like it may be possible via https://kulshekhar.github.io/ts-jest/docs/getting-started/options/astTransformers

I'll have to think on this one for a bit. Since ts-jest has a native way of doing this, I generally recommend using their built-in utility. That said, I can see people potentially wanting to use the plugin for the aspects that lie outside of simple path transformation, so I will keep this in mind.

@jasonkuhrt
Copy link
Author

jasonkuhrt commented Oct 2, 2021

Interesting, I did not know about https://kulshekhar.github.io/ts-jest/docs/getting-started/options/astTransformers/#public-transformers.

I'll give this a shot and report back.

If this works, I still think a mention in the docs would be worthwhile.

People using this tool will want it working in Jest, and it's just not obvious how to do that at first. Brief one-liner integration tips here go a long way. If they get out of date, community usually notices and hopefully opens a PR.

@jasonkuhrt
Copy link
Author

jasonkuhrt commented Oct 3, 2021

Hmmmm

It seems that it works by default, just like this:

import type { InitialOptionsTsJest } from 'ts-jest/dist/types'

const config: InitialOptionsTsJest = {
  preset: 'ts-jest',
  globals: {
    'ts-jest': {
      diagnostics: Boolean(process.env.CI),
    },
  },
}

export default config

@nonara
Copy link
Collaborator

nonara commented Oct 3, 2021

Appreciate the update! It occurred to me that it should, but the docs made it seem like they were using babel instead of typescript for compilation.

Glad it works!

@jasonkuhrt
Copy link
Author

jasonkuhrt commented Oct 6, 2021

Update

It seems I was wrong, this is what is needed. Maybe I encountered a cache issue on my end, thinking the reduced code worked when actually it was running the previous.

import type { InitialOptionsTsJest } from 'ts-jest/dist/types'

const config: InitialOptionsTsJest = {
  preset: 'ts-jest',
  globals: {
    'ts-jest': {
      astTransformers: {
        before: ['ts-jest/dist/transformers/path-mapping'],
      },
    },
  },
}

export default config

@nonara
Copy link
Collaborator

nonara commented Oct 6, 2021

Ah ok. Looks like it is using the TS compiler API.

Also... The code looks oddly familiar. 😅
https://github.com/kulshekhar/ts-jest/blob/main/src/transformers/path-mapping.ts 👀

Regarding your request, I think there is some value to natively supporting jest.

This is particularly going to be important because v4 will be multi-functional. Rather than being centered around transforming for tsconfig paths, I'm also introducing the ability to write explicit indexes and/or explicit extensions (for esm compilation output). There will also be a new middleware feature called resolver, which will allow the user to intercept resolution for any module specifier.

Along with that, we're adding full esm support. Lots of crazy gotchas involved in importing from packages with exports, etc.

With that in mind, we've been adding custom loaders and register scripts for node, node with esm, ts-node, etc. I'm going to go ahead and add a jest wrapper to the v4 todo. I will include a documented example, also.

Ultimately, config will likely end up looking like this:

astTransformers: { before: [ 'typescript-transform-paths/jest' ] }

@nonara nonara reopened this Oct 6, 2021
@nonara nonara changed the title Document usage with Jest [Feature] Add jest transformer wrapper Oct 6, 2021
@pz-uc
Copy link

pz-uc commented Jan 10, 2022

Any update on this one? 🤔

Or is there any tutorial how I can make typescript-transform-paths work with ts-jest?
tried for quite a while now and don't find any solution 🤔

Whatever I do ts-jest still can't resolve my modules.

Also ts-jest is officially pointing to this repository to use it in their docs -> https://kulshekhar.github.io/ts-jest/docs/getting-started/options/astTransformers/#public-transformers

@nonara
Copy link
Collaborator

nonara commented Jan 10, 2022

Also ts-jest is officially pointing to this repository to use it in their docs

Interesting! Didn't know that.

is there any tutorial how I can make typescript-transform-paths work with ts-jest?

A few days ago, I had to set a solution up, and I opted to use their AST transformer. It did work. The one problem is it make it so I can't use require while debugging, which was a bit annoying.

Realistically, the best way to make sure it works is to use ts-patch. When you do, you can simply add the plugin config to a tsconfig file for your tests and it will work. This repo's tests actually exemplify how that can work.

All the wrappers and loaders could be avoided if people did, but in effort to broaden support for people who prefer not to add another tool, I'm adding the extras as I'm able. Unfortunately, I've been swamped lately. I do hope to finish this when things slow down in the next major release.

In the mean time, I hope that helps!

@pz-uc
Copy link

pz-uc commented Jan 11, 2022

Interesting! Didn't know that.

Yes, but unfortunately they just link and don't provide any explanation or example, which is really a pity..

A few days ago, I had to set a solution up, and I opted to use their AST transformer. It did work. The one problem is it make it so I can't use require while debugging, which was a bit annoying.

Would you mind sharing your solution? I don't mind if I currently can't debug, as long as ts-jest is able to resolve the paths properly.

Realistically, the best way to make sure it works is to use ts-patch. When you do, you can simply add the plugin config to a tsconfig file for your tests and it will work. This repo's tests actually exemplify how that can work.

Do you have a concrete example how ts-patch works together with ts-jest to resolve the paths? Unfortunately, they also don't have any examples there.

Edit: I think I found a working solution for me leveraging tsconfig-paths

@nonara
Copy link
Collaborator

nonara commented Jan 11, 2022

@pz-uc I should probably clarify the readme. There is one "official" way of using this library and there are now several alternatives, if you really don't want to use the official route.

Here are the steps:

  1. Add ts-patch as a dev dependency
  2. Add prepare script ts-patch install -s
  3. Create a tsconfig.json for your tests and add the plugin config

It's as simple as that. If your typescript is patched, it will work.

I think I found a working solution for me leveraging tsconfig-paths

I actually used to recommend using tsconfig-paths over this for non-compile options. I got a lot of users saying that they didn't want to. Ultimately, it should be a faster solution than applying transformation for in-memory execution. Unless you absolutely need some of the additional transform features of this library, that's the way to go!

Would you mind sharing how you got it to work with jest?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants