Skip to content

Commit 28d12e9

Browse files
maraisrlukeed
andauthored
feat: expose test/suite names in context object (#55)
* feat: also pass the name of the things to hooks * chore: updates to using an array * chore: use `crumbs` var name * fix(types): use `string[]` generic variant Co-authored-by: Marais Rossouw <me@marais.co> * stash * refactor: use `__suite__` and `__test__` context keys Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
1 parent 3d3076d commit 28d12e9

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare namespace uvu {
2-
type Callback<T> = (context: T) => Promise<void> | void;
2+
type Crumbs = { __suite__: string; __test__: string };
3+
type Callback<T> = (context: T & Crumbs) => Promise<void> | void;
34

45
interface Hook<T> {
56
(hook: Callback<T>): void;

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ async function runner(ctx, name) {
6666
for (hook of before) await hook(state);
6767

6868
for (test of arr) {
69+
state.__test__ = test.name;
6970
try {
7071
for (hook of bEach) await hook(state);
7172
await test.handler(state);
@@ -80,6 +81,7 @@ async function runner(ctx, name) {
8081
}
8182
}
8283
} finally {
84+
state.__test__ = '';
8385
for (hook of after) await hook(state);
8486
let msg = ` (${num} / ${total})\n`;
8587
let skipped = (only.length ? tests.length : 0) + ctx.skips;
@@ -89,6 +91,8 @@ async function runner(ctx, name) {
8991
}
9092

9193
function setup(ctx, name = '') {
94+
ctx.state.__test__ = '';
95+
ctx.state.__suite__ = name;
9296
const test = into(ctx, 'tests');
9397
test.before = hook(ctx, 'before');
9498
test.before.each = hook(ctx, 'bEach');

test/suite.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,41 @@ context3('should allow self-referencing instance(s) within context', ctx => {
282282
});
283283

284284
context3.run();
285+
286+
// ---
287+
288+
const breadcrumbs = suite('breadcrumbs', {
289+
count: 1,
290+
});
291+
292+
breadcrumbs.before(ctx => {
293+
assert.is(ctx.__suite__, 'breadcrumbs');
294+
assert.is(ctx.__test__, '');
295+
});
296+
297+
breadcrumbs.after(ctx => {
298+
assert.is(ctx.__suite__, 'breadcrumbs');
299+
assert.is(ctx.__test__, '');
300+
});
301+
302+
breadcrumbs.before.each(ctx => {
303+
assert.is(ctx.__suite__, 'breadcrumbs');
304+
assert.is(ctx.__test__, `test #${ctx.count}`);
305+
});
306+
307+
breadcrumbs.after.each(ctx => {
308+
assert.is(ctx.__suite__, 'breadcrumbs');
309+
assert.is(ctx.__test__, `test #${ctx.count++}`);
310+
});
311+
312+
breadcrumbs('test #1', (ctx) => {
313+
assert.is(ctx.__suite__, 'breadcrumbs');
314+
assert.is(ctx.__test__, 'test #1');
315+
});
316+
317+
breadcrumbs('test #2', (ctx) => {
318+
assert.is(ctx.__suite__, 'breadcrumbs');
319+
assert.is(ctx.__test__, 'test #2');
320+
});
321+
322+
breadcrumbs.run();

0 commit comments

Comments
 (0)