javascript Jest不匹配任何具有--findRelatedTests和lint-staged的测试

fdbelqdn  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(193)

我已经在我的create-react-app中配置了lint-staged,所以它使用staged文件运行测试。我的测试正在运行,没有任何问题,它只是找不到使用--findRelatedTests的测试。

  • jest.配置js*

currentTestDirectory-可以是admin|business|consumer|core|shared

  1. {
  2. verbose: true,
  3. roots: [`<rootDir>/tests/__tests__/${currentTestDirectory}`],
  4. moduleFileExtensions: ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'],
  5. preset: 'ts-jest',
  6. resetMocks: true,
  7. setupFiles: ['react-app-polyfill/jsdom'],
  8. setupFilesAfterEnv: ['<rootDir>/tests/setupTests.ts', '<rootDir>/tests/mocks.ts'],
  9. testEnvironment: 'jest-environment-jsdom',
  10. testMatch: [`<rootDir>/tests/__tests__/${currentTestDirectory}/**/*.{spec,test}.{js,jsx,ts,tsx}`],
  11. testPathIgnorePatterns: ['/node_modules/', 'scripts'],
  12. transform: {
  13. '^.+\\.(js|jsx|mjs|cjs|ts|tsx)$': '<rootDir>/config/jest/babelTransform.js',
  14. '^.+\\.css$': '<rootDir>/config/jest/cssTransform.js',
  15. '^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)': '<rootDir>/config/jest/fileTransform.js',
  16. '^.+\\.(ts|tsx)?$': 'ts-jest',
  17. },
  18. transformIgnorePatterns: [
  19. 'node_modules/(?!(react-dnd|core-dnd|@react-dnd|dnd-core|react-dnd-html5-backend|react-dnd-touch-backend|uuid)/)',
  20. ],
  21. watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'],
  22. }

以及 package.json 文件。

  1. // package.json
  2. "scripts": {
  3. "test:staged": "CI=true node scripts/test.js --bail --findRelatedTests",
  4. }
  5. "dependencies": {
  6. "jest": "^29.3.1",
  7. // other
  8. }

这是我的 *.lintstagedrc文件 *

  1. {
  2. "**/*.+(ts|tsx|js)": [
  3. "eslint",
  4. "prettier --write",
  5. "npm run test:staged"
  6. ]
  7. }

这是我的应用程序文件夹结构,为您方便,以获得所有的细节。

  1. ├── README.md
  2. ├── apps
  3. ├── build
  4. ├── config
  5. ├── core
  6. ├── jest.config.js
  7. ├── node_modules
  8. ├── package-lock.json
  9. ├── package.json
  10. ├── public
  11. ├── scripts
  12. ├── shared
  13. ├── tests
  14. ├── tools
  15. ├── tsconfig.json
  16. └── version.json

git status显示了我已经测试过的更改文件。
修改:应用程序/管理员/源代码/页面/登录名/index.tsx
当我运行git commit -am "Some message"时,我看到代码1存在预提交钩子。

  1. Preparing lint-staged...
  2. Running tasks for staged files...
  3. .lintstagedrc 1 files
  4. **/*.+(ts|tsx|js) — 1 files
  5. ✔ eslint
  6. ✔ prettier --write
  7. ✖ npm run test:staged [FAILED]
  8. ◼ git add
  9. ↓ Skipped because of errors from tasks. [SKIPPED]
  10. ✔ Reverting to original state because of errors...
  11. ✔ Cleaning up temporary files...
  12. ✖ npm run test:staged:
  13. > grailpay-web@0.1.0 test:staged
  14. > CI=true node scripts/test.js --bail --findRelatedTests /Users/felixmanus/Desktop/grailpay-web-app/apps/admin/src/pages/login/index.tsx
  15. Running all the available tests
  16. No tests found, exiting with code 1
  17. Run with `--passWithNoTests` to exit with code 0
  18. No files found in /Users/felixmanus/Desktop/grailpay-web-app.
  19. Make sure Jest's configuration does not exclude this directory.
  20. To set up Jest, make sure a package.json file exists.
  21. Jest Documentation: https://jestjs.io/docs/configuration
  22. Pattern: /Users/felixmanus/Desktop/grailpay-web-app/apps/admin/src/pages/login/index.tsx - 0 matches
  23. husky - pre-commit hook exited with code 1 (error)

我做了大量的研究,也发现了很多情况下,--findRelatedTests选项是正确的工作与lint-staged。我猜可能有一些配置我是失踪或设置错误。

xu3bshqb

xu3bshqb1#

我已经检查了jest的内部,并找出了它们是如何在内部运行findRelatedTests的,结果发现错误出在我的jest.config.js中,特别是roots选项,路径搜索过滤器依赖于config. roots。
有关其他信息,请在此处查看-https://github.com/facebook/jest/blob/main/packages/jest-core/src/SearchSource.ts#L68
所以最后,将根更改为也包含<rootDir>解决了我的问题。

相关问题