Jest config not picked in git submodule npm package from root

Viewed 220

For an RN app. I have a project structure like

root git repo
|-jest.config
|-package.json
|-screens/
|--git submodule
|--git submodule
|--.lintstagedrc

one repo at root with package.json and jest.config and multiple git submodules in screens folder

Each submodule is a npm package which is symlinked in node modules and added in package.json pointing to the git branch.

Now what i wanted to do was add a precommit hook in each submodule and use the root configurations for jest to run when anything is committed in the submodule.

I am using husky and have defined a .lintstagedrc file in the screens folder which would be picked while running husky.

{
  "./**/app/**/*.{js,jsx,ts,tsx}": [
    "eslint --fix --ext .js,.jsx,.ts,.tsx --plugin tsc --rule 'tsc/config: [2, {configFile: \"../../../tsconfig.json\"}]'",
    "jest  --config ../../../jest.config.js --bail --forceExit --passWithNoTests --findRelatedTests"
  ]
}

i am trying to set the jest config using the args however when the commit hook runs it does not pick the config and gives a type error for RN polyfill files

Syntax Error node_modules/@react-native/polyfills/error-guard.js: Missing semicolon. (14:4)
 > 14 | type ErrorHandler = (error: mixed, isFatal: boolean) => void;

The same runs correctly if the files are not part of submodule and directly in the root repo(while being at the same path)

when running npx jest --showConfig it is correctly showing the required jest config. but running jest always fails.

How do i get jest to pick the current config and assume the root folder for config is the root repo and not the submodule npm package as i suggest having a package.json defined in submodule might be causing issues

Or that given the submodule is symlinked to node_modules folder at root might be the issue.

Jest config(payments is an example submodule)

module.exports = {
  // The preset is a node environment that mimics the environment of a React Native  app. Because it doesn't load any DOM or browser APIs, it greatly improves Jest's startup time.
  verbose: true,
  preset: 'react-native',
  // It helps you greatly improve the test speed.It does so by creating a cache of compiled modules so that it doesn't have to compile the node_modules every time we run tests.
  cacheDirectory: './cache',
  // Defines the files which we want to skip while generating coverage reports.
  // coveragePathIgnorePatterns: ['./src'],
  // Defines the threshold limit for all the tests to pass.If the coverage is less than the defined limit, the tests would fail.This helped us in keeping code coverage high throughout development.
  coverageThreshold: {
    global: {
      // global thresholds
      branches: 23,
      functions: 25,
      lines: 32,
      statements: 32,
    },
  },

  globals: {
    'ts-jest': {
      diagnostics: false,
    },
  },

  collectCoverageFrom: [
    './src/**/*.js',
    './src/**/*.ts',
    './src/**/*.tsx',
    '!**/node_modules/**',
    '!**/vendor/**',
  ],

  testPathIgnorePatterns: ['src/.+((?:.ignored)|(?:mock)).(ts|js)x?'],

  transform: {
    '^.+\\.(ts|tsx|js|jsx)$': 'babel-jest',
  },

  // All the npm modules which need to be transpiled are added here.These modules are basically ES6/ 7 modules.
  transformIgnorePatterns: [
    '/node_modules/(?!(jest-)?@?react-native|payments|react-clone-referenced-element|react-navigation|@react-native-community|rn-range-slider|victory|react-native-svg|recoil)',
  ],
  setupFilesAfterEnv: [
    '<rootDir>/src/tests/setupTests.js',
    '<rootDir>/node_modules/react-native-gesture-handler/jestSetup.js',
    '@testing-library/jest-native/extend-expect',
  ],
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],

  moduleNameMapper: {
    '\\.svg': '<rootDir>/src/tests/mocks/svgMock.js',
    '^src/(.*)': '<rootDir>/src/$1',
    '^config/(.*)': '<rootDir>/config/$1',
    '^app/(.*)': '<rootDir>/app/$1',
    '^setup/(.*)': '<rootDir>/setup/$1',
    '^payments/(.*)': '<rootDir>/src/screens/payments/$1',
  },

  notify: true,
  notifyMode: 'always',
};
0 Answers
Related