0

I am trying to use different jest configs for different testing (visual/unit). So in package.json, I used --project config option:

"projects": [
  "tests/jest.unit.config.js",
  "tests/jest.visual.config.js"
]

and the scripts:

"test": "jest --projects tests/jest.unit.config.js --coverage",
"coverage:visual": "jest --projects tests/jest.visual.config.js --coverage"

When I created two separate configurations in corresponding files, it had worked well. However, I need to use a base configuration to further simplify the two config files and extract what they had in common, so I created a third file jest.config.js in addition to the configs in two files and in package.json:

const config = {
  bail: 0,
  verbose: true,
  collectCoverage: false,
  rootDir: "../",
  moduleFileExtensions: [
    'js',
    'json',
    'ts',
    'tsx'
  ],
  moduleDirectories: [
    'node_modules',
    '<rootDir>'
  ],
  resolver: '<rootDir>/tests/__setup__/resolver.ts',
  setupFilesAfterEnv: [
    '<rootDir>/tests/__setup__/setup-env.ts'
  ],
  testEnvironment: 'jsdom',
  testPathIgnorePatterns: [
    '<rootDir>/tests/__setup__/*',
    '<rootDir>/tests/__fixtures__/*'
  ],
  transformIgnorePatterns: [
    '/node_modules/(?!(@yext/search-headless-react)/)'
  ],
  moduleNameMapper: {
    './SearchCore': '<rootDir>/tests/__fixtures__/core/SearchCore.ts',
    '\\.(css|less|scss|sass)$': 'identity-obj-proxy'
  },
  resetMocks: true,
  restoreMocks: true
};

module.exports = config;

and I wanted to import and extend it into the two configs files I had like following:

const {config} = require('jest.base.config')
module.exports = {
  ...config,
  collectCoverageFrom: [
    'src/**'
  ],
  coverageDirectory: 'coverage/unit',
  testMatch: [
    '<rootDir>/**/*.test.ts?(x)'
  ]
};

However, I get the error:

Error: Cannot find module 'tests/jest.base.config.js'
Require stack:
- /Users/search-ui-react/tests/jest.unit.config.js
- /Users/search-ui-react/node_modules/jest-util/build/requireOrImportModule.js
- /Users/search-ui-react/node_modules/jest-util/build/index.js
- /Users/search-ui-react/node_modules/jest-config/build/getCacheDirectory.js
- /Users/search-ui-react/node_modules/jest-config/build/Defaults.js
- /Users/search-ui-react/node_modules/jest-config/build/normalize.js
- /Users/search-ui-react/node_modules/jest-config/build/index.js
- /Users/search-ui-react/node_modules/jest-cli/build/init/index.js
- /Users/search-ui-react/node_modules/jest-cli/build/cli/index.js
- /Users/search-ui-react/node_modules/jest-cli/build/index.js
- /Users/search-ui-react/node_modules/jest-cli/bin/jest.js
- /Users/search-ui-react/node_modules/jest/bin/jest.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.resolve (node:internal/modules/cjs/helpers:108:19)
    at Object.<anonymous> (/Users/kzhou/search-ui-react/tests/jest.unit.config.js:1:26)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at requireOrImportModule (/Users/kzhou/search-ui-react/node_modules/jest-util/build/requireOrImportModule.js:53:28)

I tried require('jest.base.config.js') as well as require('tests/jest.base.config.js') since it is in a tests folder, but am getting the same thing. Also, I could not use ESM syntax with import from\export because I had the error: SyntaxError: Cannot use import statement outside a module.

bloomsdayforever
  • 263
  • 2
  • 13

1 Answers1

0

Try require('./tests/jest.base.config.js') (dot+slash before dirname)

If that doesn't work, I'd try getting ESM imports to work and see if that fixes your issues

Tyler Dane
  • 951
  • 1
  • 14
  • 25