typescript - 尝试在 Jest 中使用 -t 或 --testNamePattern 会运行所有测试
问题描述
我正在按照他们的文档中描述的方式使用jest
和ts-jest
配置。ts-jest
如果我运行,yarn test --listTests
我可以看到我想要运行的测试文件:processNewUser.ts
包含在__test__
我项目文件夹的输出中。
我只能用yarn test --testPathPattern='processNewUser'
但是如果我尝试使用yarn test --testNamePattern='Processes new user. Auth'
哪个是测试的名称,那么每个测试都会运行,包括所有没有指定名称字符串的测试。
与:
yarn test -t="Auth"
yarn test -t Auth
yarn test --testNamePattern "Auth"
jest Auth
jest -t="Processes"
等等等等以及每个语法组合都相同。还尝试了命名describe
函数包装器而不是测试。没运气。
我tsconfig.json
的是:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "dist",
"sourceMap": true,
"allowJs": true,
"strict": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"removeComments": false,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"lib": [
"ES2020.Promise",
"ES2015.Iterable",
"ES2015.Symbol.WellKnown"
],
},
"include": ["src/**/*"],
}
我jest.config.ts
的是:
import type {Config} from '@jest/types';
const config: Config.InitialOptions = {
clearMocks: true,
coverageDirectory: 'coverage',
coverageProvider: 'v8',
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
preset: 'ts-jest',
setupFiles: ['dotenv/config'],
setupFilesAfterEnv: ['./jest.setup.js'],
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
'^.+\\.ts?$': 'ts-jest',
},
transformIgnorePatterns: ['/node_modules/', '\\.pnp\\.[^\\/]+$'],
}
export default config;
纱线脚本只是:"test": "jest",
我希望它能够标记测试,auth
例如让所有身份验证测试运行。
有什么帮助吗?
解决方案
按名称过滤测试的谓词在此处定义:
if (globalConfig.testNamePattern) {
const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
env.specFilter = (spec: Spec) => testNameRegex.test(spec.getFullName());
}
请注意,它会根据每个规范的全名检查创建的正则表达式。即测试套件本身的名称加上所有周围描述块的名称。
推荐阅读
- react-native - 当我使用 Nativebase getImageSource React native 时显示 RNVectorIcons 模块不可用
- c# - 在测试中无法获得网络连接
- react-native - setState 未更新本机反应中的样式
- python - 运行 on_exit 任务 - Azure App Service (Windows) 和 Python
- java - 在泛型类中为通配符传递任何具体类都会出错。为什么?
- javascript - 将 Rest API 连接到 Google Charts
- arrays - 如何以数学方式添加数组值
- angular - 如何导入共享模块(使用 Angular Material)来测试 Angular 应用程序?
- python-3.x - 使用recognition_google()时如何解决“API不可用”问题
- android - Android以编程方式设置材料按钮顶部和底部插入(Java)