Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/locator-description-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2e-dev/core': patch
---

A locator in a failure message, a step label, or a report names every option it was built with: `getByRole("heading", level: 2)`, `getByRole("checkbox", name: "Agree", checked: false)`, and `getByText("Pro", exact: false)`. Locators that differ only by level, a state, or `exact` used to print the same.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 · Non-blocking · The changeset says every option is named, but some options are not printed

Optional. This does not block the merge.

The release note says a locator "names every option it was built with". describeExpression does not print an explicit exact: true, exact: false on a regular expression, or visible: false. These options are defaults or have no effect, so the output is correct, but the release note claims more than the formatter does. For example, getByText(/pro/i, { exact: false }) prints getByText(/pro/i) on this head. The ## What section of the PR body uses the same wording.

7 changes: 6 additions & 1 deletion packages/core/src/locator/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,14 @@ export function describeExpression(expression: LocatorExpression): string {
const query = expression.query;
const value = describePattern(query.value);
const name = query.name === undefined ? '' : `, name: ${describePattern(query.name)}`;
// Every option a query was built with, so two locators that differ by one read apart in a failure.
const loose = [query.value, query.name].some((pattern) => pattern?.kind === 'string' && !pattern.exact);
const exact = loose ? ', exact: false' : '';
const level = query.level === undefined ? '' : `, level: ${query.level}`;
const states = Object.entries(query.states ?? {}).map(([state, on]) => `, ${state}: ${on}`).join('');
const visible = query.visible === true ? ', visible: true' : '';
const scope = expression.scope === undefined ? '' : `${describeExpression(expression.scope)} >> `;
return `${scope}getBy${query.kind[0]!.toUpperCase()}${query.kind.slice(1)}(${value}${name}${visible})`;
return `${scope}getBy${query.kind[0]!.toUpperCase()}${query.kind.slice(1)}(${value}${name}${exact}${level}${states}${visible})`;
}
case 'filter': {
const parts: string[] = [];
Expand Down
13 changes: 13 additions & 0 deletions packages/e2e/tests/unit/expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ describe('locator expressions', () => {
);
});

it('renders level, states, and exact: false, so locators that differ by one option read apart', () => {
expect(describeExpression(roleQuery('heading', { level: 2 }, undefined))).toBe('getByRole("heading", level: 2)');
expect(describeExpression(roleQuery('checkbox', { name: 'Agree', checked: false }, undefined))).toBe(
'getByRole("checkbox", name: "Agree", checked: false)',
);
expect(describeExpression(roleQuery('button', { name: 'Save', exact: false, pressed: true, disabled: false }, undefined))).toBe(
'getByRole("button", name: "Save", exact: false, disabled: false, pressed: true)',
);
expect(describeExpression(textQuery('text', 'Pro', { exact: false }, undefined))).toBe('getByText("Pro", exact: false)');
expect(describeExpression(textQuery('text', /pro/i, { exact: false }, undefined))).toBe('getByText(/pro/i)');
expect(describeExpression(roleQuery('button', { name: 'Save' }, undefined))).toBe('getByRole("button", name: "Save")');
});

it('scopes queries under a parent expression', () => {
const scope = testIdQuery('card', undefined, undefined);
const expression = textQuery('text', 'Pro', undefined, scope);
Expand Down
Loading