Skip to content Skip to sidebar Skip to footer

Set Absolute Path For Jest In React Project

I have followed this document and set an absolute path across the project. But when I run test case it gives me following error Your application tried to access assets, but it is

Solution 1:

I added the following configuration as part of craco file and worked for me

module.exports = {
  jest: {
    configure: {
      moduleNameMapper: {
        // Jest module mapper which will detect our absolute imports.'^assets(.*)$': '<rootDir>/src/assets$1',
        '^pages(.*)$': '<rootDir>/src/pages$1',
        '^config(.*)$': '<rootDir>/src/config$1',
        '^navigation(.*)$': '<rootDir>/src/navigation$1',
        '^utils(.*)$': '<rootDir>/src/utils$1',
        '^components(.*)$': '<rootDir>/src/components$1',
        '^services(.*)$': '<rootDir>/src/services$1',

        // Another example for using a wildcard character'^~(.*)$': '<rootDir>/src$1'
      }
    }
  }
}

reference - https://resir014.xyz/posts/2019/03/13/using-typescript-absolute-paths-in-cra-20

Post a Comment for "Set Absolute Path For Jest In React Project"