testeranto
Version:
the AI powered BDD test framework for typescript projects
252 lines (251 loc) • 7.53 kB
JavaScript
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
const baseProxy = function (pm, mappings) {
return new Proxy(pm, {
get: (target, prop, receiver) => {
for (const mapping of mappings) {
const method = mapping[0];
const arger = mapping[1];
if (prop === method) {
return (...x) => {
const modifiedArgs = arger(...x);
return target[prop](...modifiedArgs);
};
}
}
return (...x) => {
return target[prop](...x);
};
},
});
};
export const butThenProxy = (pm, filepath, addArtifact) => {
return baseProxy(pm, [
[
"screencast",
(opts, p) => {
const path = `${filepath}/butThen/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
[
"createWriteStream",
(fp) => {
const path = `${filepath}/butThen/${fp}`;
addArtifact(path);
return [path];
},
],
[
"writeFileSync",
(fp, contents, testName) => {
const path = `${filepath}/butThen/${fp}`;
addArtifact(path);
return [path, contents, testName];
},
],
[
"customScreenShot",
(opts, p) => {
const path = `${filepath}/butThen/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
]);
};
export const andWhenProxy = (pm, filepath, addArtifact) => {
return baseProxy(pm, [
[
"screencast",
(opts, p) => {
const path = `${filepath}/andWhen/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
[
"createWriteStream",
(fp) => {
const path = `${filepath}/andWhen/${fp}`;
addArtifact(path);
return [path];
},
],
[
"writeFileSync",
(fp, contents, testName) => {
const path = `${filepath}/andWhen/${fp}`;
addArtifact(path);
return [path, contents, testName];
},
],
[
"customScreenShot",
(opts, p) => {
const path = `${filepath}/andWhen/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
]);
};
export const afterEachProxy = (pm, suite, given, addArtifact) => {
return baseProxy(pm, [
[
"screencast",
(opts, p) => {
const path = `suite-${suite}/given-${given}/afterEach/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
[
"createWriteStream",
(fp) => {
const path = `suite-${suite}/afterEach/${fp}`;
addArtifact(path);
return [path];
},
],
[
"writeFileSync",
(fp, contents, testName) => {
const path = `suite-${suite}/given-${given}/afterEach/${fp}`;
addArtifact(path);
return [path, contents, testName];
},
],
[
"customScreenShot",
(opts, p) => {
const path = `suite-${suite}/given-${given}/afterEach/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
]);
};
export const beforeEachProxy = (pm, suite, addArtifact) => {
return baseProxy(pm, [
[
"screencast",
(opts, p) => {
const path = `suite-${suite}/beforeEach/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
[
"writeFileSync",
(fp, contents, testName) => {
const path = `suite-${suite}/beforeEach/${fp}`;
addArtifact(path);
return [path, contents, testName];
},
],
[
"customScreenShot",
(opts, p) => {
const path = `suite-${suite}/beforeEach/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
[
"createWriteStream",
(fp) => {
const path = `suite-${suite}/beforeEach/${fp}`;
addArtifact(path);
return [path];
},
],
]);
};
export const beforeAllProxy = (pm, suite, addArtifact) => {
return baseProxy(pm, [
[
"writeFileSync",
(fp, contents, testName) => {
const path = `suite-${suite}/beforeAll/${fp}`;
addArtifact(path);
return [path, contents, testName];
},
],
[
"customScreenShot",
(opts, p) => {
const path = `suite-${suite}/beforeAll/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
[
"createWriteStream",
(fp) => {
const path = `suite-${suite}/beforeAll/${fp}`;
addArtifact(path);
return [path];
},
],
]);
};
export const afterAllProxy = (pm, suite, addArtifact) => {
return baseProxy(pm, [
[
"createWriteStream",
(fp) => {
const path = `suite-${suite}/afterAll/${fp}`;
addArtifact(path);
return [path];
},
],
[
"writeFileSync",
(fp, contents, testName) => {
const path = `suite-${suite}/afterAll/${fp}`;
addArtifact(path);
return [path, contents, testName];
},
],
[
"customScreenShot",
(opts, p) => {
const path = `suite-${suite}/afterAll/${opts.path}`;
addArtifact(path);
return [
Object.assign(Object.assign({}, opts), { path }),
p,
];
},
],
]);
};