How to silence `unexpected invalid pattern running all tests instead` Warning when overriding CRA Jest config?

Viewed 1421

Whenever I use yarn test in my create-react-app based React app, I encounter a long invalid test pattern warning before my tests run. Those tests run well but that awkward warning is not that convenient. Here are two concerns I have:

  • Is there something I am missing in my config that causes it?
  • Is there a way to silence the warning?

Warning


$ yarn test
yarn run v1.22.10
$ react-scripts test -- --config=jest.config.ts

  Invalid testPattern --config=jest.config.ts|--watch|--config|{"roots":["<rootDir>\\src"],"collectCoverageFrom":["src\\**\\*.{js,jsx,ts,tsx}","!src\\
**\\*.d.ts"],"setupFiles":node_modules\\react-app-polyfill\\jsdom.js"],"setupF
ilesAfterEnv":[],"testMatch":["<rootDir>\\src\\**\\__tests__\\**\\*.{js,jsx,ts,tsx}","<rootDir>\\src\\**\\*.{spec,test}.{js,jsx,ts,tsx}"],"testEnviron
ment":"jsdom","testRunner":"\node_modules\\jest-circus\\runner.js","transform":{"
^.+\\.(js|jsx|mjs|cjs|ts|tsx)$"\\node_modules\\react-scripts\\config\\jest\\cs
sTransform.js","^(?!.*\\.node_modules\jest-environment-jsdom\build\index.js supplied. Running all tests instead.
  Invalid testPattern --config=jest.config.ts|--watch|--config|{"roots":["<rootDir>\\src"],"collectCoverageFrom":["src\\**\\*.{js,jsx,ts,tsx}","!src\\
**\\*.d.ts"],"setupFiles":node_modules\\react-app-polyfill\\jsdom.js"],"setupF
ilesAfterEnv":[],"testMatch":["<rootDir>\\src\\**\\__tests__\\**\\*.{js,jsx,ts,tsx}","<rootDir>\\src\\**\\*.{spec,test}.{js,jsx,ts,tsx}"],"testEnviron
ment":"jsdom","testRunner":"\node_modules\\jest-circus\\runner.js","transform":{"
^.+\\.(js|jsx|mjs|cjs|ts|tsx)$"\\node_modules\\react-scripts\\config\\jest\\cs
sTransform.js","^(?!.*\\.node_modules\jest-environment-jsdom\build\index.js supplied. Running all tests instead.

Jest config


// jest.config.ts

const jestConfig = {
  preset: 'ts-jest',
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  verbose: true,
  testMatch: ['<rootDir>/test/**/*(*.)+(test).+(tsx|ts)'],
  setupFiles: ['dotenv/config'],
  setupFilesAfterEnv: [
    '<rootDir>/test/setupTests.ts',
  ],
  moduleFileExtensions: ['js', 'ts', 'tsx'],
  collectCoverage: true,
  coverageDirectory: 'target/coverage',
  collectCoverageFrom: [
    'src/**/*.tsx',
  ],
  moduleNameMapper: {
    '^.+\\.(css|scss)$': 'identity-obj-proxy',
    '^.+\\.(png|svg|pdf|jpg|jpeg)$': 'jest-transform-stub',
    '^@foo/(.*)': '<rootDir>/src/$1',
  },
};

export default jestConfig;

Dependencies

  • jest@26.6.2
  • react@17.0.2
  • yarn@1.22.10

Binaries

  • Node: 14.7.0
  • Yarn: 1.22.10
  • npm: 6.14.11
1 Answers

Your use of -- is specifically saying that everything following it is in fact a file pattern. So --config=jest.config.ts is not being interpreted as an option at all and the confusing error arises from the react-scripts test command trying to understand it as a file pattern. Options would have to come BEFORE the --

See https://www.baeldung.com/linux/double-dash-in-shell-commands

Related