Ts-jest: Support for imports with absolute path

Created on 28 Nov 2016  ·  11Comments  ·  Source: kulshekhar/ts-jest

Due to issue #58 I found out that unfortunately imports like

import { reposLoaded, repoLoadingError } from 'containers/App/actions';

don't work with jest and produces next error:

app\containers\HomePage\tests\sagas.test.ts
  ● Test suite failed to run

    Cannot find module 'containers/App/actions' from 'sagas.ts'

      at Resolver.resolveModule (node_modules\jest-resolve\build\index.js:151:17)
      at Object.<anonymous> (app\containers\HomePage\sagas.ts:8:19)
      at Object.<anonymous> (app\containers\HomePage\tests\sagas.test.ts:7:17)

While normal imports with relative paths behave as expected. It could be problem of jest-resolve module, but we have to try to solve it inside ts-jest or find what exactly causes this problem in jest-resolve.
As always we have to start from tests for this case.

Enhancement

Most helpful comment

I don't see anywhere this module passing options to jest-resolve. Putting the extra moduleDirectories in package.json jest object works for my case:

"jest": {
    "moduleDirectories": ["node_modules", "src"],

All 11 comments

Will passing the baseUrl in tsconfig.json to jest-resolve moduleDirectories do this?

Yup, manually overriding moduleDirectories in jest-resolve module worked for me. Putting together a pull request.

I don't see anywhere this module passing options to jest-resolve. Putting the extra moduleDirectories in package.json jest object works for my case:

"jest": {
    "moduleDirectories": ["node_modules", "src"],

@mohsen1, thank you fro your investigation and moving issues to Jest itself.
I guess we have to add your current workaround to readme and close this issue, because it's not related to ts-jest.
Is it would be ok for you?

Yes. Just adding that config option works.

Closed due to #77

What if this is still an Issue?

@marcusjwhelan the best would be to create a minimal repo that reproduces this and open a new issue with a link to the repo

@kulshekhar , would this repo be too large? binary-type-tree. It really only has to do with the tsconfigs and package.json

@marcusjwhelan it's better than nothing :)

That said, the smaller a repo is, the quicker the issue is likely to be fixed

The weird thing I see in nestjs is when you use "moduleDirectories": ["node_modules", "./"], the resolution fail and throw exception, but it works when "moduleDirectories": ["node_modules", "./"]

I create two branch for it
https://github.com/chungchi300/typescript-starter/tree/use-src-path-in-config - working, src
https://github.com/chungchi300/typescript-starter - failure, using ./

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qm3ster picture qm3ster  ·  3Comments

Vinnl picture Vinnl  ·  3Comments

japhar81 picture japhar81  ·  3Comments

remcohaszing picture remcohaszing  ·  4Comments

GeeWee picture GeeWee  ·  4Comments