1

I've inherited a project that has folders like:

node_modules/
server/
├── node_modules/
├── src/
│   └── helpers/
│       ├── updateTransactions.ts
│       └── updateTransactions.test.ts
├── jest.config.ts
└── tsconfig.json
shared/
└── helpers/
    └── datetime.ts
src/
└── index.tsx
jest.config.ts
tsconfig.json

If I cd into server and run yarn test, I get Cannot find module '../../../shared/helpers/datetime.js' from 'src/helpers/updateTransactions.ts'

I think it's just because Jest does not yet have awareness of how to interpret import { getFormattedDatetimeUtcFromBlockTimestamp } from '../../../shared/helpers/datetime.js'; in updateTransactions.ts.

It's probably a simple configuration issue.

As you can see in various commented-out code here, I've tried all sorts of things:

Ryan
  • 22,332
  • 31
  • 176
  • 357

1 Answers1

0

I think the main change that helped it work was to make my jest.config.ts:

// https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/

/* eslint-disable canonical/filename-match-exported */
import { type JestConfigWithTsJest } from 'ts-jest';

const jestConfig: JestConfigWithTsJest = {
  // [...]
  preset: 'ts-jest/presets/default-esm', // or other ESM presets
  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.js$': '$1',
  },
  transform: {
    // '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
    // '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
    '^.+\\.tsx?$': [
      'ts-jest',
      {
        useESM: true,
      },
    ],
  },
};

export default jestConfig;

See docs: https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/

Ryan
  • 22,332
  • 31
  • 176
  • 357