Ir al contenido

Recursos: registerResource

El módulo de recursos define cómo se traducen los escenarios Gherkin (Scenario:) en atributos estructurados para proveedores de gestión de pruebas (Jira, Zephyr, Azure DevOps, TestRail o APIs propietarias).


import { registerResource, ResourceTemplate } from '@fromeroc9/testform@beta';
// Registrar un nuevo tipo de recurso o sobreescribir la plantilla de uno existente
function registerResource(template: ResourceTemplate): void;

Define la estructura de campos y comentarios asociados a un tipo de recurso:

export interface ResourceTemplate {
/** Tipo único del recurso (ej. 'jira_testcase', 'azure_testrun', 'github_testcase') */
type: string;
/** Lista de campos a mapear desde el escenario Gherkin */
fields: ResourceField[];
/** Generador de comentarios/evidencias de ejecución (opcional) */
comments?: (
scenario: any,
context?: any
) => Array<{
identity: string;
status: string;
body: string;
title: string;
}>;
}

Describe un atributo individual del recurso:

export interface ResourceField {
/** Nombre del campo en el sistema de destino */
name: string;
/** Valor estático o función extractora que recibe el escenario y contexto */
value: ((scenario: any, context?: any) => string | string[]) | string | string[];
/** Indica si el valor final se calcula tras la sincronización remota */
knownAfterApply?: boolean;
}

1. Registrar un Nuevo Recurso (registerResource)

Sección titulada «1. Registrar un Nuevo Recurso (registerResource)»

Crea .testform/resources/azure-devops.js:

.testform/resources/azure-devops.js
module.exports = {
type: 'azure_testcase',
fields: [
{
name: 'title',
value: (scenario) => scenario.name,
},
{
name: 'description',
value: (scenario) => scenario.description || 'Sin descripción',
},
{
name: 'priority',
value: (scenario) => scenario.custom?.fields?.priority || '2',
},
{
name: 'steps_count',
value: (scenario) => String(scenario.steps?.length || 0),
},
{
name: 'tags',
value: (scenario) => scenario.tags.filter(t => !t.startsWith('@tc-')),
},
{
name: 'remote_work_item_id',
value: () => '(known after apply)',
knownAfterApply: true,
}
],
comments: (scenario, context) => [
{
identity: scenario.custom?.identity || scenario.name,
status: scenario.custom?.fields?.status || 'passed',
title: `Ejecución CI/CD #${context?.buildId || 'local'}`,
body: `Prueba ejecutada automáticamente en ambiente: ${context?.environment || 'staging'}`
}
]
};

La instancia resource permite consultar e inspeccionar los templates cargados:

import { resource } from '@fromeroc9/testform@beta';
// Consultar todos los tipos registrados
const types = resource.getRegisteredTypes();
// ['github_testcase', 'github_testrun', 'testcase', 'azure_testcase', ...]