Skip to content

Commit 825e7be

Browse files
committed
Use log groups
Inspired by @j1000's Cypress Tips [1]. This fixes #796. [1] https://j1000.github.io/blog/2022/10/27/enhanced_cypress_logging.html
1 parent 78e48d0 commit 825e7be

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## Unreleased
6+
7+
- Log steps and commands using log groups, fixes [#796](https://github.com/badeball/cypress-cucumber-preprocessor/issues/796).
8+
59
## v15.0.0
610

711
- Drop support for Cypress v8.

lib/create-tests.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import { indent, stripIndent } from "./helpers/strings";
5050

5151
import { generateSnippet } from "./snippets";
5252

53+
import { runStepWithLogGroup } from "./cypress";
54+
5355
declare global {
5456
// eslint-disable-next-line @typescript-eslint/no-namespace
5557
namespace globalThis {
@@ -421,19 +423,6 @@ function createPickle(
421423
`Expected to find scenario step associated with id = ${pickleStep.astNodeIds?.[0]}`
422424
);
423425

424-
cy.then(() => {
425-
window.testState.pickleStep = step.pickleStep;
426-
427-
Cypress.log({
428-
name: "step",
429-
displayName: assertAndReturn(
430-
"keyword" in scenarioStep && scenarioStep.keyword,
431-
"Expected to find a keyword in the scenario step"
432-
),
433-
message: text,
434-
});
435-
});
436-
437426
const argument: DataTable | string | undefined = pickleStep.argument
438427
?.dataTable
439428
? new DataTable(pickleStep.argument.dataTable)
@@ -442,6 +431,8 @@ function createPickle(
442431
: undefined;
443432

444433
cy.then(() => {
434+
window.testState.pickleStep = step.pickleStep;
435+
445436
internalProperties.currentStep = { pickleStep };
446437

447438
const start = createTimestamp();
@@ -461,13 +452,15 @@ function createPickle(
461452
return cy.wrap(start, { log: false });
462453
})
463454
.then((start) => {
464-
const ensureChain = (value: unknown): Cypress.Chainable<unknown> =>
465-
Cypress.isCy(value) ? value : cy.wrap(value, { log: false });
466-
467455
try {
468-
return ensureChain(
469-
registry.runStepDefininition(this, text, argument)
470-
).then((result: unknown) => {
456+
return runStepWithLogGroup({
457+
keyword: assertAndReturn(
458+
"keyword" in scenarioStep && scenarioStep.keyword,
459+
"Expected to find a keyword in the scenario step"
460+
),
461+
text,
462+
fn: () => registry.runStepDefininition(this, text, argument),
463+
}).then((result) => {
471464
return {
472465
start,
473466
result,

lib/cypress.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const ensureChain = (value: unknown): Cypress.Chainable<unknown> =>
2+
Cypress.isCy(value) ? value : cy.wrap(value, { log: false });
3+
4+
export function runStepWithLogGroup(options: {
5+
fn: () => unknown;
6+
keyword?: string;
7+
text: string;
8+
}) {
9+
Cypress.log({
10+
name: options.keyword ?? "Step",
11+
message: options.text,
12+
groupStart: true,
13+
} as object);
14+
15+
return ensureChain(options.fn()).then((result) => {
16+
Cypress.log({ groupEnd: true, emitOnly: true } as object);
17+
return result;
18+
});
19+
}

lib/methods.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
} from "./constants";
1010
import { InternalSpecProperties } from "./create-tests";
1111

12+
import { runStepWithLogGroup } from "./cypress";
13+
1214
import DataTable from "./data_table";
1315

1416
import { getRegistry } from "./registry";
@@ -31,7 +33,11 @@ function runStepDefininition(
3133
text: string,
3234
argument?: DataTable | string
3335
) {
34-
getRegistry().runStepDefininition(world, text, argument);
36+
runStepWithLogGroup({
37+
keyword: "Step",
38+
text,
39+
fn: () => getRegistry().runStepDefininition(world, text, argument),
40+
});
3541
}
3642

3743
function defineParameterType<T, C extends Mocha.Context>(

0 commit comments

Comments
 (0)