1

So I am using playwright/test with Typescript

I have a testapp.ts setup which contains all my methods eg for logging in, navigating etc Inside my test example.spec.ts each test looks like this:

  const testapp = new TestApp(page);
  await testapp.methodName();
});

I tried to put the const testapp = new TestApp(page); inside the beforeEach so I don't have to start every test putting in that line, but I get an error:

TypeError: Cannot read properties of undefined (reading 'testName')

jest lets you put the new TestApp(page) inside the beforeEach but not playwright/test

Is there any way to do this? Or Do I have to switch to jest?

Putting it in global-setup is not an option as I don't want to create a new browser page for each test my goal is to do end to end

Open one browser for each test suite and login, and then each test will execute on that session. Currently, it's working that way, except I have to put the new TestApp(page) inside every test

Martin H.
  • 538
  • 1
  • 7
  • 21
David
  • 21
  • 5
  • Did you ever solve this? It should work the same in jest, where you just have to declare the variable outside the beforeEach and test (`let testapp`), then assign it in beforeEach and can then use in test. Or [in Playwright you can use custom fixtures](https://playwright.dev/docs/test-fixtures). – David R Feb 12 '23 at 13:22

0 Answers0