Skip to content

Commit 247ef58

Browse files
authored
fix(browser): make custom locators available in vitest-browser-* packages (#8103)
1 parent 33f7120 commit 247ef58

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

packages/browser/src/client/tester/context.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ export const locators: BrowserLocators = {
360360
extend(methods) {
361361
const Locator = page._createLocator('css=body').constructor as typeof LocatorAPI
362362
for (const method in methods) {
363+
locators._extendedMethods.add(method)
363364
const cb = (methods as any)[method] as (...args: any[]) => string | Locator
364365
// @ts-expect-error types are hard to make work
365366
Locator.prototype[method] = function (...args: any[]) {
@@ -378,11 +379,17 @@ export const locators: BrowserLocators = {
378379
}
379380
}
380381
},
382+
_extendedMethods: new Set<string>(),
381383
}
382384

383385
declare module '@vitest/browser/context' {
384386
interface BrowserPage {
385387
/** @internal */
386388
_createLocator: (selector: string) => Locator
387389
}
390+
391+
interface BrowserLocators {
392+
/** @internal */
393+
_extendedMethods: Set<string>
394+
}
388395
}

packages/browser/src/client/tester/public-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Locator, LocatorSelectors } from '@vitest/browser/context'
22
import type { StringifyOptions } from 'vitest/internal/browser'
3-
import { page } from '@vitest/browser/context'
3+
import { locators, page } from '@vitest/browser/context'
44
import { asLocator } from 'ivya'
55
import { stringify } from 'vitest/internal/browser'
66

@@ -14,6 +14,10 @@ export function getElementLocatorSelectors(element: Element): LocatorSelectors {
1414
getByTestId: testId => locator.getByTestId(testId),
1515
getByText: (text, options) => locator.getByText(text, options),
1616
getByTitle: (title, options) => locator.getByTitle(title, options),
17+
...Array.from(locators._extendedMethods).reduce((methods, method) => {
18+
methods[method] = (...args: any[]) => (locator as any)[method](...args)
19+
return methods
20+
}, {} as any),
1721
}
1822
}
1923

test/browser/fixtures/locators-custom/basic.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type Locator, locators, page } from '@vitest/browser/context';
22
import { beforeEach, expect, test } from 'vitest';
3+
import { getElementLocatorSelectors } from '@vitest/browser/utils'
34

45
declare module '@vitest/browser/context' {
56
interface LocatorSelectors {
@@ -84,3 +85,12 @@ test('new added method works on the page', async () => {
8485

8586
expect(document.body).toHaveTextContent('New Content')
8687
})
88+
89+
test('locators are available from getElementLocatorSelectors', () => {
90+
const locators = getElementLocatorSelectors(document.body)
91+
92+
expect(locators.updateHtml).toBeTypeOf('function')
93+
expect(locators.getByCustomTitle).toBeTypeOf('function')
94+
expect(locators.updateDocumentHtml).toBeTypeOf('function')
95+
expect(locators.getByNestedTitle).toBeTypeOf('function')
96+
})

test/browser/specs/locators.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test('custom locators work', async () => {
3434
})
3535

3636
const COUNT_TEST_FILES = 1
37-
const COUNT_TESTS_OVERALL = 4
37+
const COUNT_TESTS_OVERALL = 5
3838

3939
expect(stdout).toReportSummaryTestFiles({ passed: instances.length * COUNT_TEST_FILES })
4040
expect(stdout).toReportSummaryTests({ passed: instances.length * COUNT_TESTS_OVERALL })

0 commit comments

Comments
 (0)