Cannot find module. Jest testing

введите сюда описание изображения

SignIn.test.tsx

import { render, screen } from "@diary-app/shared"
import { SignIn } from "../ui-SignIn/SignIn"


test('test', () => {
    render(<SignIn/>)
    screen.debug()
})

tsconfig.base.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "strict": true,
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "lib": ["es2020", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@diary-app/SignIn": ["libs/web/auth/SignIn/src/index.ts"],
      "@diary-app/SignUp": ["libs/web/auth/SignUp/src/index.ts"],
      "@diary-app/auth": ["libs/web/auth/src/index.ts"],
      "@diary-app/main": ["libs/web/main/src/index.ts"],
      "@diary-app/prisma": ["libs/backend/prisma/src/index.ts"],
      "@diary-app/shared": ["libs/web/shared/src/index.ts"],
      "@diary-app/task": ["libs/backend/task/src/index.ts"],
      "@diary-app/test": ["test/src/index.ts"],
      "@diary-app/testrtet": ["libs/web/auth/testetet/src/index.ts"],
      "@diary-app/user": ["libs/backend/user/src/index.ts"],
      "auth-api": ["libs/backend/auth/src/index.ts"],
      "shared-backend": ["libs/backend/shared/src/index.ts"],
    }
  },
  "exclude": ["node_modules", "tmp"]
}


jest.config.ts (Корневой)

import { getJestProjects } from '@nx/jest';

export default {
  projects: getJestProjects(),
  transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
    '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
  },
};


jest.config.ts (SignIn)

/* eslint-disable */
export default {
    displayName: 'SignIn',
    preset: '../../../../jest.preset.js',
    transform: {
      '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
      '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
    },
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
    coverageDirectory: '../../../coverage/libs/web/auth/SignIn',
  };
  

jest.preset.js

const nxPreset = require('@nx/jest/preset').default;

module.exports = { ...nxPreset };


Ответы (1 шт):

Автор решения: Maksim Ostroginskiy

Оказывается что jest (Как я понял) игнорирует абсолютные пути которые в tsconfig.json, поэтому нужно было абсолютный путь который я использую обернуть в moduleNameMapper в своём jest.config.ts:

import { getJestProjects } from '@nx/jest';

export default {
  projects: getJestProjects(),
  transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
    '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
  },
  moduleNameMapper: {
    '@diary-app/shared': '<rootDir>/libs/web/shared/src/index.ts',
  },
  testEnvironment: "jsdom",
  setupFilesAfterEnv: ["@testing-library/jest-dom", "./jest.setup.ts"],
};





→ Ссылка