← Back to Discord Forum

timeout on beforeEach error?

I have a beforeEach step where I'm throwing an error. (literally throw new Error("nope")). However, the playwright runner doesn't fail the step immediately. Instead it waits and times out after 30seconds.

const { describe, beforeEach } = test;

describe("Given I am signed in as Barb, from YourCompany", () => {

    beforeEach(async ({ page, context, sa }) => {

        throw new Error("nope");```

How do I get Playwright to fail the test immediately?

```2) [webkit] › super-admin.test.js:20:13 › Given I am signed in as Barb, from YourCompany › When I create a club › Then I can see the club listed in the admin view page 

    Test timeout of 30000ms exceeded while running "beforeEach" hook.

      15 |     describe("When I create a club", () => {
      16 |
    > 17 |         beforeEach(async ({ sa }) =>
         |         ^
      18 |             await sa.createClub({ name: "Long rods" }));
      19 |
      20 |         test("Then I can see the club listed in the admin view page", async ({ sa }) =>

        at .../test/e2e/tests/super-admin.test.js:17:9

    Error: nope

       7 |     beforeEach(async ({ page, context, sa }) => {
       8 |
    >  9 |         throw new Error("nope");
         |               ^
      10 |         await ensureDevMode({ page, context });
      11 |         await sa.asBarb();
      12 |

        at .../test/e2e/tests/super-admin.test.js:9:15

    Pending operations:
      - locator.click at fixtures/sa-fixture.js:28:67

This thread is trying to answer question "How can one make Playwright fail a test immediately when an error is thrown in a 'beforeEach' hook, instead of waiting for a timeout?"

2 replies

So, it turns out that even if the "outer" BeforeEach throws an error, it will still run the "inner" BeforeEach before failing. This means that the test can time out. Why would the inner BeforeEach continue to run if the outer one has failed????

I am also interested in understanding this behavior. I have some checks that I perform on the outer before each that if they fail the tests shouldn't even be started. @tinycode2 did you ever find out why this happens?

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.