Skip to content

Commit

Permalink
chore: refactor custom extension (#221)
Browse files Browse the repository at this point in the history
* chore: refactor custom extension

* fix: custom target assertions
  • Loading branch information
ASaiAnudeep authored Jul 13, 2024
1 parent 258868d commit 53ef654
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 33 deletions.
47 changes: 47 additions & 0 deletions src/extensions/custom.extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require('path');
const { BaseExtension } = require("./base.extension");
const { STATUS, HOOK } = require("../helpers/constants");

class CustomExtension extends BaseExtension {

/**
* @param {import('..').CustomExtension} extension
*/
constructor(target, extension, result, payload, root_payload) {
super(target, extension, result, payload, root_payload);
this.extension = extension;
this.#setDefaultOptions();
this.updateExtensionInputs();
}

#setDefaultOptions() {
this.default_options.hook = HOOK.END,
this.default_options.condition = STATUS.PASS_OR_FAIL;
}

async run() {
const params = this.#getParams();
if (typeof this.extension.inputs.load === 'string') {
const cwd = process.cwd();
const extension_runner = require(path.join(cwd, this.extension.inputs.load));
await extension_runner.run(params);
} else if (typeof this.extension.inputs.load === 'function') {
await this.extension.inputs.load(params);
} else {
throw `Invalid 'load' input in custom extension - ${this.extension.inputs.load}`;
}
}

#getParams() {
return {
target: this.target,
extension: this.extension,
payload: this.payload,
root_payload: this.root_payload,
result: this.result
}
}

}

module.exports = { CustomExtension }
29 changes: 0 additions & 29 deletions src/extensions/custom.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const rp_analysis = require('./report-portal-analysis');
const rp_history = require('./report-portal-history');
const qc_test_summary = require('./quick-chart-test-summary');
const percy_analysis = require('./percy-analysis');
const custom = require('./custom');
const metadata = require('./metadata');
const { AIFailureSummaryExtension } = require('./ai-failure-summary.extension');
const { SmartAnalysisExtension } = require('./smart-analysis.extension');
const { CIInfoExtension } = require('./ci-info.extension');
const { CustomExtension } = require('./custom.extension');
const { EXTENSION } = require('../helpers/constants');
const { checkCondition } = require('../helpers/helper');
const logger = require('../utils/logger');
Expand Down Expand Up @@ -51,7 +51,7 @@ function getExtensionRunner(extension, options) {
case EXTENSION.PERCY_ANALYSIS:
return percy_analysis;
case EXTENSION.CUSTOM:
return custom;
return new CustomExtension(options.target, extension, options.result, options.payload, options.root_payload);
case EXTENSION.METADATA:
return metadata;
case EXTENSION.CI_INFO:
Expand Down
2 changes: 1 addition & 1 deletion test/data/configs/custom-target.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "custom",
"inputs": {
"load": "test/data/custom/custom-runner.js"
"load": "test/data/custom/custom-target-runner.js"
}
}
],
Expand Down
6 changes: 5 additions & 1 deletion test/data/custom/custom-runner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const p6 = require('pactum');
const assert = require('assert');

async function run() {
async function run({ target, extension, result }) {
assert.equal(target.name, 'teams');
assert.equal(extension.name, 'custom');
assert.equal(result.name, 'Default suite');
await p6.spec().get('http://localhost:9393/custom');
}

Expand Down
12 changes: 12 additions & 0 deletions test/data/custom/custom-target-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const p6 = require('pactum');
const assert = require('assert');

async function run({ target, result }) {
assert.equal(target.name, 'custom');
assert.equal(result.name, 'Default suite');
await p6.spec().get('http://localhost:9393/custom');
}

module.exports = {
run
}

0 comments on commit 53ef654

Please sign in to comment.