Skip to content

Commit

Permalink
fix: ignore dash, underscore and space in search (#28012)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Schile <mschile@cypress.io>
Co-authored-by: Matt Schile <mschile@gmail.com>
  • Loading branch information
3 people committed Oct 23, 2023
1 parent 04a1156 commit 74a06c5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 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_
Expand Down
61 changes: 61 additions & 0 deletions packages/app/cypress/e2e/specs_list_e2e.cy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getPathForPlatform } from '../../src/paths'
import path from 'path'

describe('App: Spec List (E2E)', () => {
const launchApp = (specFilter?: string) => {
Expand Down Expand Up @@ -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'

Expand Down
15 changes: 13 additions & 2 deletions packages/app/src/specs/spec-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import _ from 'lodash'
import { FuzzyFoundSpec, getPlatform } from './tree/useCollapsibleTree'

export function fuzzySortSpecs <T extends FuzzyFoundSpec> (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

Expand All @@ -24,9 +24,20 @@ export function fuzzySortSpecs <T extends FuzzyFoundSpec> (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: [],
Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/specs/tree/useCollapsibleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type RawNode <T> = {
}

export type FuzzyFoundSpec<T = FoundSpec> = T & {
normalizedBaseName: string
normalizedRelative: string
fuzzyIndexes: {
relative: number[]
baseName: number[]
Expand Down

5 comments on commit 74a06c5

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 74a06c5 Oct 23, 2023

Choose a reason for hiding this comment

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

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.3.3/linux-arm64/develop-74a06c53b886e73f4943f381aefef3697fc1b551/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 74a06c5 Oct 23, 2023

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.3.3/linux-x64/develop-74a06c53b886e73f4943f381aefef3697fc1b551/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 74a06c5 Oct 23, 2023

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.3.3/darwin-x64/develop-74a06c53b886e73f4943f381aefef3697fc1b551/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 74a06c5 Oct 23, 2023

Choose a reason for hiding this comment

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

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.3.3/darwin-arm64/develop-74a06c53b886e73f4943f381aefef3697fc1b551/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 74a06c5 Oct 23, 2023

Choose a reason for hiding this comment

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

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.3.3/win32-x64/develop-74a06c53b886e73f4943f381aefef3697fc1b551/cypress.tgz

Please sign in to comment.