Need assistance for understanding code (TypeScript with Playwright and Axe-Core)

I’m new to TypeScript and working on a Playwright-Test-Project for Accessibility-Testing with the axe-core package.
It all works fine, but I dont get some parts of the code and I hate this feeling of not knowing what is actually going on.
So maybe you guys can help me 🙂

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
import { test as base } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';
type AxeFixture = {
makeAxeBuilder: () => AxeBuilder;
};
// Extend base test by providing "makeAxeBuilder"
//
// This new "test" can be used in multiple test files, and each of them will get
// a consistently configured AxeBuilder instance.
export const test = base.extend<AxeFixture>({
makeAxeBuilder: async ({ page }, use, testInfo) => {
const makeAxeBuilder = () => new AxeBuilder({ page })
.withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'])
.exclude('#commonly-reused-element-with-known-issue');
await use(makeAxeBuilder);
}
});
export { expect } from '@playwright/test';
</code>
<code> import { test as base } from '@playwright/test'; import AxeBuilder from '@axe-core/playwright'; type AxeFixture = { makeAxeBuilder: () => AxeBuilder; }; // Extend base test by providing "makeAxeBuilder" // // This new "test" can be used in multiple test files, and each of them will get // a consistently configured AxeBuilder instance. export const test = base.extend<AxeFixture>({ makeAxeBuilder: async ({ page }, use, testInfo) => { const makeAxeBuilder = () => new AxeBuilder({ page }) .withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa']) .exclude('#commonly-reused-element-with-known-issue'); await use(makeAxeBuilder); } }); export { expect } from '@playwright/test'; </code>

import { test as base } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';

type AxeFixture = {
  makeAxeBuilder: () => AxeBuilder;
};

// Extend base test by providing "makeAxeBuilder"
//
// This new "test" can be used in multiple test files, and each of them will get
// a consistently configured AxeBuilder instance.
export const test = base.extend<AxeFixture>({
  makeAxeBuilder: async ({ page }, use, testInfo) => {
    const makeAxeBuilder = () => new AxeBuilder({ page })
        .withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'])
        .exclude('#commonly-reused-element-with-known-issue');

    await use(makeAxeBuilder);
  }
});
export { expect } from '@playwright/test';

This is the code snipped from Playwright Fixtures I want to understand.

I’m coming from Java so I know the basics of OO-programming. And I think I got the concept of callback-functions (they play a big role in JS/TS If I got it right).

But I dont get this code. Maybe it’s a good idea if I give my explanation so you can correct me?
So let’s go 🙂

First we import some stuff from playwright.

Than we define a type ‘AxeFixture’. I explained this type-concept for me as something like a blueprint for an Object similiar as an Interface. This should contain a function that returns an AxeBuilder-Object.

Than we use an alias for the test-object (‘base’) to extend the actual test-object. The extend-method may accept a parameter of our defined AxeFixture-Type. Since our Type ‘AxeFixture’ requires the method ‘makeAxeBuilder’ we implement this now in: makeAxeBuilder: async ({ page }, use) => {...}
This method contains get 2 parameters: the page-object and a use-function.
On this point the naming is a bit confusing, since the required function and the variable have the same name: makeAxeBuilder.
Nevertheless, we now define the function makeAxeBuilder and than save the reference to the function in the variable makeAxeBuilder.

In the function body, we build a new AxeBuilder-Object and configure it with some WCAG-Tags. And finally we pass this object to the use-function (this does some magic?)

Overall, we extended the test-object of playwright with an function, that returns an AxeBuilder-Object that we can now use in our tests, if we call the function.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>test('MyTest', async ({page, makeAxeBuilder}) => {
await page.goto('/')
const axeBuilder = makeAxeBuilder()
...
}
</code>
<code>test('MyTest', async ({page, makeAxeBuilder}) => { await page.goto('/') const axeBuilder = makeAxeBuilder() ... } </code>
test('MyTest', async ({page, makeAxeBuilder}) => {
  await page.goto('/')
  const axeBuilder = makeAxeBuilder()

...
}

I would be really happy for feedback. As I already said: I’m still a learner but I want to understand the concepts. 🙂

Please see my details

New contributor

Max Mustermann is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật