Skip to content

Commit

Permalink
feat: ✨ migrating js to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
WasiqB committed Jun 18, 2024
1 parent 6b1a0c0 commit de407ea
Show file tree
Hide file tree
Showing 61 changed files with 12,794 additions and 15,709 deletions.
15,702 changes: 0 additions & 15,702 deletions examples/cypress/package-lock.json

This file was deleted.

8 changes: 8 additions & 0 deletions examples/cypress/pnpm-lock.yaml

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

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,17 @@
"uuid": "^10.0.0"
},
"devDependencies": {
"@types/find": "^0.2.4",
"@types/fs-extra": "^11.0.4",
"@types/jsonfile": "^6.1.4",
"@types/lodash": "^4.17.5",
"@types/luxon": "^3.4.2",
"@types/node": "^20.14.2",
"@types/uuid": "^9.0.8",
"jasmine": "^5.1.0",
"nyc": "^17.0.0",
"release-it": "^17.3.0"
"release-it": "^17.3.0",
"typescript": "^5.4.5"
},
"packageManager": "[email protected]"
}
80 changes: 74 additions & 6 deletions pnpm-lock.yaml

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

132 changes: 132 additions & 0 deletions src/collect-jsons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import find from "find";
import fs from "fs-extra";
import jsonFile from "jsonfile";
import path from "node:path";
import { Feature, ReportOption, Step } from "./report-types";
import { formatToLocalIso } from "./helper";

export const collectJsonFiles = (options: ReportOption): Feature[] => {
let jsonOutput: Feature[];
let files: string[];

try {
files = find.fileSync(/\.json$/, path.resolve(process.cwd(), options.dir));
} catch (e) {
throw new Error(
`There were issues reading JSON-files from '${options.dir}'.`,
{
cause: e,
}
);
}

if (files.length > 0) {
files.map((file) => {
// Cucumber json can be empty, it's likely being created by another process (#47)
const data = fs.readFileSync(file).toString() || "[]";
const stats = fs.statSync(file);
const reportTime = stats.birthtime;

JSON.parse(data).map((json: Feature) => {
if (options.metadata && !json.metadata) {
json.metadata = options.metadata;
} else {
json = Object.assign(
{
metadata: {
browser: {
name: "not known",
version: "not known",
},
device: "not known",
platform: {
name: "not known",
version: "not known",
},
},
},
json
);
}

if (json.metadata && options.displayReportTime && reportTime) {
json.metadata = Object.assign({ reportTime }, json.metadata);
json.metadata.reportTime = formatToLocalIso(json.metadata.reportTime);
}

// Only check the feature hooks if there are elements (fail-safe)
const { scenarios } = json;

if (scenarios) {
json.scenarios = scenarios.map((scenario) => {
const { before, after } = scenario;

if (before) {
scenario.steps = parseFeatureHooks(before, "Before").concat(
scenario.steps
);
}
if (after) {
scenario.steps = scenario.steps.concat(
parseFeatureHooks(after, "After")
);
}
return scenario;
});
}
jsonOutput.push(json);
});
});

if (options.saveCollectedJson) {
const file = path.resolve(options.reportPath, "merged-output.json");
fs.ensureDirSync(options.reportPath);
jsonFile.writeFileSync(file, jsonOutput, { spaces: 2 });
}

return jsonOutput;
}

console.log(
"\x1b[33m%s\x1b[0m",
`WARNING: No JSON files found in '${options.dir}'. NO REPORT CAN BE CREATED!`
);
return [];
};

/**
* Add the feature hooks to the steps so the report will pick them up properly
*
* @param {object} data
* @param {string} keyword
* @returns {{
* arguments: array,
* keyword: string,
* name: string,
* result: {
* status: string,
* },
* line: number,
* match: {
* location: string
* },
* embeddings: []
* }}
*/
const parseFeatureHooks = (data: Step[], keyword: string): Step => {
return data.map((step) => {
const match =
step.match && step.match.location
? step.match
: { location: "can not be determined" };

return {
arguments: step.arguments || [],
keyword: keyword,
name: "Hook",
result: step.result,
match,
embeddings: step.embeddings || [],
};
});
};
Loading

0 comments on commit de407ea

Please sign in to comment.