@vr1/be-driven-syntax-sugar 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 vr1org
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @vr1/be-driven-syntax-sugar
2
+
3
+ Синтаксический сахар, для того чтобы тесты, такие как pw или jest, писать в виде bdd тестов.
4
+
5
+ # ограничение
6
+ Тест может быть только асинхронным, хотя шаги могут быть и синхронными.
7
+
8
+ # пример теста
9
+ ```
10
+ test('my test', async () => {
11
+ await scenario((name, type, func) => test.step(`${type} ${name}`, func))
12
+ .given(givenStep)
13
+ .when(whenStep)
14
+ .then(thenStep);
15
+ });
16
+ ```
@@ -0,0 +1,2 @@
1
+ export * from "./scenario";
2
+ export * from "./types";
package/build/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./scenario"), exports);
18
+ __exportStar(require("./types"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,0CAAwB"}
@@ -0,0 +1,3 @@
1
+ import { StepWrapper } from "./types/StepWrapper";
2
+ import { ScenarioFirstStep } from "@vr1/be-driven-interface";
3
+ export declare function scenario(wrapper: StepWrapper): ScenarioFirstStep;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scenario = scenario;
4
+ const testScenario_1 = require("./testScenario");
5
+ function scenario(wrapper) {
6
+ return new testScenario_1.TestScenario(wrapper);
7
+ }
8
+ //# sourceMappingURL=scenario.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scenario.js","sourceRoot":"","sources":["../src/scenario.ts"],"names":[],"mappings":";;AAIA,4BAEC;AALD,iDAA8C;AAG9C,SAAgB,QAAQ,CAAC,OAAoB;IAC3C,OAAO,IAAI,2BAAY,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { Scenario } from "@vr1/be-driven-interface";
2
+ import { StepWrapper } from "../types";
3
+ export declare class TestScenario implements Scenario {
4
+ private wrapper;
5
+ private steps;
6
+ constructor(wrapper: StepWrapper);
7
+ given<TArgs extends any[], TMnemonic extends (...a: TArgs) => void | Promise<void>>(func: TMnemonic, ...args: TArgs): Scenario;
8
+ when<TArgs extends any[], TMnemonic extends (...a: TArgs) => void | Promise<void>>(func: TMnemonic, ...args: TArgs): Scenario;
9
+ then<TArgs extends any[], TMnemonic extends (...a: TArgs) => void | Promise<void>>(func: TMnemonic, ...args: TArgs): Scenario;
10
+ and<TArgs extends any[], TMnemonic extends (...a: TArgs) => void | Promise<void>>(func: TMnemonic, ...args: TArgs): Scenario;
11
+ split(): Scenario;
12
+ run(): Promise<void>;
13
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TestScenario = void 0;
4
+ const be_driven_interface_1 = require("@vr1/be-driven-interface");
5
+ const runTest_1 = require("./runTest");
6
+ class TestScenario {
7
+ wrapper;
8
+ steps = [];
9
+ constructor(wrapper) {
10
+ this.wrapper = wrapper;
11
+ }
12
+ given(func, ...args) {
13
+ this.steps.push(createStep(func, args, be_driven_interface_1.StepType.Given));
14
+ return this;
15
+ }
16
+ when(func, ...args) {
17
+ this.steps.push(createStep(func, args, be_driven_interface_1.StepType.When));
18
+ return this;
19
+ }
20
+ then(func, ...args) {
21
+ this.steps.push(createStep(func, args, be_driven_interface_1.StepType.Then));
22
+ return this;
23
+ }
24
+ and(func, ...args) {
25
+ this.steps.push(createStep(func, args, this.steps[this.steps.length - 1].type));
26
+ return this;
27
+ }
28
+ split() {
29
+ throw new Error("Splitting test into several tests is not supported in syntax-sugar mode.");
30
+ }
31
+ run() {
32
+ const steps = this.steps;
33
+ this.steps = null;
34
+ return (0, runTest_1.runTest)(this.wrapper, steps);
35
+ }
36
+ }
37
+ exports.TestScenario = TestScenario;
38
+ function createStep(target, args, type) {
39
+ const stepType = target.stepType ?? type;
40
+ if (stepType !== type)
41
+ throw new Error(`Cannot use ${stepType} type of step, ${type} was expected.`);
42
+ return {
43
+ name: createName(target, args),
44
+ target,
45
+ type,
46
+ args,
47
+ };
48
+ }
49
+ function createName(target, args) {
50
+ return args.reduce((prev, cur, i) => prev.replace(new RegExp(`\\{${i}\\}`, "g"), cur), target.stepName ?? target.name);
51
+ }
52
+ //# sourceMappingURL=TestScenario.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TestScenario.js","sourceRoot":"","sources":["../../src/testScenario/TestScenario.ts"],"names":[],"mappings":";;;AACA,kEAA8D;AAG9D,uCAAoC;AAEpC,MAAa,YAAY;IAGH;IAFZ,KAAK,GAAe,EAAE,CAAC;IAE/B,YAAoB,OAAoB;QAApB,YAAO,GAAP,OAAO,CAAa;IAAI,CAAC;IAEtC,KAAK,CAA+E,IAAe,EAAE,GAAG,IAAW;QACxH,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,8BAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;QACxD,OAAO,IAAI,CAAC;IACd,CAAC;IACM,IAAI,CAA+E,IAAe,EAAE,GAAG,IAAW;QACvH,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,8BAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QACvD,OAAO,IAAI,CAAC;IACd,CAAC;IACM,IAAI,CAA+E,IAAe,EAAE,GAAG,IAAW;QACvH,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,8BAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QACvD,OAAO,IAAI,CAAC;IACd,CAAC;IACM,GAAG,CAA+E,IAAe,EAAE,GAAG,IAAW;QACtH,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,U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 -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=TestStep.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TestStep.js","sourceRoot":"","sources":["../../src/testScenario/TestStep.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export * from "./TestScenario";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./TestScenario"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testScenario/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B"}
@@ -0,0 +1,3 @@
1
+ import { StepWrapper } from "../types";
2
+ import { TestStep } from "./TestStep";
3
+ export declare function runTest(wrapper: StepWrapper, steps: TestStep[]): Promise<void>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runTest = runTest;
4
+ async function runTest(wrapper, steps) {
5
+ const store = {};
6
+ for (let step of steps) {
7
+ await wrapper(step.type, step.name, async () => {
8
+ const stepOutput = await step.target.call(store, ...step.args);
9
+ Object.assign(store, stepOutput);
10
+ });
11
+ }
12
+ }
13
+ //# sourceMappingURL=runTest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runTest.js","sourceRoot":"","sources":["../../src/testScenario/runTest.ts"],"names":[],"mappings":";;AAGA,0BASC;AATM,KAAK,UAAU,OAAO,CAAC,OAAoB,EAAE,KAAiB;IACnE,MAAM,KAAK,GAAG,EAAE,CAAC;IAEjB,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE,CAAC;QACvB,MAAM,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,IAAI,EAAE;YAC7C,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;YAC/D,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { StepType } from "@vr1/be-driven-interface";
2
+ export type StepWrapper = (type: StepType, name: string, func: () => Promise<void>) => Promise<void>;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=StepWrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StepWrapper.js","sourceRoot":"","sources":["../../src/types/StepWrapper.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export * from "./StepWrapper";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./StepWrapper"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@vr1/be-driven-syntax-sugar",
3
+ "version": "1.0.0",
4
+ "description": "syntax sugar to write pw and jest tests in bdd style ",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "ssh://git@gitverse.ru:2222/vr1/be-driven-syntax-sugar.git"
8
+ },
9
+ "license": "MIT",
10
+ "author": "Vladimir Repin",
11
+ "type": "commonjs",
12
+ "main": "build/index.js",
13
+ "types": "build/index.d.ts",
14
+ "files": [
15
+ "src",
16
+ "build"
17
+ ],
18
+ "scripts": {
19
+ "clean": "rm -rf build",
20
+ "lint": "npx eslint",
21
+ "compile": "npm run lint && npx tsc",
22
+ "build": "npm run lint && npx tsc -p ./tsconfig.build.json",
23
+ "test": "npm run compile && npx jest --coverage=true ./build/test"
24
+ },
25
+ "devDependencies": {
26
+ "@babel/plugin-proposal-decorators": "^7.25.9",
27
+ "@babel/preset-env": "^7.26.0",
28
+ "@types/jest": "^29.5.13",
29
+ "@types/node": "^22.7.5",
30
+ "@typescript-eslint/eslint-plugin": "^5.61.0",
31
+ "@typescript-eslint/eslint-plugin-tslint": "^5.61.0",
32
+ "@typescript-eslint/parser": "^5.61.0",
33
+ "babel-jest": "^29.7.0",
34
+ "eslint": "^8.44.0",
35
+ "eslint-plugin-jsdoc": "^46.4.3",
36
+ "eslint-plugin-no-null": "^1.0.2",
37
+ "gulp": "^5.0.0",
38
+ "jest": "^29.7.0",
39
+ "ts-node": "^10.9.2",
40
+ "typescript": "^5.7.3"
41
+ },
42
+ "dependencies": {
43
+ "@vr1/be-driven-interface": "^1.2.0"
44
+ }
45
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./scenario";
2
+ export * from "./types";
@@ -0,0 +1,8 @@
1
+ import { StepWrapper } from "./types/StepWrapper";
2
+ import { TestScenario } from "./testScenario";
3
+ import { ScenarioFirstStep } from "@vr1/be-driven-interface";
4
+
5
+ export function scenario(wrapper: StepWrapper): ScenarioFirstStep {
6
+ return new TestScenario(wrapper);
7
+ }
8
+
@@ -0,0 +1,53 @@
1
+
2
+ import { Scenario, StepType } from "@vr1/be-driven-interface";
3
+ import { StepWrapper } from "../types";
4
+ import { TestStep } from "./TestStep";
5
+ import { runTest } from "./runTest";
6
+
7
+ export class TestScenario implements Scenario {
8
+ private steps: TestStep[] = [];
9
+
10
+ constructor(private wrapper: StepWrapper) { }
11
+
12
+ public given<TArgs extends any[], TMnemonic extends (...a: TArgs) => void | Promise<void>>(func: TMnemonic, ...args: TArgs): Scenario {
13
+ this.steps.push(createStep(func, args, StepType.Given));
14
+ return this;
15
+ }
16
+ public when<TArgs extends any[], TMnemonic extends (...a: TArgs) => void | Promise<void>>(func: TMnemonic, ...args: TArgs): Scenario {
17
+ this.steps.push(createStep(func, args, StepType.When));
18
+ return this;
19
+ }
20
+ public then<TArgs extends any[], TMnemonic extends (...a: TArgs) => void | Promise<void>>(func: TMnemonic, ...args: TArgs): Scenario {
21
+ this.steps.push(createStep(func, args, StepType.Then));
22
+ return this;
23
+ }
24
+ public and<TArgs extends any[], TMnemonic extends (...a: TArgs) => void | Promise<void>>(func: TMnemonic, ...args: TArgs): Scenario {
25
+ this.steps.push(createStep(func, args, this.steps[this.steps.length - 1].type));
26
+ return this;
27
+ }
28
+
29
+ public split(): Scenario {
30
+ throw new Error("Splitting test into several tests is not supported in syntax-sugar mode.");
31
+ }
32
+
33
+ public run(): Promise<void> {
34
+ const steps = this.steps;
35
+ this.steps = null;
36
+ return runTest(this.wrapper, steps);
37
+ }
38
+ }
39
+
40
+ function createStep(target: any, args: any[], type: StepType): TestStep {
41
+ const stepType = target.stepType ?? type;
42
+ if (stepType !== type) throw new Error(`Cannot use ${stepType} type of step, ${type} was expected.`);
43
+ return {
44
+ name: createName(target, args),
45
+ target,
46
+ type,
47
+ args,
48
+ };
49
+ }
50
+ function createName(target: any, args: any[]): string {
51
+ return args.reduce((prev, cur, i) => prev.replace(new RegExp(`\\{${i}\\}`, "g"), cur), target.stepName ?? target.name);
52
+ }
53
+
@@ -0,0 +1,8 @@
1
+ import { StepType } from "@vr1/be-driven-interface";
2
+
3
+ export interface TestStep {
4
+ type: StepType;
5
+ name: string;
6
+ target: Function;
7
+ args: any[];
8
+ }
@@ -0,0 +1 @@
1
+ export * from "./TestScenario";
@@ -0,0 +1,13 @@
1
+ import { StepWrapper } from "../types";
2
+ import { TestStep } from "./TestStep";
3
+
4
+ export async function runTest(wrapper: StepWrapper, steps: TestStep[]): Promise<void> {
5
+ const store = {};
6
+
7
+ for (let step of steps) {
8
+ await wrapper(step.type, step.name, async () => {
9
+ const stepOutput = await step.target.call(store, ...step.args);
10
+ Object.assign(store, stepOutput);
11
+ });
12
+ }
13
+ }
@@ -0,0 +1,3 @@
1
+ import { StepType } from "@vr1/be-driven-interface";
2
+
3
+ export type StepWrapper = (type: StepType, name: string, func: () => Promise<void>) => Promise<void>;
@@ -0,0 +1 @@
1
+ export * from "./StepWrapper";