Skip to content

Commit bc0695e

Browse files
committed
Add steps resolver option, add ReScript bindings
1 parent 47dcef4 commit bc0695e

24 files changed

+293
-160
lines changed

README.md

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ Tests can run in parallel (no shared state) and are fast and hot reloadable.
1616
**Features**
1717

1818
- **write with Gherkin, execute with vitest !**
19-
- Gherkin in markdown
19+
- Gherkin block inside markdown
20+
- ReScript step definitions
2021
- async tests
2122
- concurrent testing
2223
- failed tests in steps definitions and Gherkin
2324
- supports number, string and table parameters
2425
- steps are explicitly linked to your context (easy to trace usage)
2526
- supports "Background"
26-
- experimental "Gherkin in markdown"
2727
- ESM and CJS projects support
2828
- Gherkin parsing with [@cucumber/gherkin](https://github.com/cucumber/gherkin).
2929

@@ -61,6 +61,43 @@ Options are passed as an object to the vitestBdd function. The default options a
6161
debug: false,
6262
markdownExtensions: [".md", ".mdx", ".markdown"],
6363
gherkinExtensions: [".feature"],
64+
stepsResolver: stepsResolver,
65+
}
66+
```
67+
68+
And the default stepsResolver function is below. This resulver would find the following files for a "./test/foobar.feature" file:
69+
70+
- `./test/foobar.feature.ts`
71+
- `./test/foobar.feature.js`
72+
- ... etc
73+
- `./test/foobar.steps.ts`
74+
- `./test/foobar.steps.js`
75+
- ... etc
76+
- `./test/foobarSteps.ts`
77+
- `./test/foobarSteps.js`
78+
- ... etc
79+
80+
The last setting helps for ReScript users to mach `./test/Foobar.feature` with `./test/FoobarSteps.res` steps files.
81+
82+
```ts
83+
function baseResolver(path: string): string | null {
84+
for (const ext of [".ts", ".js", ".mjs", ".cjs", ".res.mjs"]) {
85+
const p = `${path}${ext}`;
86+
if (existsSync(p)) {
87+
return p;
88+
}
89+
}
90+
return null;
91+
}
92+
93+
export function stepsResolver(path: string): string | null {
94+
for (const r of [".feature", ".steps", "Steps"]) {
95+
const p = baseResolver(path.replace(/\.feature$/, r));
96+
if (p) {
97+
return p;
98+
}
99+
}
100+
return null;
64101
}
65102
```
66103

@@ -129,15 +166,15 @@ Given("I have a {string} calculator", async ({ When, And, Then }, type) => {
129166
And("I subtract {number} and {number}", calculator.subtract);
130167
And("I multiply {number} by {number}", calculator.multiply);
131168
And("I divide {number} by {number}", calculator.divide);
132-
resultAssertions(calculator);
169+
resultAssertions(Then, calculator);
133170
break;
134171
}
135172
case "rpn": {
136173
const calculator = rpnCalculator();
137174
When("I enter {number}", calculator.enter);
138175
And("I enter {number}", calculator.enter);
139176
And("I divide", calculator.divide);
140-
resultAssertions(calculator);
177+
resultAssertions(Then, calculator);
141178
break;
142179
}
143180
default:
@@ -146,6 +183,41 @@ Given("I have a {string} calculator", async ({ When, And, Then }, type) => {
146183
});
147184
```
148185

186+
For ReScript, the bindings are a little bit simpler for now:
187+
188+
```rescript
189+
// src/domain/test/CalculatorSteps.res
190+
open VitestBdd
191+
192+
// To show assertion reuse (could be just added in the given block).
193+
let resultAssertions = (step, calculator) => {
194+
// We define an async step, just to look cool 😎 (again).
195+
step("the result is {number}", async (n: number) => {
196+
await calculator.proccessBigComputation()
197+
expect(calculator.result.value).toBe(n)
198+
})
199+
}
200+
201+
given("I have a {string} calculator", async ({step}, ctype) => {
202+
switch ctype {
203+
| "basic" => {
204+
let calculator = basicCalculator()
205+
step("I add {number} and {number}", calculator.add)
206+
step("I subtract {number} and {number}", calculator.subtract)
207+
step("I multiply {number} by {number}", calculator.multiply)
208+
step("I divide {number} by {number}", calculator.divide)
209+
resultAssertions(step, calculator)
210+
}
211+
| "rpn": {
212+
let calculator = rpnCalculator()
213+
step("I enter {number}", calculator.enter)
214+
step("I enter {number}", calculator.enter)
215+
step("I divide", calculator.divide)
216+
resultAssertions(step, calculator)
217+
}
218+
})
219+
```
220+
149221
### Reuse steps
150222

151223
You can reuse steps in multiple contexts. For example, a preference manager

bin/publish-canary.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

bin/publish.sh

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
set -e
44

5+
# ============================================ UTILS and basic CHECKS
6+
57
# Set up environment
68
if ! command -v pnpm &>/dev/null; then
79
echo "pnpm is not installed. Please install it first."
@@ -28,25 +30,53 @@ is_semver() {
2830
fi
2931
}
3032

33+
DATE=$(date +'%Y%m%dT%H%M%S')
34+
3135
# Install dependencies
3236
pnpm i
33-
# Check compilation for all projects
37+
# Rebuild for all projects
3438
pnpm build
35-
# Copy README.md
36-
cp README.md vitest-bdd/README.md
3739

38-
# ================ VITEST-BDD
39-
cd vitest-bdd
40-
LIB_VERSION=$(npm pkg get version | sed 's/"//g')
41-
is_semver "$LIB_VERSION"
40+
VERSION=$(npm pkg get version | sed 's/"//g')
41+
is_semver "$VERSION"
42+
43+
# ============================================ PUBLISH
4244

43-
pnpm publish --access public --no-git-checks
45+
# Update version if publishing beta (--beta argument)
46+
if [[ $1 == "--beta" ]]; then
47+
VERSION=$VERSION-beta.$DATE
48+
elif [[ $1 == "--canary" ]]; then
49+
VERSION=$VERSION-canary.$DATE
50+
fi
51+
52+
# ================ vitest-bdd
53+
cd vitest-bdd
54+
npm --no-git-tag-version version $VERSION
4455

56+
if [[ $1 == "--beta" ]]; then
57+
pnpm publish --tag beta --access public --no-git-checks
58+
elif [[ $1 == "--canary" ]]; then
59+
CANARY=true pnpm publish --tag canary --access public --no-git-checks
60+
else
61+
pnpm publish --access public --no-git-checks
62+
fi
4563
cd ..
4664

65+
echo "Wait for version to propagate on npm"
66+
sleep 3
67+
echo "Wait for version to propagate on npm"
68+
sleep 3
69+
echo "Wait for version to propagate on npm"
70+
sleep 3
71+
4772
# Reset git repo
4873
git reset --hard HEAD
49-
git clean -fd
50-
git tag v$LIB_VERSION
5174

52-
echo "Published successfully!"
75+
if [[ $1 == "--beta" ]]; then
76+
echo "Beta versions published successfully!"
77+
elif [[ $1 == "--canary" ]]; then
78+
echo "Canary versions published successfully!"
79+
else
80+
echo "Published successfully!"
81+
fi
82+

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vitest-bdd-monorepo",
33
"private": true,
4-
"version": "0.0.0",
4+
"version": "0.4.0",
55
"description": "Monorepo for vitest-bdd plugin and related packages.",
66
"homepage": "https://github.com/midasum/vitest-bdd",
77
"repository": {
@@ -10,9 +10,10 @@
1010
},
1111
"scripts": {
1212
"build": "pnpm -r build",
13-
"test": "pnpm -r test",
14-
"canary": "./bin/publish-canary.sh",
15-
"pub": "./bin/publish.sh"
13+
"canary": "./bin/publish.sh --canary",
14+
"beta": "./bin/publish.sh --beta",
15+
"pub": "./bin/publish.sh",
16+
"test": "pnpm -r test"
1617
},
1718
"author": "Gaspard Anna Bucher <g.a.bucher@midasum.com>",
1819
"license": "MIT"

pnpm-lock.yaml

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-app/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"res:dev": "rescript -w"
1111
},
1212
"devDependencies": {
13+
"tilia": "3.0.0-beta.20250817T011709",
1314
"vite-tsconfig-paths": "^5.1.4",
1415
"vitest": "^3.1.3",
15-
"vitest-bdd": "workspace:^"
16+
"vitest-bdd": "file:../vitest-bdd"
1617
},
1718
"dependencies": {
1819
"@rescript/core": "1.6.1",
19-
"rescript": "11.1.4",
20-
"tilia": "2.0.0"
20+
"rescript": "11.1.4"
2121
}
2222
}

test-app/rescript.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"suffix": ".mjs",
1212
"bs-dependencies": [
1313
"@rescript/core",
14+
"vitest-bdd",
1415
"tilia"
1516
],
1617
"bsc-flags": [

test-app/src/domain/feature/ResCalculator.mjs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
import * as Tilia from "tilia/src/Tilia.mjs";
44

55
function make(name) {
6-
var result = Tilia.signal(0);
7-
var title = Tilia.signal(name);
6+
var match = Tilia.signal(0);
7+
var setResult = match[1];
8+
var result = match[0];
89
return Tilia.tilia({
910
add: (function (a, b) {
10-
result.value = a + b;
11+
setResult(a + b);
1112
}),
1213
subtract: (function (a, b) {
13-
result.value = a - b;
14+
setResult(a - b);
1415
}),
1516
multiply: (function (a, b) {
16-
result.value = a * b;
17+
setResult(a * b);
1718
}),
1819
divide: (function (a, b) {
19-
result.value = a / b;
20+
setResult(a / b);
2021
}),
2122
result: Tilia.computed(function () {
2223
return result.value;
2324
}),
24-
title: Tilia.computed(function () {
25-
return title.value;
26-
})
25+
title: name
2726
});
2827
}
2928

0 commit comments

Comments
 (0)