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

Support tsconfig paths mapping similar to Jest's moduleNameMapper #4904

Closed
ackvf opened this issue Jul 26, 2022 · 7 comments
Closed

Support tsconfig paths mapping similar to Jest's moduleNameMapper #4904

ackvf opened this issue Jul 26, 2022 · 7 comments
Labels
type: question support question

Comments

@ackvf
Copy link

ackvf commented Jul 26, 2022

Is your feature request related to a problem or a nice-to-have?? Please describe.

In a TypeScript project that uses custom paths aliases in tsconfig.json, it is not possible to import files using e.g. @abi/something, which is mapped in tsconfig.json like this

    "paths": {
      "@abi/*": ["abi/*"]
    }

Describe the solution you'd like
The custom paths are supported by jest using modulenamemapper

https://jestjs.io/docs/configuration#modulenamemapper-objectstring-string--arraystring

Describe alternatives you've considered
using hardhat plugins

@ackvf ackvf added the type: feature enhancement proposal label Jul 26, 2022
@ackvf
Copy link
Author

ackvf commented Jul 26, 2022

@ackvf
Copy link
Author

ackvf commented Jul 26, 2022

So, I actually think that the issue was caused not by the @abi path alias, but by the fact that I was importing a json file directly, which works in the project thanks to tsconfig resolveJsonModule: true, but it doesn't seem to work in mocha tests.

@ackvf
Copy link
Author

ackvf commented Jul 26, 2022

Well, this is weird, I don't really understand what is going on.

this doesn't work

import AABI from '@abi/ExampleABI.json'

this works

import AABI from '../abi/ExampleABI.json'

However, this also seems to be working, although it might be that it is just a type.

import type { ExampleABI } from '@typings/abi'

What is going on? How can I use imports with path aliases and json files?

@JoshuaKGoldberg
Copy link
Member

Sounds like this is a question more than anything else. Closing as questions should go in Discord or StackOverflow. Good luck!

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg added type: question support question and removed type: feature enhancement proposal labels Dec 27, 2023
@ackvf
Copy link
Author

ackvf commented Jan 3, 2024

So, path aliases are supported by mocha, am I reading this right?

Because in my previous experiment, it was clear that

this doesn't work

import AABI from '@abi/ExampleABI.json'

and this works

import AABI from '../abi/ExampleABI.json'

@JoshuaKGoldberg
Copy link
Member

(re-opening to put back in the triage queue)

@JoshuaKGoldberg
Copy link
Member

Ok, yes, coming back to this now that I know more about Mocha: you're correct that Mocha doesn't natively support path aliases. Mocha directly runs JavaScript files. It doesn't set up fancy virtual environments with mapped names the way, say, Jest's moduleNameMapper does.

Mocha does support using Node.js-style loaders that can modify files on the fly - including rewriting import paths and/or transpiling TypeScript syntax to pure JavaScript. mochajs/mocha-examples#75 -> mochajs/mocha-examples#76 shows a couple examples of that.

If that examples repo & its open PRs doesn't include an example of using Mocha+TypeScript the way you're looking for, I'd suggest filing an issue on the repo. There's definitely a lot missing at the moment. Cheers! 🤎

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question support question
Projects
None yet
Development

No branches or pull requests

2 participants