Skip to content

Commit

Permalink
gets tests working
Browse files Browse the repository at this point in the history
- switches ts-jest compiler from ttypescript to ts-patch due to cevek/ttypescript#140
- switches to typescripts getDecorators() method as .decorator is removed
- uses new declare global syntax https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html
- uses typescript factory.getParameterDeclaration
- fixes types in tests

Signed-off-by: Lukas Bockstaller <lukas.bockstaller@posteo.de>
  • Loading branch information
bockstaller committed Oct 31, 2023
1 parent a942aa2 commit 5454206
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ yarn-debug.log*
yarn-error.log*
deploy/
artifacts/
*.tgz
*.tgz
.vscode/settings.json
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
testEnvironment: 'node',
globals: {
"ts-jest": {
"compiler": "ttypescript"
"compiler": "ts-patch/compiler"
}
},
testRegex: '/tests/.*(Test)?\\.(ts)$',
Expand Down
66 changes: 54 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@
"@types/google-protobuf": "^3.15.9",
"@types/jest": "^29.5.7",
"@types/klaw-sync": "^6.0.3",
"@types/uuid": "^9.0.6",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"eslint": "^8.52.0",
"@types/uuid": "^9.0.6",
"codecov": "^3.8.2",
"grpc-tools": "^1.12.4",
"eslint": "^8.52.0",
"grpc_tools_node_protoc_ts": "^5.3.3",
"grpc-tools": "^1.12.4",
"jest": "^29.7.0",
"jest-mock-process": "^2.0.0",
"jest-ts-auto-mock": "^2.1.0",
"ts-auto-mock": "^3.7.1",
"ts-jest": "^29.1.1",
"ttypescript": "^1.5.15"
"ts-patch": "^3.0.2"
}
}
54 changes: 35 additions & 19 deletions src/helpers/CodeHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createPrinter, Decorator, MethodDeclaration, Printer, TextRange} from "typescript";
import {createPrinter, Decorator, MethodDeclaration, Printer, TextRange, getDecorators} from "typescript";

export type ExpressionElementType = {
text: string;
Expand All @@ -21,18 +21,23 @@ export abstract class CodeHelper {
protected printer: Printer = createPrinter();

protected getStepTexts(method: MethodDeclaration): Array<string> {
const dec = (method.decorators as unknown) as Array<Decorator>;
const stepDecExp = (dec.filter(CodeHelper.isStepDecorator)[0]
.expression as unknown) as ExpressionType;
const arg = stepDecExp.arguments[0];
const dec = getDecorators(method);

if (dec) {
const stepDecExp = dec.filter(CodeHelper.isStepDecorator)[0]
.expression as unknown as ExpressionType;
const arg = stepDecExp.arguments[0];

if (!arg.text && arg.elements) {
if (!arg.text && arg.elements) {
return arg.elements.map((e) => {
return e.text;
return e.text;
});
}

return [arg.text];
}

return [arg.text];
return [];
}

protected static isStepDecorator(d: Decorator): boolean {
Expand All @@ -42,22 +47,33 @@ export abstract class CodeHelper {
}

protected hasStepDecorator(method: MethodDeclaration): boolean {
return !!method.decorators && method.decorators.some(CodeHelper.isStepDecorator);
const decorators = getDecorators(method);

if (decorators) {
return !!decorators && decorators.some(CodeHelper.isStepDecorator);
}

return false;
}

protected hasStepText(method: MethodDeclaration, stepText: string): boolean {
const dec = (method.decorators as unknown) as Array<Decorator>;
const stepDecExp = (dec.filter(CodeHelper.isStepDecorator)[0]
.expression as unknown) as ExpressionType;
const arg = stepDecExp.arguments[0];

if (!arg.text && arg.elements) {
return arg.elements.some((e) => {
return e.text === stepText;
});
const dec = getDecorators(method);

if (dec) {
const stepDecExp = (dec.filter(CodeHelper.isStepDecorator)[0]
.expression as unknown) as ExpressionType;
const arg = stepDecExp.arguments[0];

if (!arg.text && arg.elements) {
return arg.elements.some((e) => {
return e.text === stepText;
});
}

return arg.text === stepText;
}

return arg.text === stepText;
return false;
}

}
8 changes: 6 additions & 2 deletions src/loaders/StaticLoader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
createSourceFile,
Decorator,
getDecorators,
forEachChild,
isClassDeclaration,
isMethodDeclaration,
Expand Down Expand Up @@ -71,7 +71,11 @@ export class StaticLoader extends CodeHelper {
}

private static getRange(node: MethodDeclaration, source: SourceFile): Range {
const dec = (node.decorators as unknown) as Array<Decorator>;
const dec = getDecorators(node);

if (!dec) {
throw new Error("no decorator found");
}
const start = source.getLineAndCharacterOfPosition(dec[0].expression.pos);
const end = source.getLineAndCharacterOfPosition(node.end);

Expand Down
8 changes: 7 additions & 1 deletion src/models/GlobalRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Global = NodeJS.Global;
import {StepRegistry} from "./StepRegistry";
import {HookRegistry} from "./HookRegistry";

export interface GlobalStepRegistry extends Global {
gaugeStepRegistry: StepRegistry,
gaugeHookRegistry: HookRegistry
}

declare global {
// eslint-disable-next-line no-var
var gaugeHookRegistry: HookRegistry;
// eslint-disable-next-line no-var
var gaugeStepRegistry: StepRegistry;
}
2 changes: 0 additions & 2 deletions src/models/HookRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Operator } from "..";
import { HookMethod } from "./HookMethod";
import { HookType } from "./HookType";
import {GlobalStepRegistry} from "./GlobalRegistry";

export class HookRegistry {

Expand Down Expand Up @@ -70,7 +69,6 @@ export class HookRegistry {
}

}
declare const global: GlobalStepRegistry;

if (!global.gaugeHookRegistry) {
global.gaugeHookRegistry = new HookRegistry();
Expand Down
3 changes: 0 additions & 3 deletions src/models/StepRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {AssertionError} from "assert";
import {Range} from './Range';
import {StepRegistryEntry} from "./StepRegistryEntry";
import {CommonFunction} from '../utils/Util';
import {GlobalStepRegistry} from "./GlobalRegistry";

export class StepRegistry {

Expand Down Expand Up @@ -114,8 +113,6 @@ export class StepRegistry {

}

declare const global: GlobalStepRegistry;

if (!global.gaugeStepRegistry) {
global.gaugeStepRegistry = new StepRegistry();
}
Expand Down
11 changes: 8 additions & 3 deletions src/processors/RefactorProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EOL } from "os";
import { createParameter, createSourceFile, Decorator, EmitHint, forEachChild, isClassDeclaration, isMethodDeclaration, MethodDeclaration, Node, NodeArray, ParameterDeclaration, ScriptKind, ScriptTarget, SourceFile } from "typescript";
import { createSourceFile, EmitHint, factory, forEachChild, getDecorators, isClassDeclaration, isMethodDeclaration, MethodDeclaration, Node, NodeArray, ParameterDeclaration, ScriptKind, ScriptTarget, SourceFile } from "typescript";
import { CodeHelper } from "../helpers/CodeHelper";
import registry from "../models/StepRegistry";
import { Util } from "../utils/Util";
Expand Down Expand Up @@ -64,7 +64,7 @@ export class RefactorProcessor extends CodeHelper {
if (p.getOldposition() < 0) {
const pName = this.getParamName(paramPositions.indexOf(p), oldParams, source);

newParams.splice(p.getNewposition(), 0, createParameter(undefined, undefined, undefined, `${pName}: any`));
newParams.splice(p.getNewposition(), 0, factory.createParameterDeclaration(undefined, undefined, `${pName}: any`));
} else {
newParams.splice(p.getNewposition(), 0, oldParams[p.getOldposition()]);
}
Expand Down Expand Up @@ -103,7 +103,12 @@ export class RefactorProcessor extends CodeHelper {
}

private getStepTextRange(source: SourceFile, node: MethodDeclaration): Span {
const dec = (node.decorators as unknown) as Array<Decorator>;
const dec = getDecorators(node);

if (!dec) {
throw new Error("no decorators found");
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
const stepDecExp = dec.filter(CodeHelper.isStepDecorator)[0].expression as any;

Expand Down
1 change: 0 additions & 1 deletion src/stores/GlobalDataStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Global = NodeJS.Global;
import {DataStore} from "./DataStore";

export interface GlobalDataStore extends Global {
Expand Down
Loading

0 comments on commit 5454206

Please sign in to comment.