UNPKG

jest-bdd-generator

Version:

Jest code generator empowered with Gherkin and BDD

120 lines (119 loc) 4.96 kB
import * as ts from 'typescript'; import { IStepKey, Step } from '../types'; export * from '../types'; export type ICallbackOnFunctionExpression = (node: ts.Expression, callExpression: ts.CallExpression, context: IArgumentsSearchContext, ..._any: unknown[]) => void; export type IArgumentsSearchContext = { host: string; stepOfCurrentFunction: Step; }; export type ISearchExpressionSchema = { keyword: string; arguments: { callback?: ICallbackOnFunctionExpression; searchCallSchema?: ISearchCallSchema; }[]; chain?: ISearchCallSchema; }; export type ISearchCallSchema = Array<ISearchExpressionSchema>; /** * TypeScript Custom Transformer Factory for Jest to Gherkin transpilation. * Can be used in `ts.transpileModule` transformers. * @param searchSchema {ISearchCallSchema} Search schema for Jest to Gherkin transpilation. * @param self The factory owner, i.e., Transpile instance. * @returns {ts.CustomTransformerFactory} TypeScript Custom Transformer Factory. Works as a part of TypeScript transpilation. */ export declare const customTransformerFactory: (searchSchema: ISearchCallSchema, self: Transpile) => ts.CustomTransformerFactory; export declare class Transpile { /** * The search schema for Jest to Gherkin transpilation. It looks for `describe`, `test`, `it` function calls. * And ``@When ...``, `@Then ...`, `@Given ...` comments. */ searchSchema: ISearchCallSchema; /** * The source code to be transpiled. */ source: string; sourceFile?: ts.SourceFile; /** * The retrieved step. * During the transpilation, steps are pushed into this array. * 'searchSchema' defines how to find the steps. */ output: Step[]; /** * the `Step` compiled from the source code. */ get steps(): Step[]; get uniqueSteps(): Step[]; /** * Based on code repeation, if a sequence of code matches multiple times, it is considered as a possible step. * Set during transpilation. */ possibleStep: Step[]; /** Setter for the source code to be transpiled. */ set input(input: string); /** Getter for the source code to be transpiled. */ get input(): string; constructor(searchSchema?: ISearchCallSchema); /** Parse the position number to line and character. */ parsePosition(pos: number): Step['pos']['start']; groupToScenario(): void; getDeclaration(declaration: ts.VariableDeclaration | ts.BindingElement): string[]; /** * Find code for a step when it is not implemented yet. * Search for the same Step signature in other Scenarios. * If found, generate comments for those steps. * Use the following RegExp to find or replace the generated comments: * ```ts * // /^\s*?\/\*\*\n\*\s*?FOUND: @.+\n(\*.+\n)+/g * ``` * @returns the generated comment */ generateComments(): string; /** * Search the block for code sequences that match known steps. * If found, generate a comment to mark the Step signature. * @param block * @returns {Step[]} The retrieved steps from matched code sequences. */ generateFromKnownSteps(block: ts.Block): Transpile['output']; /** * Traverse the block to find comments that match `@When ...`, `@Then ...`, `@Given ...`. * @param block {ts.Block} The block to be traversed. Usually the body of a function expression. * @param _lastItem {Step} The last step found. Used to accumulate source code. * @returns {Step[]} The retrieved steps from comments. */ getComments(block: ts.Block, _lastItem?: Step): Transpile['output']; /** * Traversing the AST, when the string argument is found, create a step and push into the output. * @param name Gherkin Keyword, Feature or Scenario * @returns the retrieved step */ callbackOnStringArgumentFactory: (name: IStepKey) => ICallbackOnFunctionExpression; /** * When a function expression argument is found, traverse its body to find steps inside it. * @param expressionOfArg * @param callExpression * @param callContext * @returns {void} */ callbackOnFnArgument: ICallbackOnFunctionExpression; /** * @deprecated Does nothing currently. Used to earse source map from input. * @param input * @returns */ dealwithSourceMap(input: string): Record<string, string>; /** * TypeScript Transpiler for Jest code. * Note: If the `fileName` in `options` does not end with `.test.ts`, it works as an ordinary TypeScript transpiler. * @param input {string} Jest source code in TypeScript * @param options TS transpiler options, i.e { fileName: 'index.test.ts' } * @returns {Transpiler} Transpiler class */ transpile(input: string, options?: ts.TranspileOptions): ts.TranspileOutput; /** @deprecated */ private _prepareOutput; /** @deprecated */ outputCode(): string; }