Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
joelindridge-sh opened this issue Dec 17, 2024 · 6 comments
Labels
question Further information is requested

Comments

@joelindridge-sh
Copy link

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

@joelindridge-sh joelindridge-sh added the question Further information is requested label Dec 17, 2024
@vitalets
Copy link
Owner

vitalets commented Dec 17, 2024

Hi @joelindridge-sh

This way:

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

Will add it to docs.
Done.

@joelindridge-sh
Copy link
Author

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

@vitalets
Copy link
Owner

Confirm the bug, releasing as 8.0.1

@vitalets
Copy link
Owner

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);
});

@joelindridge-sh
Copy link
Author

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;
});

@vitalets
Copy link
Owner

vitalets commented Dec 20, 2024

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants