Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type script reference path is not getting resolved in jest #1523

Closed
pallabrath opened this issue Apr 13, 2020 · 14 comments · Fixed by #1507
Closed

type script reference path is not getting resolved in jest #1523

pallabrath opened this issue Apr 13, 2020 · 14 comments · Fixed by #1507
Labels
🐛 Bug Confirmed Bug is confirmed

Comments

@pallabrath
Copy link

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",
};

@pallabrath pallabrath added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Apr 13, 2020
@pallabrath
Copy link
Author

Its something out of scope for jest # jestjs/jest#9805

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 13, 2020

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.

@pallabrath
Copy link
Author

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 13, 2020

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

@ahnpnl ahnpnl added 💬 Question and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Apr 13, 2020
@pallabrath
Copy link
Author

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 13, 2020

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

@pallabrath
Copy link
Author

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 13, 2020

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

@pallabrath
Copy link
Author

pallabrath commented Apr 13, 2020

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 13, 2020

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 ?

@pallabrath
Copy link
Author

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

@ahnpnl ahnpnl reopened this Apr 13, 2020
@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 13, 2020

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 13, 2020

@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.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 17, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Confirmed Bug is confirmed
Projects
None yet
2 participants