@step-forge/step-forge 0.0.5
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/.eslintignore +6 -0
- package/.eslintrc +18 -0
- package/.prettierignore +6 -0
- package/.prettierrc +15 -0
- package/CHANGELOG.md +28 -0
- package/LICENSE.md +9 -0
- package/README.md +289 -0
- package/cucumber.mjs +38 -0
- package/docs/assets/state_deps.gif +0 -0
- package/dts-bundle-generator.config.ts +11 -0
- package/features/basic.feature +21 -0
- package/features/exported.feature +6 -0
- package/features/steps/commonSteps.ts +93 -0
- package/features/steps/exportedSteps.ts +42 -0
- package/features/steps/world.ts +29 -0
- package/package.json +51 -0
- package/src/builderTypeUtils.ts +27 -0
- package/src/common.ts +138 -0
- package/src/given.ts +102 -0
- package/src/index.ts +6 -0
- package/src/then.ts +128 -0
- package/src/utils.ts +74 -0
- package/src/vite-env.d.ts.old +1 -0
- package/src/when.ts +118 -0
- package/src/world.ts +90 -0
- package/test/givenCompilationTests.ts +130 -0
- package/test/testUtils.ts +30 -0
- package/test/thenCompilationTests.ts +207 -0
- package/test/whenCompilationTests.ts +157 -0
- package/ts-node-esm-register.js +8 -0
- package/tsconfig.cucumber.json +23 -0
- package/tsconfig.json +26 -0
- package/vite.config.ts +36 -0
package/.eslintignore
ADDED
package/.eslintrc
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"root": true,
|
|
3
|
+
"parser": "@typescript-eslint/parser",
|
|
4
|
+
"plugins": ["@typescript-eslint", "prettier"],
|
|
5
|
+
"extends": [
|
|
6
|
+
"eslint:recommended",
|
|
7
|
+
"plugin:@typescript-eslint/eslint-recommended",
|
|
8
|
+
"plugin:@typescript-eslint/recommended",
|
|
9
|
+
"prettier"
|
|
10
|
+
],
|
|
11
|
+
"env": {
|
|
12
|
+
"browser": true,
|
|
13
|
+
"node": true
|
|
14
|
+
},
|
|
15
|
+
"rules": {
|
|
16
|
+
"prettier/prettier": "error"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/.prettierignore
ADDED
package/.prettierrc
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"printWidth": 80,
|
|
3
|
+
"tabWidth": 2,
|
|
4
|
+
"singleQuote": false,
|
|
5
|
+
"trailingComma": "es5",
|
|
6
|
+
"arrowParens": "avoid",
|
|
7
|
+
"bracketSpacing": true,
|
|
8
|
+
"useTabs": false,
|
|
9
|
+
"endOfLine": "auto",
|
|
10
|
+
"singleAttributePerLine": false,
|
|
11
|
+
"bracketSameLine": false,
|
|
12
|
+
"jsxSingleQuote": false,
|
|
13
|
+
"quoteProps": "as-needed",
|
|
14
|
+
"semi": true
|
|
15
|
+
}
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to the "@step-forge/step-forge" project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [0.0.5] - 2024-12-31
|
|
6
|
+
|
|
7
|
+
- Add support for exporting steps
|
|
8
|
+
|
|
9
|
+
## [0.0.4] - 2024-07-30
|
|
10
|
+
|
|
11
|
+
- Update all packages to the latest versions
|
|
12
|
+
- Remove support for CommonJS modules
|
|
13
|
+
|
|
14
|
+
## [0.0.3] - 2024-01-23
|
|
15
|
+
|
|
16
|
+
- Update all packages to the latest versions (update to vite 5.x)
|
|
17
|
+
|
|
18
|
+
## [0.0.2] - 2023-01-18
|
|
19
|
+
|
|
20
|
+
- Update all packages to the latest versions (update to vite 4.x)
|
|
21
|
+
|
|
22
|
+
## [0.0.1] - 2022-09-08
|
|
23
|
+
|
|
24
|
+
- Update all packages to the latest versions (update to vite 3.x)
|
|
25
|
+
|
|
26
|
+
## [0.0.0] - 2022-03-28
|
|
27
|
+
|
|
28
|
+
- Initial release
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Justin Dragos
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# Step Forge
|
|
2
|
+
|
|
3
|
+
Step Forge is a typed wrapper around the Cucumber library. It provides an opinionated way to define steps, work with scenario state, and enforce dependencies between steps.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @step-forge/step-forge
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Getting Started
|
|
13
|
+
### Setting Up World State
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// features/steps/world.ts
|
|
17
|
+
import { setWorldConstructor } from "@cucumber/cucumber";
|
|
18
|
+
import { BasicWorld } from "@step-forge/step-forge";
|
|
19
|
+
|
|
20
|
+
export interface GivenState {
|
|
21
|
+
user: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface WhenState {
|
|
25
|
+
name: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ThenState {}
|
|
29
|
+
|
|
30
|
+
setWorldConstructor(BasicWorld<GivenState, WhenState, ThenState>);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Defining Steps
|
|
34
|
+
|
|
35
|
+
This defines a `given` step that doesn't need input variables or dependencies.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// features/steps/common.ts
|
|
39
|
+
import { givenBuilder } from "@step-forge/step-forge";
|
|
40
|
+
|
|
41
|
+
type GivenState = {
|
|
42
|
+
user: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
givenBuilder<GivenState>()
|
|
46
|
+
.statement("a user")
|
|
47
|
+
.step(() => {
|
|
48
|
+
return {
|
|
49
|
+
user: "John Doe",
|
|
50
|
+
};
|
|
51
|
+
})
|
|
52
|
+
.register();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This creates a cucumber step that conceptually looks like this:
|
|
56
|
+
```ts
|
|
57
|
+
Given("a user", function (this: World) {
|
|
58
|
+
this.given.merge({
|
|
59
|
+
user: "John Doe",
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Not very exciting if you are used to cucumber, but it gets better as this get more complex.
|
|
65
|
+
|
|
66
|
+
#### Defining Steps with Statement Variables
|
|
67
|
+
|
|
68
|
+
Let's create a similar step that takes the name as a variable instead of hardcoding it.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
// features/steps/common.ts
|
|
72
|
+
import { givenBuilder } from "@step-forge/step-forge";
|
|
73
|
+
|
|
74
|
+
type GivenState = {
|
|
75
|
+
user: string;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
givenBuilder<GivenState>()
|
|
79
|
+
.statement((userName: string) => `a user named ${userName}`)
|
|
80
|
+
.step(({ variables: [ userName ] }) => {
|
|
81
|
+
return {
|
|
82
|
+
user: userName,
|
|
83
|
+
};
|
|
84
|
+
})
|
|
85
|
+
.register();
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Here you can see that you can pass a template function to the statement that will be used to generate the statement for the cucumber step. Additionally, the `variables` array is strongly typed to match the variables provided in the statement function.
|
|
89
|
+
|
|
90
|
+
> Note: Right now only `string` variables are supported, but we're working to improve this.
|
|
91
|
+
|
|
92
|
+
This creates a cucumber step that conceptually looks like this:
|
|
93
|
+
```ts
|
|
94
|
+
Given("a user named {name}", function (this: World, name: string) {
|
|
95
|
+
this.given.merge({
|
|
96
|
+
user: name,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Defining Steps with Dependencies
|
|
102
|
+
|
|
103
|
+
Now let's added other scenario dependencies to the step. Let's assume we have another step that sets the user's real name into context, and now we want to give them a user name in our step.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// features/steps/common.ts
|
|
107
|
+
import { givenBuilder } from "@step-forge/step-forge";
|
|
108
|
+
|
|
109
|
+
type GivenState = {
|
|
110
|
+
realName: string;
|
|
111
|
+
user: {
|
|
112
|
+
name: string;
|
|
113
|
+
realName: string;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
givenBuilder<GivenState>()
|
|
118
|
+
.statement((userName: string) => `a user named ${userName}`)
|
|
119
|
+
// Since this is a given step, only allow dependencies on given state
|
|
120
|
+
// Strongly typed to ensure you can't depend on something not in given state
|
|
121
|
+
.dependencies({ given: { realName: "required"}})
|
|
122
|
+
.step(({ variables: [ userName ], given: { realName } }) => {
|
|
123
|
+
return {
|
|
124
|
+
user: {
|
|
125
|
+
name: userName,
|
|
126
|
+
// realName is of type `string` and is not possibly undefined since it was required
|
|
127
|
+
realName,
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
})
|
|
131
|
+
.register();
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Now this is getting more interesting. We've added a dependency on the `realName` part of given state. Step Forge strongly types dependencies based on your world state, and properly restricts access based on the step type. Additionally, since `realName` is required Step Forge will fail the scenario if it doesn't exist when the step is run.
|
|
135
|
+
|
|
136
|
+
This creates a cucumber step that conceptually looks like this:
|
|
137
|
+
```ts
|
|
138
|
+
Given("a user named {name}", function (this: World, name: string) {
|
|
139
|
+
if (!this.given.realName) {
|
|
140
|
+
fail("given.realName is required, but was not provided");
|
|
141
|
+
}
|
|
142
|
+
this.given.merge({
|
|
143
|
+
user: {
|
|
144
|
+
name,
|
|
145
|
+
realName: this.given.realName,
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### Complex Dependencies
|
|
152
|
+
|
|
153
|
+
Now let's add a `then` step to check that the user's realName matches what we expected in the given step.
|
|
154
|
+
|
|
155
|
+
```gherkin
|
|
156
|
+
# features/update-username.feature
|
|
157
|
+
Feature: Updating a user's username
|
|
158
|
+
|
|
159
|
+
Scenario: Updating a user's username
|
|
160
|
+
Given a real name of "John Doe"
|
|
161
|
+
And a user name of "Furball"
|
|
162
|
+
When I update the username
|
|
163
|
+
Then the user's real name should not change
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Now let's implement the `then` step.
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
// features/steps/common.ts
|
|
170
|
+
import { thenBuilder } from "@step-forge/step-forge";
|
|
171
|
+
|
|
172
|
+
type GivenState = {
|
|
173
|
+
realName: string;
|
|
174
|
+
user: {
|
|
175
|
+
name: string;
|
|
176
|
+
realName: string;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
type WhenState = {
|
|
181
|
+
user: {
|
|
182
|
+
name: string;
|
|
183
|
+
realName: string;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
type ThenState = {}
|
|
188
|
+
|
|
189
|
+
thenBuilder<GivenState, WhenState, ThenState>()
|
|
190
|
+
.statement("the user's real name should not change")
|
|
191
|
+
.dependencies({
|
|
192
|
+
given: { realName: "required" },
|
|
193
|
+
when: { user: "required" }
|
|
194
|
+
})
|
|
195
|
+
.step(({ given: { realName: expectedRealName }, when: { user: updatedUser } }) => {
|
|
196
|
+
expect(updatedUser.realName).toEqual(expectedRealName);
|
|
197
|
+
// Then steps can return void if they are not adding anything to `then` state
|
|
198
|
+
})
|
|
199
|
+
.register();
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Even though we have complex dependencies across multiple parts of state, Step Forge ensures that the dependencies are properly enforced. It also provides type safety ensuring that the step can't depend on something from state that isn't explicitly declared.
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
### Simpler Step Definitions
|
|
206
|
+
|
|
207
|
+
> Note: Defining steps this way currently doesn't work with the vs code extension. We're working to improve this.
|
|
208
|
+
|
|
209
|
+
Some people find the builder syntax to be a bit verbose, having to provide the generics for `GivenState`, `WhenState`, and `ThenState`. If you prefer a simpler syntax you can export your own definitions that have some of it built in.
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
// features/steps/common.ts
|
|
213
|
+
|
|
214
|
+
export const Given = givenBuilder<GivenState>().statement;
|
|
215
|
+
export const When = whenBuilder<GivenState, WhenState>().statement;
|
|
216
|
+
export const Then = thenBuilder<GivenState, WhenState, ThenState>().statement;
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
You can now define your steps like this:
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
// features/steps/user.ts
|
|
223
|
+
import { Given } from "./common";
|
|
224
|
+
|
|
225
|
+
// You can now start your definition with the statement directly, without having to provide GivenState or chain through the builder.
|
|
226
|
+
Given("a user")
|
|
227
|
+
.dependencies({ given: { realName: "required" } })
|
|
228
|
+
.step(({ given: { realName } }) => {
|
|
229
|
+
return {
|
|
230
|
+
user: {
|
|
231
|
+
realName,
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
})
|
|
235
|
+
.register();
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Compile Time Gherkin Analysis
|
|
239
|
+
|
|
240
|
+
> Note: These features are currently only available in the `@step-forge/step-forge-vscode`VS code extension.
|
|
241
|
+
|
|
242
|
+
Hopefully you are excited about the strong typing and dependency enforcement that Step Forge provides. But there is more to it than just that. Since we each steps declares its dependencies, and we know what it sets in state from its `return` type, Step Forge can analyze your Gherkin scenarios and provide errors if there are unsatisfied dependencies.
|
|
243
|
+
|
|
244
|
+
For instance, take the following steps and scenario:
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
// features/steps/user.ts
|
|
248
|
+
import { Given } from "./common";
|
|
249
|
+
|
|
250
|
+
type GivenState = {
|
|
251
|
+
name: string;
|
|
252
|
+
user: {
|
|
253
|
+
name: string;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
Given((name: string) => `a user named ${name}`)
|
|
258
|
+
.step(({ variables: [ name ] }) => {
|
|
259
|
+
return { name }
|
|
260
|
+
})
|
|
261
|
+
.register();
|
|
262
|
+
|
|
263
|
+
When("I save the user")
|
|
264
|
+
.dependencies({ given: { user: "required" } })
|
|
265
|
+
.step(({ given: { user } }) => {
|
|
266
|
+
// save the user
|
|
267
|
+
})
|
|
268
|
+
.register();
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
```gherkin
|
|
272
|
+
# features/update-username.feature
|
|
273
|
+
Feature: Updating a user's username
|
|
274
|
+
|
|
275
|
+
Scenario: Updating a user's username
|
|
276
|
+
Given a user named "Furball"
|
|
277
|
+
# This step will show a compilation error because the given step sets "name", but the when step depends on "user"
|
|
278
|
+
When I save the user
|
|
279
|
+
Then the user's real name should not change
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
#### Autocomplete
|
|
283
|
+
|
|
284
|
+
Additionally when adding a step to a feature file, if the step has unfulfilled dependencies, the extension will show a list of steps that can be used to fulfill the dependencies.
|
|
285
|
+
|
|
286
|
+

|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
package/cucumber.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const defaultProfile = {
|
|
2
|
+
format: [
|
|
3
|
+
process.env.CI || !process.stdout.isTTY ? "progress" : "progress-bar",
|
|
4
|
+
"json:./reports/cucumber-json-reports/report.json",
|
|
5
|
+
"rerun:./reports/cucumber/@rerun.txt",
|
|
6
|
+
"usage:./reports/cucumber/usage.txt",
|
|
7
|
+
],
|
|
8
|
+
parallel: 1,
|
|
9
|
+
importModule: ["ts-node/esm", "tsconfig-paths/register"],
|
|
10
|
+
import: [
|
|
11
|
+
"./features/steps/**/*.ts",
|
|
12
|
+
"./features/steps/*.ts",
|
|
13
|
+
"./src/**/*.ts",
|
|
14
|
+
],
|
|
15
|
+
strict: false,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const ciProfile = {
|
|
19
|
+
format: [
|
|
20
|
+
process.env.CI || !process.stdout.isTTY ? "progress" : "progress-bar",
|
|
21
|
+
"json:./reports/cucumber-json-reports/report.json",
|
|
22
|
+
"rerun:./reports/cucumber/@rerun.txt",
|
|
23
|
+
"usage:./reports/cucumber/usage.txt",
|
|
24
|
+
],
|
|
25
|
+
parallel: 1,
|
|
26
|
+
importModule: ["ts-node/register/transpile-only", "tsconfig-paths/register"],
|
|
27
|
+
import: ["./features/steps/**/*.ts", "./features/steps/*.ts"],
|
|
28
|
+
strict: false,
|
|
29
|
+
publish: true,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const all = {
|
|
33
|
+
...defaultProfile,
|
|
34
|
+
paths: ["./features"],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export { ciProfile as ci, all };
|
|
38
|
+
export default defaultProfile;
|
|
Binary file
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Feature: Steps written in gherkin can be matched to Step Forge steps
|
|
2
|
+
|
|
3
|
+
Scenario: Basic feature steps with no dependencies or variables can be run as a cucumber scenario
|
|
4
|
+
Given I started
|
|
5
|
+
When I got here
|
|
6
|
+
Then everything was good
|
|
7
|
+
|
|
8
|
+
Scenario: Basic feature steps with dependencies can be run as a cucumber scenario
|
|
9
|
+
Given a user
|
|
10
|
+
When I save the user
|
|
11
|
+
Then there is a user
|
|
12
|
+
|
|
13
|
+
Scenario: Basic feature steps with variables can be run as a cucumber scenario
|
|
14
|
+
Given a user named "John"
|
|
15
|
+
When I save the user
|
|
16
|
+
Then there is a user
|
|
17
|
+
|
|
18
|
+
Scenario: Basic feature steps with dependencies and variables can be run as a cucumber scenario
|
|
19
|
+
Given a user
|
|
20
|
+
When I name the user "John"
|
|
21
|
+
Then the user's name is "John"
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { givenBuilder } from "../../src/given";
|
|
2
|
+
import { whenBuilder } from "../../src/when";
|
|
3
|
+
import { thenBuilder } from "../../src/then";
|
|
4
|
+
import { GivenState, ThenState, WhenState } from "./world";
|
|
5
|
+
import { expect } from "earl";
|
|
6
|
+
|
|
7
|
+
// --- No dependency no variable steps --- //
|
|
8
|
+
givenBuilder<GivenState>()
|
|
9
|
+
.statement("I started")
|
|
10
|
+
.step(() => ({}))
|
|
11
|
+
.register();
|
|
12
|
+
|
|
13
|
+
whenBuilder<GivenState, WhenState>()
|
|
14
|
+
.statement("I got here")
|
|
15
|
+
.step(() => ({}))
|
|
16
|
+
.register();
|
|
17
|
+
|
|
18
|
+
thenBuilder<GivenState, WhenState, ThenState>()
|
|
19
|
+
.statement("everything was good")
|
|
20
|
+
.step(() => ({}))
|
|
21
|
+
.register();
|
|
22
|
+
|
|
23
|
+
// --- Dependency only steps --- //
|
|
24
|
+
givenBuilder<GivenState>()
|
|
25
|
+
.statement("a user")
|
|
26
|
+
.step(() => {
|
|
27
|
+
return {
|
|
28
|
+
user: {
|
|
29
|
+
type: "person",
|
|
30
|
+
token: "random",
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
})
|
|
34
|
+
.register();
|
|
35
|
+
|
|
36
|
+
whenBuilder<GivenState, WhenState>()
|
|
37
|
+
.statement("I save the user")
|
|
38
|
+
.dependencies({ given: { user: "required" } })
|
|
39
|
+
.step(({ given: { user } }) => {
|
|
40
|
+
return {
|
|
41
|
+
user: {
|
|
42
|
+
...user,
|
|
43
|
+
saved: true,
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
})
|
|
47
|
+
.register();
|
|
48
|
+
|
|
49
|
+
thenBuilder<GivenState, WhenState, ThenState>()
|
|
50
|
+
.statement("there is a user")
|
|
51
|
+
.dependencies({ when: { user: "required" } })
|
|
52
|
+
.step(({ when: { user } }) => {
|
|
53
|
+
expect(user.saved).toBeTruthy();
|
|
54
|
+
})
|
|
55
|
+
.register();
|
|
56
|
+
|
|
57
|
+
// --- Variable only steps --- //
|
|
58
|
+
givenBuilder<GivenState>()
|
|
59
|
+
.statement((userName: string) => `a user named ${userName}`)
|
|
60
|
+
.step(({ variables: [userName] }) => {
|
|
61
|
+
return {
|
|
62
|
+
user: {
|
|
63
|
+
type: "person",
|
|
64
|
+
token: userName,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
})
|
|
68
|
+
.register();
|
|
69
|
+
|
|
70
|
+
// --- More complex steps --- //
|
|
71
|
+
|
|
72
|
+
whenBuilder<GivenState, WhenState>()
|
|
73
|
+
.statement((userName: string) => `I name the user ${userName}`)
|
|
74
|
+
.dependencies({ given: { user: "required" } })
|
|
75
|
+
.step(({ given: { user }, variables: [userName] }) => {
|
|
76
|
+
return {
|
|
77
|
+
user: {
|
|
78
|
+
...user,
|
|
79
|
+
token: userName,
|
|
80
|
+
saved: true,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
})
|
|
84
|
+
.register();
|
|
85
|
+
|
|
86
|
+
thenBuilder<GivenState, WhenState, ThenState>()
|
|
87
|
+
.statement((userName: string) => `the user's name is ${userName}`)
|
|
88
|
+
.dependencies({ when: { user: "required" } })
|
|
89
|
+
.step(({ when: { user }, variables: [userName] }) => {
|
|
90
|
+
const token = user.token;
|
|
91
|
+
expect(token).toEqual(userName);
|
|
92
|
+
})
|
|
93
|
+
.register();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { expect } from "earl";
|
|
2
|
+
import { givenBuilder } from "../../src/given";
|
|
3
|
+
import { thenBuilder } from "../../src/then";
|
|
4
|
+
import { whenBuilder } from "../../src/when";
|
|
5
|
+
import { GivenState, ThenState, WhenState } from "./world";
|
|
6
|
+
|
|
7
|
+
const Given = givenBuilder<GivenState>().statement;
|
|
8
|
+
const When = whenBuilder<GivenState, WhenState>().statement;
|
|
9
|
+
const Then = thenBuilder<GivenState, WhenState, ThenState>().statement;
|
|
10
|
+
|
|
11
|
+
Given("a bank user")
|
|
12
|
+
.step(() => {
|
|
13
|
+
return {
|
|
14
|
+
user: {
|
|
15
|
+
type: "customer",
|
|
16
|
+
token: "random",
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
})
|
|
20
|
+
.register();
|
|
21
|
+
|
|
22
|
+
When((amount: string, currency: string) => `I deposit ${amount} ${currency}`)
|
|
23
|
+
.dependencies({ given: { user: "required" } })
|
|
24
|
+
.step(({ variables: [rawAmount, currency], given: { user } }) => {
|
|
25
|
+
const amount = parseFloat(rawAmount);
|
|
26
|
+
return {
|
|
27
|
+
deposit: {
|
|
28
|
+
amount,
|
|
29
|
+
currency,
|
|
30
|
+
user,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
})
|
|
34
|
+
.register();
|
|
35
|
+
|
|
36
|
+
Then((amount: string) => `the balance is ${amount}`)
|
|
37
|
+
.dependencies({ when: { deposit: "required" } })
|
|
38
|
+
.step(({ when: { deposit }, variables: [rawAmount] }) => {
|
|
39
|
+
const amount = parseFloat(rawAmount);
|
|
40
|
+
expect(deposit.amount).toEqual(amount);
|
|
41
|
+
})
|
|
42
|
+
.register();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { setWorldConstructor } from "@cucumber/cucumber";
|
|
2
|
+
import { BasicWorld } from "../../src/world";
|
|
3
|
+
|
|
4
|
+
export interface GivenState {
|
|
5
|
+
user: {
|
|
6
|
+
type: string;
|
|
7
|
+
token: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface WhenState {
|
|
12
|
+
user: {
|
|
13
|
+
type: string;
|
|
14
|
+
token: string;
|
|
15
|
+
saved: boolean;
|
|
16
|
+
};
|
|
17
|
+
deposit: {
|
|
18
|
+
amount: number;
|
|
19
|
+
currency: string;
|
|
20
|
+
user: {
|
|
21
|
+
type: string;
|
|
22
|
+
token: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ThenState {}
|
|
28
|
+
|
|
29
|
+
setWorldConstructor(BasicWorld<GivenState, WhenState, ThenState>);
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@step-forge/step-forge",
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"module": "./dist/step-forge.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/step-forge.js"
|
|
9
|
+
},
|
|
10
|
+
"./dist/": {
|
|
11
|
+
"import": "./dist/"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "vite --host",
|
|
17
|
+
"build": "rimraf build/**/* && tsc && vite build && dts-bundle-generator --config ./dts-bundle-generator.config.ts && copyfiles ./package.json build",
|
|
18
|
+
"test:cucumber": "NODE_ENV=test NODE_OPTIONS=\"--experimental-specifier-resolution=node --loader ts-node/esm\" TS_NODE_PROJECT=./tsconfig.cucumber.json cucumber-js -p default",
|
|
19
|
+
"test:ci": "NODE_ENV=test cucumber-js -p ci",
|
|
20
|
+
"test": "NODE_ENV=test PORT=7888 LOG_LEVEL=none cucumber-js -p all 2> /dev/null",
|
|
21
|
+
"test:debug": "NODE_ENV=test PORT=7888 cucumber-js -p all",
|
|
22
|
+
"lint": "eslint . --ext .ts",
|
|
23
|
+
"format": "prettier . --write"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@cucumber/pretty-formatter": "^1.0.1",
|
|
27
|
+
"@types/jsdom": "^21.1.7",
|
|
28
|
+
"@types/lodash": "^4.17.13",
|
|
29
|
+
"@types/node": "^22.10.2",
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
31
|
+
"@typescript-eslint/parser": "^7.18.0",
|
|
32
|
+
"@vitest/coverage-v8": "^2.0.4",
|
|
33
|
+
"copyfiles": "^2.4.1",
|
|
34
|
+
"dts-bundle-generator": "^9.5.1",
|
|
35
|
+
"earl": "^1.3.0",
|
|
36
|
+
"eslint": "^8.57.0",
|
|
37
|
+
"eslint-config-prettier": "^9.1.0",
|
|
38
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
39
|
+
"prettier": "^3.3.3",
|
|
40
|
+
"rimraf": "^6.0.1",
|
|
41
|
+
"ts-node": "^10.9.2",
|
|
42
|
+
"tsconfig-paths": "^4.2.0",
|
|
43
|
+
"typescript": "^5.7.2",
|
|
44
|
+
"vite": "^5.3.5",
|
|
45
|
+
"vitest": "^2.0.4"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@cucumber/cucumber": "^11.1.1",
|
|
49
|
+
"lodash": "^4.17.21"
|
|
50
|
+
}
|
|
51
|
+
}
|