0

ts-jest unable to find a module when using require imports.

const file = require("./file")
const file = require(path.join(__dirname, "file"))

Both cases are failing.

App runs fine on prod and dev. Its only test where require statements are failing to find module.

jest.config.ts

import path from 'path'

const jestConfig = {
  collectCoverage: true,
  testEnvironment: path.join(__dirname, '.jest', 'fastify-env'),
  preset: 'ts-jest',
  testPathIgnorePatterns: ['.d.ts', '.js'],
  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.js$': '$1',
  },
  globals: {
    'ts-jest': {
      isolatedModules: true,
    },
  },
  //   setupFilesAfterEnv: ['./src/test/setup.ts'],
}

export default jestConfig
Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • do you have separate `tsconfig` for dev, test and prod? maybe you're excluding the file somehow – M-Raw Aug 02 '22 at 06:01
  • Have the same config. Was using "require" to prevent cyclic dependency issue on objectionjs joins. But figured that import statement handles cyclic dependency differently than "require" – abhishekbutola Aug 02 '22 at 06:11

0 Answers0