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

Tests fail on absolute import/TS path alias #699

Closed
ildar-icoosoft opened this issue Apr 27, 2020 · 1 comment · Fixed by #707
Closed

Tests fail on absolute import/TS path alias #699

ildar-icoosoft opened this issue Apr 27, 2020 · 1 comment · Fixed by #707
Labels
solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: TS Paths Aliases Related to using aliases with TypeScript paths

Comments

@ildar-icoosoft
Copy link

ildar-icoosoft commented Apr 27, 2020

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;
3. 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 <div>the snozzberries taste like snozzberries</div>;
};

  1. run npm run test in mylib folder
  2. You will see failed test:
    tsdx

Expected behavior

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

Suggested solution(s)

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

Your environment

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 agilgur5 changed the title Unit tests failed if testing code contains relative import path Unit tests failed if testing code contains absolute import/TS path alias Apr 27, 2020
@agilgur5
Copy link
Collaborator

agilgur5 commented Apr 27, 2020

Duplicate of #91 and issues labeled with TS Paths Aliases

Can read my summary of the situation in #91 (comment), 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.

@agilgur5 agilgur5 added solution: duplicate This issue or pull request already exists topic: TS Paths Aliases Related to using aliases with TypeScript paths solution: workaround available There is a workaround available for this issue labels Apr 27, 2020
@agilgur5 agilgur5 changed the title Unit tests failed if testing code contains absolute import/TS path alias Tests fail on absolute import/TS path alias Apr 27, 2020
@agilgur5 agilgur5 added this to the v0.13.x milestone May 3, 2020
@agilgur5 agilgur5 linked a pull request May 3, 2020 that will close this issue
Repository owner locked as resolved and limited conversation to collaborators Aug 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: TS Paths Aliases Related to using aliases with TypeScript paths
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants