Skip to content

Commit aa86077

Browse files
committed
Add source maps to rescript tests
1 parent bc0695e commit aa86077

File tree

11 files changed

+348
-15
lines changed

11 files changed

+348
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ And finally, here are some nice extensions for VS Code that can support your BDD
393393

394394
# Changelog
395395

396+
- **0.4.0** (2025-08-17)
397+
- Add support for ReScript unit tests (with source maps!)
396398
- **0.3.0** (2025-07-26)
397399
- Add options for markdown extension (and default support for .mdx)
398400
- **0.2.0** (2025-07-23)

launch.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Vitest - bdd",
6+
"type": "node",
7+
"request": "launch",
8+
"runtimeExecutable": "pnpm",
9+
"runtimeArgs": [
10+
"run",
11+
"test:watch"
12+
],
13+
"cwd": "${workspaceRoot}/vitest-bdd",
14+
"protocol": "inspector",
15+
"console": "integratedTerminal",
16+
"internalConsoleOptions": "neverOpen"
17+
},
18+
{
19+
"name": "Vitest - app",
20+
"type": "node",
21+
"request": "launch",
22+
"runtimeExecutable": "pnpm",
23+
"runtimeArgs": [
24+
"run",
25+
"test:watch"
26+
],
27+
"cwd": "${workspaceRoot}/test-app",
28+
"protocol": "inspector",
29+
"console": "integratedTerminal",
30+
"internalConsoleOptions": "neverOpen"
31+
}
32+
]
33+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"canary": "./bin/publish.sh --canary",
1414
"beta": "./bin/publish.sh --beta",
1515
"pub": "./bin/publish.sh",
16+
"app": "pnpm -filter test-app",
17+
"bdd": "pnpm -filter vitest-bdd",
1618
"test": "pnpm -r test"
1719
},
1820
"author": "Gaspard Anna Bucher <g.a.bucher@midasum.com>",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Vitest from "vitest";
4+
5+
Vitest.describe("ReScript tests and source maps", (function () {
6+
Vitest.it("should add two numbers", (function () {
7+
Vitest.expect(4).toBe(4);
8+
}));
9+
Vitest.it("should work with async", (async function () {
10+
return Vitest.expect(9).toBe(9);
11+
}));
12+
Vitest.it("should have more tests", (function () {
13+
Vitest.expect(3).toBe(3);
14+
}));
15+
Vitest.it("should have more and more tests", (function () {
16+
Vitest.expect(3).toBe(3);
17+
}));
18+
Vitest.it.todo("todo");
19+
Vitest.it.skip("skipped test", (function () {
20+
Vitest.expect(3).toBe(3);
21+
}));
22+
}));
23+
24+
export {
25+
26+
}
27+
/* Not a pure module */
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
open VitestBdd
2+
3+
describe("ReScript tests and source maps", () => {
4+
it("should add two numbers", () => {
5+
expect(2 + 2).toBe(4)
6+
})
7+
8+
it("should work with async", async () => {
9+
expect(11 - 2).toBe(9)
10+
})
11+
12+
it("should have more tests", () => {
13+
expect(5 - 2).toBe(3)
14+
})
15+
16+
it("should have more and more tests", () => {
17+
expect(5 - 2).toBe(3)
18+
})
19+
20+
Todo.it("todo")
21+
Skip.it("skipped test", () => {
22+
expect(5 - 2).toBe(3)
23+
})
24+
})

test-app/vitest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export default defineConfig({
1212
],
1313
test: {
1414
pool: "threads",
15-
include: ["**/*.feature", "**/*.md", "**/*.spec.ts"],
15+
// include: ["**/*.feature", "**/*.md", "**/*.spec.ts", "**/*_test.res"],
16+
include: ["**/*_test.res"],
1617
},
1718
});
1819

vitest-bdd/src/VitestBdd.mjs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
2-
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
2+
3+
4+
var Skip = {};
5+
6+
var Only = {};
7+
8+
var Todo = {};
9+
10+
export {
11+
Skip ,
12+
Only ,
13+
Todo ,
14+
}
15+
/* No side effect */

vitest-bdd/src/VitestBdd.res

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,93 @@
11
type given = {step: 'a. (string, 'a) => unit}
22
type assertions<'a> = {toBe: 'a => unit, toEqual: 'a => unit}
33

4+
// ========= Gherkin ==========
5+
46
@module("vitest-bdd")
57
external given: (string, (given, 'args) => unit) => unit = "Given"
68

9+
// ========= Vitest ==========
10+
11+
type rec testContext = {
12+
onTestFailed: 'a. (testContext => 'a) => unit,
13+
onTestFinished: 'a. (testContext => 'a) => unit,
14+
}
15+
716
@module("vitest")
817
external expect: 'a => assertions<'a> = "expect"
18+
19+
@module("vitest")
20+
external concurrent: (string, unit => unit) => unit = "describe.concurrent"
21+
22+
@module("vitest")
23+
external beforeAll: (string, 'a) => unit = "beforeAll"
24+
25+
@module("vitest")
26+
external beforeEach: (string, 'a) => unit = "beforeEach"
27+
28+
@module("vitest")
29+
external afterAll: (string, 'a) => unit = "afterAll"
30+
31+
@module("vitest")
32+
external afterEach: (string, 'a) => unit = "afterEach"
33+
34+
@module("vitest")
35+
external onTestFinished: (unit => unit) => unit = "onTestFinished"
36+
37+
@module("vitest")
38+
external onTestFailed: (unit => unit) => unit = "onTestFinished"
39+
// ========= Modes ==========
40+
41+
@module("vitest")
42+
external describe: (string, unit => unit) => unit = "describe"
43+
44+
@module("vitest")
45+
external bench: (string, unit => unit) => unit = "bench"
46+
47+
@module("vitest")
48+
external test: (string, 'a) => unit = "test"
49+
50+
@module("vitest")
51+
external it: (string, 'a) => unit = "it"
52+
53+
module Skip = {
54+
@module("vitest") @scope("describe")
55+
external describe: (string, unit => unit) => unit = "skip"
56+
57+
@module("vitest") @scope("bench")
58+
external bench: (string, unit => unit) => unit = "skip"
59+
60+
@module("vitest") @scope("test")
61+
external test: (string, 'a) => unit = "skip"
62+
63+
@module("vitest") @scope("it")
64+
external it: (string, 'a) => unit = "skip"
65+
}
66+
67+
module Only = {
68+
@module("vitest") @scope("describe")
69+
external describe: (string, unit => unit) => unit = "only"
70+
71+
@module("vitest") @scope("bench")
72+
external bench: (string, unit => unit) => unit = "only"
73+
74+
@module("vitest") @scope("test")
75+
external test: (string, 'a) => unit = "only"
76+
77+
@module("vitest") @scope("it")
78+
external it: (string, 'a) => unit = "only"
79+
}
80+
81+
module Todo = {
82+
@module("vitest") @scope("describe")
83+
external describe: string => unit = "todo"
84+
85+
@module("vitest") @scope("bench")
86+
external bench: string => unit = "todo"
87+
88+
@module("vitest") @scope("test")
89+
external test: string => unit = "todo"
90+
91+
@module("vitest") @scope("it")
92+
external it: string => unit = "todo"
93+
}

vitest-bdd/src/index.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@ import { extname } from "node:path";
33
import { SourceMapGenerator } from "source-map";
44
import type { Plugin } from "vite";
55
import { parse, type SourceLocation } from "./parser";
6+
import { resCompile, resCompiledResolver } from "./resCompile";
67
export * from "./steps";
78

89
export type VitestBddOptions = {
910
debug?: boolean;
1011
markdownExtensions?: string[];
1112
gherkinExtensions?: string[];
13+
rescriptExtensions?: string[];
1214
stepsResolver?: (path: string) => string | null;
15+
resCompiledResolver?: (path: string) => string | null;
1316
};
1417

1518
const defaultOptions: Required<VitestBddOptions> = {
1619
debug: false,
1720
markdownExtensions: [".md", ".mdx", ".markdown"],
1821
gherkinExtensions: [".feature"],
22+
rescriptExtensions: [".res"],
1923
stepsResolver,
24+
resCompiledResolver,
2025
};
2126

2227
export function vitestBdd(opts: VitestBddOptions = {}): Plugin {
@@ -33,24 +38,25 @@ export function vitestBdd(opts: VitestBddOptions = {}): Plugin {
3338
return id;
3439
},
3540
load(id) {
36-
const ext = extname(id);
37-
const markdown = options.markdownExtensions.includes(ext);
38-
if (markdown || options.gherkinExtensions.includes(ext)) {
39-
return compile(id, markdown, options);
40-
}
41+
return compile(id, options);
4142
},
4243
};
4344
}
4445

45-
function compile(
46-
path: string,
47-
markdown: boolean,
48-
opts: Required<VitestBddOptions>
49-
) {
50-
const { debug } = opts;
46+
function compile(path: string, opts: Required<VitestBddOptions>) {
47+
const { debug, rescriptExtensions, markdownExtensions, gherkinExtensions } =
48+
opts;
49+
const ext = extname(path);
50+
if (rescriptExtensions.includes(ext)) {
51+
return resCompile(path, opts);
52+
}
53+
const isMarkdown = markdownExtensions.includes(ext);
54+
if (!gherkinExtensions.includes(ext) && !isMarkdown) {
55+
return;
56+
}
5157
const text = readFileSync(path, "utf8");
5258
const lines = text.split("\n");
53-
const feature = parse(text, markdown);
59+
const feature = parse(text, isMarkdown);
5460
if (!feature) {
5561
return `
5662
import { describe} from "vitest";

vitest-bdd/src/parser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
22
import { mdToGherkin, parse, parseStep } from "./parser";
33

44
describe("parse", () => {
5-
it("Should parse a simple feature", () => {
5+
it("Should parse a simple feature", (r) => {
66
const text = `Feature: Calculator
77
88
Scenario: Add two numbers

0 commit comments

Comments
 (0)