Can I run a TypeScript Jest test file without any transpilation to JavaScript whatsoever?

  Kiến thức lập trình

Context:
I was having trouble with aliasing imports. The exact same aliases would work in some files but not in others. I found this problem to be to do with the JavaScript that my code was compiled to.

I changed the way I ran my project (specifically, I use ts-node and tsconfig-paths/register now) which has fixed the problem I had.

However, there has indeed been a catch.

In my src/ directory I have managed to get the aliasing of imports working simply because I am not transpiling my code to JavaScript anymore when I run it. But, my tests are written in Jest. I run them using npx jest. Running with npx jest involves transpilation to JavaScript, and when I run them they import from src files which have the aliased imports.

Therefore, I run into the same problems I had: When I run them they import from src files which have the aliased imports.

My question is, can I run these jest test files without having any transpilation to JavaScript along the way? I have not found a way to do this so far.

Or, is there another method I should be using?

Thank you so much to anyone who can help me.

P.S. If changing the way I ran my project isn’t the correct solution to the problem of aliased imports not working despite being the same in different files in the same directory, please educate me on better ways. I fixed this but I am still interested to know if there are better ways.
For example: import {A} from @enums/E would work in X.ts but not work in Y.ts, despite X.ts and Y.ts being in the same directory.

LEAVE A COMMENT