module-alias icon indicating copy to clipboard operation
module-alias copied to clipboard

+ Jest

Open ivan-ruizrosello opened this issue 5 years ago • 15 comments

Aliases not working with Jest. Has someone managed to make it work?

ivan-ruizrosello avatar Dec 17 '18 09:12 ivan-ruizrosello

I looked into this package for the purpose of using it with Jest and ended up using Jest's own module alias configuration in my jest.config.js file.

This article was helpful: https://medium.com/@justintulk/solve-module-import-aliasing-for-webpack-jest-and-vscode-74007ce4adc9

I think in order to get deep aliasing across the board (Jest, VSCode for autocomplete, etc) it would require configuration in multiple places.

HWideman avatar Jan 04 '19 23:01 HWideman

not work

zhangwilling avatar Mar 12 '19 00:03 zhangwilling

same here, anyone got a solution for this?

ayushnawani avatar Apr 07 '19 17:04 ayushnawani

There is a built in functionality for resolving aliases in Jest. Try adding something like this to package.json or jest.config.js:

  "jest": {
    "moduleNameMapper": {
      "~/(.*)": "<rootDir>/src/$1",
      "~static/(.*)": "<rootDir>/static/$1"
    },
  }

ilearnio avatar Apr 07 '19 18:04 ilearnio

i had try this way many days ago,but still not works. Maybe that's bug of my jest

zhangwilling avatar Apr 11 '19 11:04 zhangwilling

What would I need to do in order to make this module work with Jest? I can't use Jest's moduleNameMapper because it's too basic. I need to have one alias point to multiple targets. Is there any hack of workaround I could use?

Telokis avatar May 13 '19 16:05 Telokis

I am not 100% sure, but it looks like Jest uses it's own implementation of require. So module-alias can't be used with it

ilearnio avatar May 13 '19 16:05 ilearnio

PR #54 supposedly fixes that (according to the author), but I haven't tried yet... I'll try and take a look some time soon.

Kehrlann avatar May 13 '19 19:05 Kehrlann

I couldn't make jest work with this, any ideas?

hosseinalipour avatar May 30 '19 10:05 hosseinalipour

In case it might help anyone, I worked around the issue by creating a custom Jest resolver. (https://jestjs.io/docs/en/configuration#resolver-string)

Telokis avatar May 30 '19 13:05 Telokis

I have tried PR #54, it doesn't help. Mostly because as @ilearnio suggested, Jest uses it's own "Module" implementation.

@Telokis does your resolve make use of module-alias' code or is it something you've reimplemented ?

Kehrlann avatar Jun 01 '19 15:06 Kehrlann

@Kehrlann It's something I've implemented. I don't even use module-alias by itself, I use the function feature to specify the custom resolving by myself. (Because I needed a feature not supported which is one alias pointing to multiple directories)

Telokis avatar Jun 01 '19 16:06 Telokis

My structure is like as this:

  "_moduleAliases": {
    "@models": "src/models/"
  }

I added jest.config.js on my root path with this configuration:

module.exports = {
  moduleNameMapper: {
    '^@models(.*)$': '<rootDir>/src/models$1',
  },
};

lucasteles22 avatar Oct 25 '19 00:10 lucasteles22

nice job, it works.

'^@models(.*)$': '<rootDir>/src/models$1' is ok, but '^@models$': '<rootDir>/src/models' not

i don't know how jest resolve the path, i log the path in test suite and find paths are same.

zhangwilling avatar Oct 28 '19 03:10 zhangwilling

nice job, it works.

'^@models(.*)$': '/src/models$1' is ok, but '^@models$': '/src/models' not

i don't know how jest resolve the path, i log the path in test suite and find paths are same.

Did you ever resolve this?

spock123 avatar Feb 16 '21 17:02 spock123