Skip to content

Commit

Permalink
better titles for mocha hooks (fixes #424, via #510)
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw committed Oct 28, 2022
1 parent 4122761 commit cbb3426
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/allure-mocha/src/AllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ export class AllureReporter {
public startHook(hook: Mocha.Hook): void {
const suite: AllureGroup | null = this.currentSuite;
const title = hook.title;

if (suite && title && title.includes("before")) {
this.currentExecutable = suite.addBefore();
} else if (suite && title && title.includes("after")) {
this.currentExecutable = suite.addAfter();
}

if (this.currentExecutable) {
this.currentExecutable.name = hook.originalTitle || hook.title;
this.currentExecutable.name = title;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/allure-mocha/src/MochaAllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ export class MochaAllureReporter extends Mocha.reporters.Base {
}

private onHookEnd(hook: Mocha.Hook): void {
this.coreReporter.endHook(hook.error());
this.coreReporter.endHook(hook?.error?.());
}
}
4 changes: 2 additions & 2 deletions packages/allure-mocha/test/specs/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class HooksSuite {

const [beforeHook] = group.befores;

expect(beforeHook.name).eq('"before each" hook: some beforeEach name');
expect(beforeHook.name).eq('"before each" hook: some beforeEach name for "named hooks test"');
}

@test
Expand All @@ -84,6 +84,6 @@ class HooksSuite {

const [afterEach] = group.afters;

expect(afterEach.name).eq('"after each" hook: some afterEach name');
expect(afterEach.name).eq('"after each" hook: some afterEach name for "named hooks test"');
}
}

0 comments on commit cbb3426

Please sign in to comment.