Jest: Resolve TypeScript modules using baseUrl

Created on 21 Nov 2016  路  1Comment  路  Source: facebook/jest

Do you want to request a feature or report a bug?

Bug I think.

What is the current behavior?

Jest seems to ignore TypeScript's baseUrl when doing module resolution. baseUrl was introduced with TypeScript 2.0: https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#base-url

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install and npm test.

https://github.com/unindented/jest/tree/master/examples/typescript

Running tsc -p . on that directory shows that TypeScript can compile the project fine. Running npm test makes Jest throw errors like this one:

Cannot find module 'utils/sum' from 'sum-test.ts'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:151:17)
      at Object.<anonymous> (__tests__/sum-test.ts:2:13)

What is the expected behavior?

If tsc can compile it, I'd expect jest to be able to run it too.

Run Jest again with --debug and provide the full configuration it prints. Please mention your node and npm version and operating system.

$ node --version ; npm --version ; npm test -- --debug
v7.1.0
3.10.9

> @ test /private/tmp/jest/examples/typescript
> jest "--debug"

jest version = 17.0.3
test framework = jasmine2
config = {
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js"
  ],
  "transform": [
    [
      "^.+\\.(ts|tsx)$",
      "/private/tmp/jest/examples/typescript/preprocessor.js"
    ]
  ],
  "testRegex": "/__tests__/.*\\.(ts|tsx|js)$",
  "rootDir": "/private/tmp/jest/examples/typescript",
  "name": "-private-tmp-jest-examples-typescript",
  "setupFiles": [],
  "testRunner": "/private/tmp/jest/examples/typescript/node_modules/jest-jasmine2/build/index.js",
  "automock": false,
  "bail": false,
  "browser": false,
  "cacheDirectory": "/var/folders/mm/jk7g3fgx3q18vhcrhl8wq93h0000gn/T/jest",
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "expand": false,
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "resetMocks": false,
  "resetModules": false,
  "snapshotSerializers": [],
  "testEnvironment": "jest-environment-jsdom",
  "testPathDirs": [
    "/private/tmp/jest/examples/typescript"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testURL": "about:blank",
  "timers": "real",
  "transformIgnorePatterns": [
    "/node_modules/"
  ],
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": true,
  "watchman": true
}

Most helpful comment

This is not a bug. Jest is not aware of your tsconfig. You can set modulePaths though.
Hope that helps!

>All comments

This is not a bug. Jest is not aware of your tsconfig. You can set modulePaths though.
Hope that helps!

Was this page helpful?
0 / 5 - 0 ratings