From 373e34df9e6375af779df1ea8a2084e6fe1081d7 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Fri, 22 May 2026 11:40:59 +0200 Subject: [PATCH 1/2] fix: generate `exact:` in `asLocator` only if it's different from the default --- src/locatorGenerators.ts | 9 +++--- test/spec.test.ts | 65 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/locatorGenerators.ts b/src/locatorGenerators.ts index 6643993..db9b666 100644 --- a/src/locatorGenerators.ts +++ b/src/locatorGenerators.ts @@ -24,6 +24,7 @@ import { parseSelector, stringifySelector, } from './selectorParser' +import { Ivya } from '.' export type Language = 'javascript' export type LocatorType = @@ -417,8 +418,8 @@ export class JavaScriptLocatorFactory implements LocatorFactory { attrs.push(`name: ${this.regexToSourceString(options.name)}`) } else if (typeof options.name === 'string') { attrs.push(`name: ${this.quote(options.name)}`) - if (options.exact) { - attrs.push(`exact: true`) + if (options.exact != null && Ivya.options.exact !== options.exact) { + attrs.push(`exact: ${options.exact}`) } } for (const { name, value } of options.attrs!) { @@ -478,8 +479,8 @@ export class JavaScriptLocatorFactory implements LocatorFactory { if (isRegExp(body)) { return `${method}(${this.regexToSourceString(body)})` } - return exact - ? `${method}(${this.quote(body)}, { exact: true })` + return exact != null && Ivya.options.exact !== exact + ? `${method}(${this.quote(body)}, { exact: ${exact} })` : `${method}(${this.quote(body)})` } diff --git a/test/spec.test.ts b/test/spec.test.ts index 9e29016..19aecd1 100644 --- a/test/spec.test.ts +++ b/test/spec.test.ts @@ -8,7 +8,12 @@ import { Ivya, asLocator, } from '../src' -import { expect, test } from 'vitest' +import { beforeEach, expect, test } from 'vitest' + +beforeEach(() => { + // the default + Ivya.options.exact = false +}) test('works correctly', () => { const button = document.createElement('button') @@ -48,6 +53,64 @@ test('file input', () => { ) }) +test.only('asLocator generates "exact" correctly dependning on the default', () => { + Ivya.options.exact = false + + expect(asLocator('javascript', getByTextSelector('Hello'))).toMatchInlineSnapshot( + `"getByText('Hello')"` + ) + expect( + asLocator('javascript', getByTextSelector('Hello', { exact: true })) + ).toMatchInlineSnapshot(`"getByText('Hello', { exact: true })"`) + expect( + asLocator('javascript', getByTextSelector('Hello', { exact: false })) + ).toMatchInlineSnapshot(`"getByText('Hello')"`) + + expect( + asLocator('javascript', getByRoleSelector('alert', { name: 'Hello' })) + ).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`) + expect( + asLocator( + 'javascript', + getByRoleSelector('alert', { name: 'Hello', exact: true }) + ) + ).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello', exact: true })"`) + expect( + asLocator( + 'javascript', + getByRoleSelector('alert', { name: 'Hello', exact: false }) + ) + ).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`) + + Ivya.options.exact = true + + expect(asLocator('javascript', getByTextSelector('Hello'))).toMatchInlineSnapshot( + `"getByText('Hello')"` + ) + expect( + asLocator('javascript', getByTextSelector('Hello', { exact: true })) + ).toMatchInlineSnapshot(`"getByText('Hello')"`) + expect( + asLocator('javascript', getByTextSelector('Hello', { exact: false })) + ).toMatchInlineSnapshot(`"getByText('Hello', { exact: false })"`) + + expect( + asLocator('javascript', getByRoleSelector('alert', { name: 'Hello' })) + ).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`) + expect( + asLocator( + 'javascript', + getByRoleSelector('alert', { name: 'Hello', exact: true }) + ) + ).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`) + expect( + asLocator( + 'javascript', + getByRoleSelector('alert', { name: 'Hello', exact: false }) + ) + ).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello', exact: false })"`) +}) + test('global exact option affects all selector helpers', () => { Ivya.options.exact = true From 9657fab53092aeec9325d8cf91c500340a7b9627 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Fri, 22 May 2026 12:38:52 +0200 Subject: [PATCH 2/2] fix: remove only --- test/spec.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec.test.ts b/test/spec.test.ts index 19aecd1..53fceda 100644 --- a/test/spec.test.ts +++ b/test/spec.test.ts @@ -53,7 +53,7 @@ test('file input', () => { ) }) -test.only('asLocator generates "exact" correctly dependning on the default', () => { +test('asLocator generates "exact" correctly dependning on the default', () => { Ivya.options.exact = false expect(asLocator('javascript', getByTextSelector('Hello'))).toMatchInlineSnapshot(