module-alias
module-alias copied to clipboard
+ Jest
Aliases not working with Jest. Has someone managed to make it work?
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.
not work
same here, anyone got a solution for this?
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"
},
}
i had try this way many days ago,but still not works. Maybe that's bug of my jest
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?
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
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.
I couldn't make jest work with this, any ideas?
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)
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 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)
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',
},
};
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.
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?