skip to Main Content

My jest config

  clearMocks: true,
  rootDir: '../../',
  cacheDirectory: 'cache',
  testEnvironment: 'jsdom',
  modulePaths: [
    '<rootDir>'
  ],
  setupFilesAfterEnv: [
    '<rootDir>/config/jest/setup.ts'
  ],
  moduleDirectories: [
    'node_modules', 'src'
  ],
  moduleNameMapper: {
    '\.(jpg|jpeg|png|gif|svg)$': '<rootDir>/config/jest/mocks/file.js',
    '\.s?css$': 'identity-obj-proxy'
  },
  moduleFileExtensions: [
    'js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node'
  ],
  roots: [ '<rootDir>/src' ],
  transformIgnorePatterns: [
    '/node_modules/',
    '\.pnp\.[^\/]+$',
    '/vendor/'
  ],
  globals: {
    __IS_DEV__: false
  },
  testMatch: [
    '**/__tests__/**/*unit.[jt]s?(x)',
  ],

I import import { counterReducer, CounterStateSchema } from 'entities/Counter';

All working fine when I rename the directory entities to any other directory. For Example [‘aaaa’, ‘bbbb’, ‘entity’] and others.

 PASS  src/aaaa/Counter/ui/__tests__/Counter.unit.tsx
   Counter
     ✓ test render Counter (106 ms)

But the directory named is entities test fails with error:

 FAIL  src/entities/Counter/ui/__tests__/Counter.unit.tsx
  ● Test suite failed to run

  Cannot find module 'entities/Counter' from 'src/app/config/redux/index.ts'

All other import working fine in my project and I have no idea, what it could be.

All other in my project working fine. Webpack, Storybook and so on.

I can’t change the directory "entities" to other because I use Feature-Sliced Design. According to this methodology there should be this folder.

If I use relative path, it’s work

-----script app/config/redux/index.ts------
import { counterReducer, CounterStateSchema } from '../../../entities/Counter';
----test result---
 PASS  src/entities/Counter/ui/__tests__/Counter.unit.tsx
  Counter
   ✓ test render Counter (102 ms)

2

Answers


  1. Chosen as BEST ANSWER

    I found a bug in the jest-environment-jsdom package. Reported the bug to the jest team. Link to the bug here


  2. I have installed the repo in your comment in the github issue (really helpful btw).
    Check your node_modules folder and you see package with name entities

    As you use absolute path ‘entities/someModule’, jest resolves in node_modules first and finds! entities folder inside, but there is not someModule subfolder.

    Why delete of jest-environment-jsdom did help?

    It turned out that jest-environment-jsdom has following list of deps:

    jest-environment-jsdom -> @types/jsdom -> parse5 -> entities.

    So there is a reason why delete of jest-environment-jsdom helped. Entities folder is deleted from node_modules and jest resolves in src folder.
    Use relative path also fixed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search