diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 83704f784c3c..79cfe638b208 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,4 +1,12 @@ +## 13.3.3 + +_Released 10/24/2023 (PENDING)_ + +**Bugfixes:** + +- Fixed a regression in [10.0.0](#10.0.0), where search would not find a spec if the file name contains "-" or "\_", but search prompt contains " " instead (e.g. search file "spec-file.cy.ts" with prompt "spec file"). Fixes [#25303](https://github.com/cypress-io/cypress/issues/25303). + ## 13.3.2 _Released 10/18/2023_ diff --git a/packages/app/cypress/e2e/specs_list_e2e.cy.ts b/packages/app/cypress/e2e/specs_list_e2e.cy.ts index 06a1ab33123d..38db09b577f0 100644 --- a/packages/app/cypress/e2e/specs_list_e2e.cy.ts +++ b/packages/app/cypress/e2e/specs_list_e2e.cy.ts @@ -1,4 +1,5 @@ import { getPathForPlatform } from '../../src/paths' +import path from 'path' describe('App: Spec List (E2E)', () => { const launchApp = (specFilter?: string) => { @@ -253,6 +254,66 @@ describe('App: Spec List (E2E)', () => { cy.findByText('No specs matched your search:').should('not.be.visible') }) + it('searches specs with "-" or "_" when search contains space', function () { + clearSearchAndType('accounts list') + + cy.findAllByTestId('spec-item') + .should('have.length', 1) + .and('contain', 'accounts_list.spec.js') + + cy.findByText('No specs matched your search:').should('not.be.visible') + }) + + it('searches specs with "-" or "_" when search contains "-"', function () { + clearSearchAndType('accounts-list') + + cy.findAllByTestId('spec-item') + .should('have.length', 1) + .and('contain', 'accounts_list.spec.js') + + cy.findByText('No specs matched your search:').should('not.be.visible') + }) + + it('searches specs with "-" or "_" when search contains "_"', function () { + clearSearchAndType('accounts_list') + + cy.findAllByTestId('spec-item') + .should('have.length', 1) + .and('contain', 'accounts_list.spec.js') + + cy.findByText('No specs matched your search:').should('not.be.visible') + }) + + it('searches folders with "-" or "_" when search contains space', function () { + clearSearchAndType('a b c') + + cy.findAllByTestId('spec-list-directory') + .should('have.length', 1) + .and('contain', path.join('cypress', 'e2e', 'a-b_c')) + + cy.findByText('No specs matched your search:').should('not.be.visible') + }) + + it('searches folders with "-" or "_" when search contains "-"', function () { + clearSearchAndType('a-b-c') + + cy.findAllByTestId('spec-list-directory') + .should('have.length', 1) + .and('contain', path.join('cypress', 'e2e', 'a-b_c')) + + cy.findByText('No specs matched your search:').should('not.be.visible') + }) + + it('searches folders with "-" or "_" when search contains "_"', function () { + clearSearchAndType('a_b_c') + + cy.findAllByTestId('spec-list-directory') + .should('have.length', 1) + .and('contain', path.join('cypress', 'e2e', 'a-b_c')) + + cy.findByText('No specs matched your search:').should('not.be.visible') + }) + it('saves the filter when navigating to a spec and back', function () { const targetSpecFile = 'accounts_list.spec.js' diff --git a/packages/app/src/specs/spec-utils.ts b/packages/app/src/specs/spec-utils.ts index 5e725ac13a6c..87364a9f1aaf 100644 --- a/packages/app/src/specs/spec-utils.ts +++ b/packages/app/src/specs/spec-utils.ts @@ -5,10 +5,10 @@ import _ from 'lodash' import { FuzzyFoundSpec, getPlatform } from './tree/useCollapsibleTree' export function fuzzySortSpecs (specs: T[], searchValue: string) { - const normalizedSearchValue = getPlatform() === 'win32' ? searchValue.replaceAll('/', '\\') : searchValue + const normalizedSearchValue = normalizeSpecValue(searchValue) const fuzzySortResult = fuzzySort - .go(normalizedSearchValue, specs, { keys: ['relative', 'baseName'], allowTypo: false, threshold: -3000 }) + .go(normalizedSearchValue, specs, { keys: ['normalizedRelative', 'normalizedBaseName'], allowTypo: false, threshold: -3000 }) .map((result) => { const [relative, baseName] = result @@ -24,9 +24,20 @@ export function fuzzySortSpecs (specs: T[], searchVal return fuzzySortResult } +function normalizeSpecValue (name: string) { + const escapedPath = getPlatform() === 'win32' ? name.replaceAll('/', '\\') : name + // replace dash, underscore and space with common character (in this case dash) + // they are replaced and not removed to preserve string length (so highlighting works correctly) + const normalizedSymbols = escapedPath.replace(/[-_\s]/g, '-') + + return normalizedSymbols +} + export function makeFuzzyFoundSpec (spec: FoundSpec): FuzzyFoundSpec { return { ...spec, + normalizedBaseName: normalizeSpecValue(spec.baseName), + normalizedRelative: normalizeSpecValue(spec.relative), fuzzyIndexes: { relative: [], baseName: [], diff --git a/packages/app/src/specs/tree/useCollapsibleTree.ts b/packages/app/src/specs/tree/useCollapsibleTree.ts index d15215753660..7c9062ec01c7 100644 --- a/packages/app/src/specs/tree/useCollapsibleTree.ts +++ b/packages/app/src/specs/tree/useCollapsibleTree.ts @@ -11,6 +11,8 @@ export type RawNode = { } export type FuzzyFoundSpec = T & { + normalizedBaseName: string + normalizedRelative: string fuzzyIndexes: { relative: number[] baseName: number[]