vitalets/playwright-bdd

Question: Can I use the default empty world when calling steps from steps?

Closed this issue · 6 comments

Hi @vitalets, thanks for this project, it's working really well.

I noticed a default empty world was implemented for pw-style steps: #208 (comment)

How can I make use of this default empty world when calling steps from steps?

(using very latest "playwright-bdd": "^8.0.0".)


In this example, Test1 passes, but Test2 fails.

test.feature:

Feature: Test

  Scenario: Test1
    When a
    Then b

  Scenario: Test2
    When c

test.ts:

import { createBdd } from "playwright-bdd";
import { expect } from "@playwright/test";

const { When, Then } = createBdd();

const a = When("a", async function ({}) {
  this.a = 1;
});

const b = Then("b", async function ({}) {
  expect(this.a).toBe(1);
});

When("c", async function ({}) {
  await a({});
  await b({});
});

image

Hi @joelindridge-sh

This way:

When("c", async function ({}) {
  await a.call(this);
  await b.call(this);
});

Will add it to docs.
Done.

Thanks @vitalets, but it seems this doesn't work in my playwright-style example. I guess this will only work with cucumber-style steps.

Confirm the bug, releasing as 8.0.1

Released in 8.0.1.
Now the following sample works:

const a = When("a", async function () {
  this.a = 1;
});

const b = Then("b", async function () {
  expect(this.a).toBe(1);
});

When("c", async function () {
  await a.call(this);
  await b.call(this);
});

Excellent, thank you @vitalets !

I managed to use it in the After block too:

After.call(this, async function () {
  // this.a
});

I think I can also define the default world type like this (I'm not sure if there's an easier way):

interface MyWorldType extends Record<string, any> {
  a: number;
}

const a = When("a", async function (this: MyWorldType) {
  this.a = 1;
});

I think I can also define the default world type like this (I'm not sure if there's an easier way):

If it contains only a, then it can be:

interface MyWorldType {
  a: number;
}