Jest 无法 运行 模拟文件并显示错误消息您的测试套件必须至少包含一个测试

Jest is failed to running with Mock files with Error Message Your test suite must contain at least one test

我有 webpack 和 jest,我的 jest 选项设置如下:

"moduleNameMapper": {
      "^.+\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__tests__/__mocks__/fileMock.js",
      "^.+\.(css|less)$": "<rootDir>/__tests__/__mocks__/styleMock.js"
    }
  }

styleMock.js内容:

export default '';

fileMock.js内容:

 export default 'test-file-stub';

开玩笑失败 运行。出现以下错误:

 FAIL  __tests__/__mocks__/fileMock.js
  ● Test suite failed to run

    Your test suite must contain at least one test.

 FAIL  __tests__/__mocks__/styleMock.js
  ● Test suite failed to run

    Your test suite must contain at least one test.

只是 Jest 不应该尝试将这些文件作为测试执行。您应该将 jest.config.js 中的 testMatch 更改为仅检查 .spec.js 文件或 .test.js 文件:

  testMatch: [
    "**/__tests__/**/*.[tj]s?(x)",
    "**/?(*.)+(spec|test).[tj]s?(x)"
  ],