ts-jest
ts-jest copied to clipboard
[Bug]: Getting TS error when importing with subpath import
Version
29.0.3
Steps to reproduce
- Clone my Repo at https://github.com/shiftcode/ts-jest-issue/tree/esm-investigation-mts
- Follow instruction in README
Expected behavior
I expect to see the two test cases run & pass successfully (commons/util.spec.mts
& app/main.spec.mts
) as seen by running test:tsc-and-jest
in root of project.
Actual behavior
Only one test passes. The other one ( commons/util.spec.mts
) will fail since compilation can't be completet.
Debug log
FAIL src/main.spec.mts ● Test suite failed to run
src/main.spec.mts:3:33 - error TS2792: Cannot find module '@lab/commons/helpers'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
3 import {nodeMaxHeaderSize} from '@lab/commons/helpers'
Additional context
As suggested by the error message I tried to change the moduleResolution
field in tsconfig
which did not help. Also tried with moduleNameMapper
in jest config.
Environment
System:
OS: macOS 12.5.1
CPU: (10) arm64 Apple M1 Max
Binaries:
Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
npmPackages:
jest: ^29.2.1 => 29.2.1
Did you check the doc https://kulshekhar.github.io/ts-jest/docs/guides/esm-support#support-mts-extension ?
@ahnpnl yes I tried with the tsconfig
options documented in the doc and also took into account the other things mentioned there
I may be having this same issue, but tbh, I thought my issue was ts-jest not properly parsing ESM modules. I'm not able to import multiformats
package in my tests. I have no issues with the package imported in my project (although I'm using esbuild for the builds, so it's probably doing some magic there), but when trying to run the tests, all I get is cannot find module 'multiformats/cid'
.
Seeing this issue, and given I'm importing only subpaths from multiformat
s package.. maybe I was wrong and the problem comes from this instead.
I did start with a jest.config.ts
file like the one shown in the documentation stated above, and tried many things until my file looks something like this (note it does not work either... it's just for reference, to note I've been trying different things, all without success):
import type { JestConfigWithTsJest } from 'ts-jest'
const jestConfig: JestConfigWithTsJest = {
// [...]
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
testEnvironment: 'node',
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
'^.+\\.m?[tj]sx?$': [
'ts-jest',
{
useESM: true,
},
],
'node_modules/multiformats/.+\\.(j|t)sx?$': 'ts-jest'
},
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!multiformats/.*)'
],
}
export default jestConfig
My tsconfig.json
file looks more or less the same than the documentation link above, and my package.json
file has type: "module"
defined too.
I've just now realized the package CID
I was trying to import is exported from multiformat's index file, so I tried changing that, and I still have import errors... Maybe am getting a different issue (?) 🤔
Edit: Just to clarify. Although I said I'm using esbuild for the builds, I also tried using ts-node
to run the code, and simply specifying --esm
when running ts-node
makes the code work importing everything as expected.
@elboletaire Did you find a solution? I have the same error
Test suite failed to run
Cannot find module '../../support/lib/toastIsDisplayed.js' from 'src/pages/desktop/contractPage.js'
Require stack:
src/pages/desktop/contractPage.js
src/support/check/checkContainsText.js
test/support/check/checkContainsText.spec.js
8 | import DtApp from '../common/dtappPage.js';
@lacell75 TBH I did not find a solution. In my case, the package I was using changed something related to how the bundle was made, and downgrading such package (multiformats) to the previous version before those changes worked.
I spent an embarrassing amount of time with that issue and cannot spend more time on it. So I'm still not sure if it's a ts-jest issue or only happens to multiformats, sorry 😞
literally copy/pasted the example scripts/configs etc and all I get is:
TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at pathsToModuleNameMapper (/Users/dom/Projects/scripts/node_modules/ts-jest/dist/config/paths-to-module-name-mapper.js:28:39)
at file:///Users/dom/Projects/scripts/jest.config.js:11:21
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
at async requireOrImportModule (/Users/dom/Projects/scripts/node_modules/jest-util/build/requireOrImportModule.js:55:32)
at async readConfigFileAndSetRootDir (/Users/dom/Projects/scripts/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:112:22)
at async readInitialOptions (/Users/dom/Projects/scripts/node_modules/jest-config/build/index.js:396:13)
Even changed the relative imports there to point to ts-jest etc. Nothing seems to work. I hate modules in JS