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.
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
coverageDirectory: "work/coverage",
};
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" : "
}
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" : "
}
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.
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/.*)',
],
};
ts-jestExplanation
transformIgnorePatterns is needed in this case, otherwise you will get error from typescriptimport {PanelCtrl} from '../features/panel/panel_ctrl';
^^^^^^
SyntaxError: Cannot use import statement outside a module
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
Most helpful comment
I will close this by a proper fix, for now you can use that workaround 👍