jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.28k stars 507 forks source link

Tests fail on absolute import/TS path alias #699

Closed ildar-icoosoft closed 4 years ago

ildar-icoosoft commented 4 years ago

Current Behavior

npm run test failed because of absolute import path aliases in tested code.

Steps to reproduce the error:

  1. npx tsdx create mylib (select "react" option)
  2. create some file in /src folder. For example: /src/config.ts with following content:

export const a = 1;

  1. Import constant "a" in /src/index.tsx file. Here is my /src/index.tsx file content:
    
    import * as React from 'react';
    import {a} from "config";

// Delete me export const Thing = () => { console.log(a); return

the snozzberries taste like snozzberries
; };

4. run `npm run test` in mylib folder
5. You will see failed test:
![tsdx](https://user-images.githubusercontent.com/29747197/80406361-6319b300-88dd-11ea-8674-46ba33841bad.png)

### Expected behavior

<!-- A clear and concise description of what you expected to happen. -->

test shouldn't fail because of absolute paths in scripts in /src folder.

### Suggested solution(s)

<!-- How could we solve this bug? What changes would need to made to TSDX? -->

I know If I replace `import {a} from "config";` with `import {a} from "./config";` then this error will be fixed. But tsconfig.json allows me to use absolute paths. From tsconfig.json:

"paths": { "": ["src/", "node_modules/*"] },


And my IDE automatically adds absolute paths. And absolute paths works if I run `npm run build` or `npm start`. But it doesn't work in testing.

Give the opportunity to use absolute paths in tests. Or take away the opportunity to use absolute paths in `/src` folder

### Additional context

<!-- Add any other context about the problem here.  -->

### Your environment

<!-- PLEASE FILL THIS OUT -->

| Software         | Version(s) |
| ---------------- | ---------- |
| TSDX           | "^0.13.2"
| TypeScript       | "^3.8.3"
| Browser          | Last Chrome
| npm/Yarn         | npm 6.12.0
| Node             |  v12.13.0
| Operating System | Win 10
agilgur5 commented 4 years ago

Duplicate of #91 and issues labeled with TS Paths Aliases

Can read my summary of the situation in https://github.com/jaredpalmer/tsdx/issues/91#issuecomment-597989579, but I'll address some of the points here:

And absolute paths works if I run npm run build or npm start

It actually doesn't work, all absolute imports get treated as externals, so the build output will be very broken.

Give the opportunity to use absolute paths in tests. Or take away the opportunity to use relative paths in /src folder

That's the topic of #91 et al. There is a workaround listed there, but it is fairly complex because you have to configure tsconfig.json, .babelrc, and jest.config.js to get it to work.

Unfortunately the incorrect use of paths is very common with TS users as I've linked to in the summary and those patterns made their way into the templates too 😕 . I'm planning to remove them and potentially add full support for aliases at a later date. They are fundamentally broken, but they've been around for a while (I didn't write them), so doing so is potentially breaking, but we've got a few breaking changes already lined up so it'll be a bit (maybe v0.15?). As it's a template change, I might be able to release as patch in v0.13.x (which has been mostly about templates) though, pending if you consider the templates to be an "API" or not.