Ts-jest: type script reference path is not getting resolved in jest

Created on 13 Apr 2020  ·  14Comments  ·  Source: kulshekhar/ts-jest

In one of the ts file (query_ctrl.ts) the module import are like below

///

import { QueryCtrl } from 'app/plugins/sdk';
export class MyQueryCtrl extends QueryCtrl
{ .....

In module.ts
export {
MyQueryCtrl as QueryCtrl
};
While in jest test module.test.ts
import { QueryCtrl } from '../../datasource/module'

● Test suite failed to run

Cannot find module 'app/plugins/sdk' from 'query_ctrl.ts'

Require stack:
src/datasource/query_ctrl.ts
src/datasource/module.ts
src/specs/datasource/module.test.ts

6 | import { Utils } from './Utils';

7 | import { QueryCtrl } from 'app/plugins/sdk';
| ^

9 |
10 |

at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:296:11)
at Object. (src/datasource/query_ctrl.ts:7:1)

jest.config.js

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
coverageDirectory: "work/coverage",
};

Bug Confirmed

Most helpful comment

I will close this by a proper fix, for now you can use that workaround 👍

All 14 comments

Its something out of scope for jest # https://github.com/facebook/jest/issues/9805

You can try moduleNameMapper in jest config. The error is about jest can’t find your file with that path. This issue is not ts-jest issue.

I have tried with moduleNameMapper
moduleNameMapper: {
"app/plugins/sdk" : "/node_modules/grafana-sdk-mocks/app/headers/common.d.ts",
}
But it didn't work for me

you need to set proper rootDir to be able to use moduleNameMapper. But I don't think using type definition directly like that will work. Because moduleNameMapper only works with ts or js.

In general, when using moduleNameMapper, it should be not pointing to d.ts. Type declaration file is helpful for type checking and IDE suggestion, but not for resolving import modules

ok, i did, it too did not work.
moduleNameMapper: {
"app/plugins/sdk" : "/node_modules/grafana-sdk-mocks/app/headers/common.d.ts",
}
So any way i can make ts-jest work ? same time grunt-typescript is workinng

why don't you point to common.ts or common.js ?

unfortunately, I don't see the corresponding .ts file. I found only the .d.ts file where all the classes are getting export https://github.com/grafana/grafana-sdk-mocks/blob/master/app/headers/common.d.ts

hmm do you mind share a minimum repo so I can take a look ? :)

https://github.com/pallabrath/ts-jest-Issue1523
npx jest will reproduce the issue.
I have tried both with moduleNameMapper and without.

ok I found workaround for your case.

  • Adjust your jest.config.js to:
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ["<rootDir>"],
  moduleNameMapper: {
    "app/plugins/sdk" : "<rootDir>/node_modules/grafana-sdk-mocks/app/plugins/sdk.ts",
  },
  transformIgnorePatterns: [
    'node_modules/(?!grafana-sdk-mocks/.*)',
  ],
};
  • Install version 25.2.1 of ts-jest
  • Clear cache
  • Try to run test again (worked for me)

Explanation

  • transformIgnorePatterns is needed in this case, otherwise you will get error from typescript
import {PanelCtrl} from '../features/panel/panel_ctrl';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
  • There seems to be a regression in 25.3.x in your scenario, use 25.2.1 works.

Can you please try ?

Thanks .. Its working now @ahnpnl you are awsome 💯

I will close this by a proper fix, for now you can use that workaround 👍

@pallabrath I just checked your problem against ts-jest master. Actually PR #1507 already fixed this issue. So no extra PR needed for this. I will close this as already fixed.

Hi @pallabrath , you can use ts-jest 25.4.0 now. However, the jest.config.js I provided before has to be like that. v25.4.0 has the same behavior as v25.2.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikeyakymenko picture mikeyakymenko  ·  3Comments

RiJung picture RiJung  ·  4Comments

ahnpnl picture ahnpnl  ·  3Comments

Slessi picture Slessi  ·  3Comments

qm3ster picture qm3ster  ·  3Comments