avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

ava, ~, and tsconfig-paths #2537

Closed jupl closed 4 years ago

jupl commented 4 years ago

I am following the instructions as noted here: https://github.com/avajs/ava/blob/master/docs/recipes/typescript.md#using-module-path-mapping

With the following tsconfig.json:

{
    "baseUrl": ".",
    "paths": {
        "~/*": ["src/*"]
    }
}

When I have a file /some/project/src/users/test.ts like this:

import {something} from '~/users/util'

And inside /some/project/src/users/util.ts I have something like this:

import {other} from '~/common/util'

I get an error like this:

Error: Cannot find module '/some/project/src/users/~/common/util'

The path should resolve to /some/project/src/common/util.

If I change ~ to - I get the same error. If I change ~ to something else like _ in tsconfig.json and test.ts it works just fine. Also I have no issue using ~ with tsconfig-paths in other test libraries like Mocha and Jest. In fact, this is the first time I've come across this issue with ava.

EDIT: Almost gave up but I got more details. It seems to occur when my test file imports a file that imports from ~. If my test file is the only one that uses import from ~ then it works fine.

In other words this is okay:

// /some/project/src/users/test.ts
import {something} from '~/users/util'

// /some/project/src/users/util.ts
export const something = 'ok'

But this is not:

// /some/project/src/users/test.ts
import {something} from '~/users/util'

// /some/project/src/users/util.ts
import {other} from '~/common/util' // when running ava import attempts to resolve to /some/project/src/users/~/common/util
export const something = other

// /some/project/src/common/util.ts
export const other = 'ok'
novemberborn commented 4 years ago

I take it you're using ts-node/register and tsconfig-paths/register? Are you combining this with @ava/typescript? (You shouldn't be.)

I can't really help with this I'm afraid, I don't use those tools. Improvements to the recipe are welcome though.

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

jupl commented 4 years ago

I'm not using @ava/typescript. This is a major deal breaker for me as I use parcel, which uses ~ for path resolution.

jupl commented 4 years ago

I think it makes no sense for this to be closed. You said you can't help, but if you keep this closed the visibility of this issue is limited as perhaps someone who may have insight into this will less likely see this.

novemberborn commented 4 years ago

if you keep this closed the visibility of this issue is limited as perhaps someone who may have insight into this will less likely see this

Open or closed, it's been my experience in this project that it makes no difference either way. I'll see about getting access to GitHub's new Discussions feature which would be a better fit for resolving this.

jupl commented 4 years ago

Ok. Well I added some more details.

CraigglesO commented 2 years ago

I fought this for a few hours. I don't do it EXACTLY this way, but I found that ava + typescript + modules + paths was a nightmare. I switched to vitest and it immediately worked out of the box. I would recommend to anyone going the typescript test path to just use vitest.