【问题标题】:How to silence `unexpected invalid pattern running all tests instead` Warning when overriding CRA Jest config?在反应应用程序上运行纱线测试时如何摆脱“运行所有测试的意外无效模式”警告
【发布时间】:2021-06-30 16:17:27
【问题描述】:

每当我在我的 react.js 项目中使用 `yarn test 时,我都会在我的测试运行之前遇到一个很长的无效测试模式。这些测试运行良好,但那个尴尬的警告并不那么方便。我的配置中是否缺少某些内容?

这是警告:


$ 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.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;

依赖项

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

环境信息

系统:

  • 操作系统:Windows 10 10.0.19042
  • CPU:(8) x64 Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz

二进制文件:

  • 节点:14.7.0
  • 纱线:1.22.10
  • npm: 6.14.11

【问题讨论】:

    标签: reactjs typescript jestjs yarnpkg


    【解决方案1】:

    您对-- 的使用明确表示它后面的所有内容实际上都是文件模式。所以--config=jest.config.ts 根本没有被解释为一个选项,而令人困惑的错误来自于试图将其理解为文件模式的react-scripts test 命令。选项必须出现在--

    之前

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

    【讨论】:

    • 感谢您的链接。我们同意使用双破折号。事实证明,当我将测试模式更改为这些时,没有找到测试 iis: react-scripts test --config=jest.config.ts 或 react-scripts test --config=jest.config.ts -- 或 react-scripts test --config=jest.config.ts 或 react-scripts 测试 --config jest.config.ts 或 react-scripts 测试 -- --config jest.config.ts
    • 更新:该消息实际上已打印,因为 CRA 的内部笑话配置正在被我的 ovverriden。一切都很好,没有什么可怕的。
    • @exaucae,因此无法在不弹出的情况下使用 CRA 样板代码覆盖 jest 配置文件
    • @sagg1295,是的,可以覆盖 jest 配置文件。额外的事情是,每次运行测试时,您都会收到上述错误消息,因为 CRA 内部人员不希望您这样做......但到目前为止,这无关紧要:它不会阻止您的代码从运行也不会产生副作用。真是烦人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多