我已经在我的create-react-app
中配置了lint-staged,所以它使用staged文件运行测试。我的测试正在运行,没有任何问题,它只是找不到使用--findRelatedTests
的测试。
- jest.配置js*
currentTestDirectory
-可以是admin|business|consumer|core|shared
。
{
verbose: true,
roots: [`<rootDir>/tests/__tests__/${currentTestDirectory}`],
moduleFileExtensions: ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'],
preset: 'ts-jest',
resetMocks: true,
setupFiles: ['react-app-polyfill/jsdom'],
setupFilesAfterEnv: ['<rootDir>/tests/setupTests.ts', '<rootDir>/tests/mocks.ts'],
testEnvironment: 'jest-environment-jsdom',
testMatch: [`<rootDir>/tests/__tests__/${currentTestDirectory}/**/*.{spec,test}.{js,jsx,ts,tsx}`],
testPathIgnorePatterns: ['/node_modules/', 'scripts'],
transform: {
'^.+\\.(js|jsx|mjs|cjs|ts|tsx)$': '<rootDir>/config/jest/babelTransform.js',
'^.+\\.css$': '<rootDir>/config/jest/cssTransform.js',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)': '<rootDir>/config/jest/fileTransform.js',
'^.+\\.(ts|tsx)?$': 'ts-jest',
},
transformIgnorePatterns: [
'node_modules/(?!(react-dnd|core-dnd|@react-dnd|dnd-core|react-dnd-html5-backend|react-dnd-touch-backend|uuid)/)',
],
watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'],
}
以及 package.json 文件。
// package.json
"scripts": {
"test:staged": "CI=true node scripts/test.js --bail --findRelatedTests",
}
"dependencies": {
"jest": "^29.3.1",
// other
}
这是我的 *.lintstagedrc文件 *
{
"**/*.+(ts|tsx|js)": [
"eslint",
"prettier --write",
"npm run test:staged"
]
}
这是我的应用程序文件夹结构,为您方便,以获得所有的细节。
├── README.md
├── apps
├── build
├── config
├── core
├── jest.config.js
├── node_modules
├── package-lock.json
├── package.json
├── public
├── scripts
├── shared
├── tests
├── tools
├── tsconfig.json
└── version.json
git status
显示了我已经测试过的更改文件。
修改:应用程序/管理员/源代码/页面/登录名/index.tsx
当我运行git commit -am "Some message"
时,我看到代码1存在预提交钩子。
✔ Preparing lint-staged...
❯ Running tasks for staged files...
❯ .lintstagedrc — 1 files
❯ **/*.+(ts|tsx|js) — 1 files
✔ eslint
✔ prettier --write
✖ npm run test:staged [FAILED]
◼ git add
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...
✖ npm run test:staged:
> grailpay-web@0.1.0 test:staged
> CI=true node scripts/test.js --bail --findRelatedTests /Users/felixmanus/Desktop/grailpay-web-app/apps/admin/src/pages/login/index.tsx
Running all the available tests
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
No files found in /Users/felixmanus/Desktop/grailpay-web-app.
Make sure Jest's configuration does not exclude this directory.
To set up Jest, make sure a package.json file exists.
Jest Documentation: https://jestjs.io/docs/configuration
Pattern: /Users/felixmanus/Desktop/grailpay-web-app/apps/admin/src/pages/login/index.tsx - 0 matches
husky - pre-commit hook exited with code 1 (error)
我做了大量的研究,也发现了很多情况下,--findRelatedTests
选项是正确的工作与lint-staged
。我猜可能有一些配置我是失踪或设置错误。
1条答案
按热度按时间xu3bshqb1#
我已经检查了jest的内部,并找出了它们是如何在内部运行
findRelatedTests
的,结果发现错误出在我的jest.config.js
中,特别是roots
选项,路径搜索过滤器依赖于config. roots。有关其他信息,请在此处查看-https://github.com/facebook/jest/blob/main/packages/jest-core/src/SearchSource.ts#L68
所以最后,将根更改为也包含
<rootDir>
解决了我的问题。