ts-jest icon indicating copy to clipboard operation
ts-jest copied to clipboard

[Bug]: Getting TS error when importing with subpath import

Open michaelwittwer opened this issue 2 years ago • 7 comments

Version

29.0.3

Steps to reproduce

  1. Clone my Repo at https://github.com/shiftcode/ts-jest-issue/tree/esm-investigation-mts
  2. 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

michaelwittwer avatar Oct 21 '22 14:10 michaelwittwer

Did you check the doc https://kulshekhar.github.io/ts-jest/docs/guides/esm-support#support-mts-extension ?

ahnpnl avatar Oct 21 '22 14:10 ahnpnl

@ahnpnl yes I tried with the tsconfig options documented in the doc and also took into account the other things mentioned there

michaelwittwer avatar Oct 24 '22 06:10 michaelwittwer

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

elboletaire avatar Nov 16 '22 12:11 elboletaire

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 (?) 🤔

imatge

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 avatar Nov 16 '22 12:11 elboletaire

@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 avatar Dec 16 '22 15:12 lacell75

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

elboletaire avatar Dec 16 '22 15:12 elboletaire

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

intellix avatar Dec 29 '22 16:12 intellix