ts-jest: ts-jest fails to import .ts modules from outside project root

  • Issue

ts-jest throws an error whenever a test utilizes a module from outside the project root.

ReferenceError: Unknown plugin "transform-es2015-modules-commonjs" specified in "base" at 0, attempted toresolve relative to "/home/colin/localdev/ts-jest-error/common"

      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:180:17
          at Array.map (<anonymous>)
  • Expected behavior

TS files should be correctly imported even from outside the project.

  • Link to a minimal repo that reproduces this issue

https://github.com/colinskow/ts-jest-transform-error

cd main && npm install && npm test

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 8
  • Comments: 17 (3 by maintainers)

Most upvoted comments

Anyone know any more recent updates on this issue? I know Babel 7 is released now.

@lyy011lyy Works for me too.

I also had to set moduleDirectories in my jest config to something like this:

"moduleDirectories": [
	"node_modules",
	"main/node_modules"
],

We use both. Babel is not used to transpile type-script but as a postprocessing step, to handle some import stuff and e.g. hoist “jest.mock” calls.

I updated the repo on the same branch so you can run npm run build && npm test to test the ts and JavaScript build at the same time. JS passes, TS fails.

I did some research and figured out this is a known bug in Babel which has been fixed, but since it is a breaking change will be available in Babel 7. The choices are to upgrade to babel-core@7.0.0-beta.3 now or wait for the stable release.

I suggest keeping this issue open until the upgrade has been integrated and tested.