ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.76k stars 69 forks source link

module-alias doesn't work with jest #97

Closed BlackoutGG closed 4 years ago

BlackoutGG commented 4 years ago

Use case: I'm trying to test endpoints with jest and super test against an express api backend. My problem being I'm using module-alias to make my requires() a little more sane. Works fine when running it normally, however seen as I throw jest into the mix that's where everything goes pear shaped. From what I read module-alias doesn't work inside dependencies, so I guess it won't resolve within a test being executed by jest.

Here's the error I get: ` Test suite failed to run

Cannot find module '@services' from 'users.unit.test.js'

   7 | });
   8 | 
>  9 | console.log(require("@services"));`

Outside of tests everything works fine.

Kehrlann commented 4 years ago

Hi @BlackoutGG !

Yes that's a know issue ; jest completely overrides the normal import behavior, so module-alias can't do much. However jest has it's own way of defining aliases. That's documented in our README ; we point to Jest docs . I'm afraid you'll have to copy your aliases over into you Jest confing.

Technically you could use a config.js file that would dynamically read your module-alias config from package.json, but unless you maintain a bajillion aliases that's probably not worth it.

Will close the issue.

PS: Your issue made me realize our link was out of date, thanks ^^