Rayrun
← Back to Discord Forum

test.skip() ran in outer beforeEach() but still inner beforeEach() is executed. How to prevent this

I want to prevent the call to inner beforeEach from the outer beforeEach. Calling test.skip() does not seem to be working.

Need - I need to skip the test from outer beforeEach and also not call the inner beforeEach.

import { test } from '@playwright/test';

test.beforeEach(async () => {
  console.log("Outer beforeEach")
  test.skip();
});


test.describe('TD 1', () => {
  test.beforeEach(async ({ page }) => {
    console.log("Inner beforeEach")

    // The below line is the bug to avoid running
    await new Promise(r => setTimeout(r, 2000));
  });
  
  test('Test 1', async ({ page }) => {
    console.log("Test 1")
  });

});

The above snippet prints both Outer beforeEach and Inner beforeEach before skipping the test.

Why ? - the outer beforeEach will get a list of tests to skip. So trying to do that programatically ( without even beforeEach being executed )

Is there any way i can achieve this ? Any help is appreciated.

This thread is trying to answer question "How can I prevent the execution of inner beforeEach from the outer beforeEach using test.skip() in Playwright's testing library?"

0 replies

Answer

Sure, you can control the execution of an inner beforeEach from the outer beforeEach using conditional skipping in Playwright's testing library. Here's how you can do it:

First, import the necessary functions:

import { test } from '@playwright/test';

Next, define your outer beforeEach hook. Inside it, use test.skip() to conditionally skip all tests within it based on a condition:

test.beforeEach(async ({ page }) => {
  // Outer beforeEach logic here
  
  if (condition) {
    test.skip('Skipping all tests in this group');
  }
  
  // Rest of the code for outer beforeEach
});

By calling test.skip('Skipping all tests in this group'), all subsequent tests within this group will be skipped if the specified condition is met.

Now, define your inner beforeEach hook. You can perform any necessary setup or actions specific to that particular test case:

test.beforeEach(async ({ page }) => {
  // Inner beforeEach logic here
  
  // Rest of the code for inner beforeEach
});

Both hooks should be defined within a common scope such as a file or a describe block. This way, you can prevent the execution of an inner beforeEach from being affected by whether or not its corresponding outer beforeEach is skipped. This gives you more control over the execution of tests in Playwright's testing library.

Remember, the specific condition and implementation details may vary based on your use case.

Related Discord Threads

TwitterGitHubLinkedIn
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.