Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fuzzy search #137

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
23 changes: 9 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
# [1.0.0](https://github.com/jest-community/jest-watch-typeahead/compare/v0.6.5...v1.0.0) (2021-09-29)


Copy link
Member

@SimenB SimenB Mar 22, 2022

Choose a reason for hiding this comment

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

changelog is autogenerated, so don't make any changes to it

Copy link
Author

Choose a reason for hiding this comment

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

oh okay, I'll add it to .prettierignore then

Copy link
Member

Choose a reason for hiding this comment

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

did so in 286aa14 🙂

### Bug Fixes

* add `exports` field to package.json ([#86](https://github.com/jest-community/jest-watch-typeahead/issues/86)) ([1bcd08f](https://github.com/jest-community/jest-watch-typeahead/commit/1bcd08fdc316fdb63b2665aac2b100222fcf2132))
* drop support for EOL versions of node ([#85](https://github.com/jest-community/jest-watch-typeahead/issues/85)) ([7351933](https://github.com/jest-community/jest-watch-typeahead/commit/73519337607032704ed67b3cf006d236256d1844))

- add `exports` field to package.json ([#86](https://github.com/jest-community/jest-watch-typeahead/issues/86)) ([1bcd08f](https://github.com/jest-community/jest-watch-typeahead/commit/1bcd08fdc316fdb63b2665aac2b100222fcf2132))
- drop support for EOL versions of node ([#85](https://github.com/jest-community/jest-watch-typeahead/issues/85)) ([7351933](https://github.com/jest-community/jest-watch-typeahead/commit/73519337607032704ed67b3cf006d236256d1844))

### Features

* migrate to native ESM ([#84](https://github.com/jest-community/jest-watch-typeahead/issues/84)) ([a8e6940](https://github.com/jest-community/jest-watch-typeahead/commit/a8e6940851e7eefe16b69113fb29c6bd36916bb8))

- migrate to native ESM ([#84](https://github.com/jest-community/jest-watch-typeahead/issues/84)) ([a8e6940](https://github.com/jest-community/jest-watch-typeahead/commit/a8e6940851e7eefe16b69113fb29c6bd36916bb8))

### BREAKING CHANGES

* Drop support for Jest v26
* Module is now written in native ESM
* Disallow importing internal files
* Supported version range of node is now ^12.22.0 || ^14.17.0 || >=16.0.0
- Drop support for Jest v26
- Module is now written in native ESM
- Disallow importing internal files
- Supported version range of node is now ^12.22.0 || ^14.17.0 || >=16.0.0

## [0.6.5](https://github.com/jest-community/jest-watch-typeahead/compare/v0.6.4...v0.6.5) (2021-09-28)


### Bug Fixes

* remove test files from published module ([db522ac](https://github.com/jest-community/jest-watch-typeahead/commit/db522ac9d1623952bbd3b8b992d583d0cb3cf1e8))
- remove test files from published module ([db522ac](https://github.com/jest-community/jest-watch-typeahead/commit/db522ac9d1623952bbd3b8b992d583d0cb3cf1e8))

## [0.6.4](https://github.com/jest-community/jest-watch-typeahead/compare/v0.6.3...v0.6.4) (2021-05-27)


### Bug Fixes

* **deps:** update jest peer dependency to accept v27 ([#59](https://github.com/jest-community/jest-watch-typeahead/issues/59)) ([6552ead](https://github.com/jest-community/jest-watch-typeahead/commit/6552ead2dafb0258d11783f3c9e3e12877b15226))
- **deps:** update jest peer dependency to accept v27 ([#59](https://github.com/jest-community/jest-watch-typeahead/issues/59)) ([6552ead](https://github.com/jest-community/jest-watch-typeahead/commit/6552ead2dafb0258d11783f3c9e3e12877b15226))

## 0.6.2

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"ansi-escapes": "^4.3.1",
"chalk": "^4.0.0",
"fuzzysearch": "^1.0.3",
"jest-regex-util": "^27.0.0",
"jest-watcher": "^27.0.0",
"slash": "^4.0.0",
Expand Down
46 changes: 46 additions & 0 deletions src/file_name_plugin/__tests__/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`can fuzzy search 1`] = `

[MOCK - cursorLeft]

pattern › f
[MOCK - cursorSavePosition]
[MOCK - cursorLeft]


Pattern matches 3 files

› src/foo.js

› src/file-1.js

› src/file-2.js
[MOCK - cursorTo(12, 5)]
[MOCK - cursorRestorePosition]

[MOCK - cursorLeft]

pattern › f1
[MOCK - cursorSavePosition]
[MOCK - cursorLeft]


Pattern matches 1 file

› src/file-1.js
[MOCK - cursorTo(13, 5)]
[MOCK - cursorRestorePosition]

[MOCK - cursorLeft]

pattern › f1
[MOCK - cursorSavePosition]
[MOCK - cursorLeft]


Pattern matches 1 file

› src/file-1.js
[MOCK - cursorTo(13, 5)]
[MOCK - cursorRestorePosition]
`;

exports[`can select a pattern that matches multiple files 1`] = `

[MOCK - cursorLeft]
Expand Down
20 changes: 19 additions & 1 deletion src/file_name_plugin/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ it('can select a pattern that matches multiple files', async () => {

expect(updateConfigAndRun).toHaveBeenCalledWith({
mode: 'watch',
testPathPattern: 'fi',
testPathPattern: 'f.*i',
});
});

Expand Down Expand Up @@ -179,3 +179,21 @@ it("selected file doesn't include trimming dots", async () => {
testPathPattern: 'ong_name_gonna_need_trimming\\.js',
});
});

it('can fuzzy search', async () => {
const { stdout, hookEmitter, updateConfigAndRun, plugin, type } =
pluginTester(FileNamePlugin);

hookEmitter.onFileChange({ projects });
const runPromise = plugin.run({}, updateConfigAndRun);
stdout.write.mockReset();
type('f', '1', KEYS.ARROW_DOWN, KEYS.ENTER);
expect(stdout.write.mock.calls.join('\n')).toMatchSnapshot();

await runPromise;

expect(updateConfigAndRun).toHaveBeenCalledWith({
mode: 'watch',
testPathPattern: 'src/file-1\\.js',
});
});
15 changes: 5 additions & 10 deletions src/file_name_plugin/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
printRestoredPatternCaret,
} from 'jest-watcher';
import { escapeStrForRegex } from 'jest-regex-util';
import fuzzysearch from 'fuzzysearch';
import type { Config } from '@jest/types';
import {
highlight,
Expand Down Expand Up @@ -91,14 +92,6 @@ export default class FileNamePatternPrompt extends PatternPrompt {
path: string;
context: { config: Config.ProjectConfig };
}> {
let regex: RegExp;

try {
regex = new RegExp(pattern, 'i');
} catch (e) {
return [];
}

return this._searchSources.reduce<
Array<{
path: string;
Expand All @@ -107,7 +100,9 @@ export default class FileNamePatternPrompt extends PatternPrompt {
>((tests, { testPaths, config }) => {
return tests.concat(
testPaths
.filter((testPath) => regex.test(testPath))
.filter((testPath) =>
fuzzysearch(pattern.toLowerCase(), testPath.toLowerCase()),
)
.map((path) => ({
path,
context: { config },
Expand All @@ -130,7 +125,7 @@ export default class FileNamePatternPrompt extends PatternPrompt {
super.run(
(value) => {
onSuccess(
removeTrimmingDots(value).split('/').map(escapeStrForRegex).join('/'),
removeTrimmingDots(value).split('').map(escapeStrForRegex).join('.*'),
);
},
onCancel,
Expand Down
6 changes: 6 additions & 0 deletions src/fuzzysearch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** Declaration file generated by dts-gen */
declare module 'fuzzysearch' {
function fuzzysearch(needle: string, haystack: string): boolean;

export = fuzzysearch;
}
145 changes: 75 additions & 70 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,11 @@ functional-red-black-tree@^1.0.1:
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=

fuzzysearch@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/fuzzysearch/-/fuzzysearch-1.0.3.tgz#dffc80f6d6b04223f2226aa79dd194231096d008"
integrity sha1-3/yA9tawQiPyImqnndGUIxCW0Ag=

gauge@^4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/gauge/-/gauge-4.0.3.tgz#286cf105c1962c659f0963058fb05116c1b82d3f"
Expand Down Expand Up @@ -5164,76 +5169,76 @@ npm@^7.0.0:
resolved "https://registry.yarnpkg.com/npm/-/npm-7.24.2.tgz#861117af8241bea592289f22407230e5300e59ca"
integrity sha512-120p116CE8VMMZ+hk8IAb1inCPk4Dj3VZw29/n2g6UI77urJKVYb7FZUDW8hY+EBnfsjI/2yrobBgFyzo7YpVQ==
dependencies:
"@isaacs/string-locale-compare" "*"
"@npmcli/arborist" "*"
"@npmcli/ci-detect" "*"
"@npmcli/config" "*"
"@npmcli/map-workspaces" "*"
"@npmcli/package-json" "*"
"@npmcli/run-script" "*"
abbrev "*"
ansicolors "*"
ansistyles "*"
archy "*"
cacache "*"
chalk "*"
chownr "*"
cli-columns "*"
cli-table3 "*"
columnify "*"
fastest-levenshtein "*"
glob "*"
graceful-fs "*"
hosted-git-info "*"
ini "*"
init-package-json "*"
is-cidr "*"
json-parse-even-better-errors "*"
libnpmaccess "*"
libnpmdiff "*"
libnpmexec "*"
libnpmfund "*"
libnpmhook "*"
libnpmorg "*"
libnpmpack "*"
libnpmpublish "*"
libnpmsearch "*"
libnpmteam "*"
libnpmversion "*"
make-fetch-happen "*"
minipass "*"
minipass-pipeline "*"
mkdirp "*"
mkdirp-infer-owner "*"
ms "*"
node-gyp "*"
nopt "*"
npm-audit-report "*"
npm-install-checks "*"
npm-package-arg "*"
npm-pick-manifest "*"
npm-profile "*"
npm-registry-fetch "*"
npm-user-validate "*"
npmlog "*"
opener "*"
pacote "*"
parse-conflict-json "*"
qrcode-terminal "*"
read "*"
read-package-json "*"
read-package-json-fast "*"
readdir-scoped-modules "*"
rimraf "*"
semver "*"
ssri "*"
tar "*"
text-table "*"
tiny-relative-date "*"
treeverse "*"
validate-npm-package-name "*"
which "*"
write-file-atomic "*"
"@isaacs/string-locale-compare" "^1.1.0"
"@npmcli/arborist" "^2.9.0"
"@npmcli/ci-detect" "^1.2.0"
"@npmcli/config" "^2.3.0"
"@npmcli/map-workspaces" "^1.0.4"
"@npmcli/package-json" "^1.0.1"
"@npmcli/run-script" "^1.8.6"
abbrev "~1.1.1"
ansicolors "~0.3.2"
ansistyles "~0.1.3"
archy "~1.0.0"
cacache "^15.3.0"
chalk "^4.1.2"
chownr "^2.0.0"
cli-columns "^3.1.2"
cli-table3 "^0.6.0"
columnify "~1.5.4"
fastest-levenshtein "^1.0.12"
glob "^7.2.0"
graceful-fs "^4.2.8"
hosted-git-info "^4.0.2"
ini "^2.0.0"
init-package-json "^2.0.5"
is-cidr "^4.0.2"
json-parse-even-better-errors "^2.3.1"
libnpmaccess "^4.0.2"
libnpmdiff "^2.0.4"
libnpmexec "^2.0.1"
libnpmfund "^1.1.0"
libnpmhook "^6.0.2"
libnpmorg "^2.0.2"
libnpmpack "^2.0.1"
libnpmpublish "^4.0.1"
libnpmsearch "^3.1.1"
libnpmteam "^2.0.3"
libnpmversion "^1.2.1"
make-fetch-happen "^9.1.0"
minipass "^3.1.3"
minipass-pipeline "^1.2.4"
mkdirp "^1.0.4"
mkdirp-infer-owner "^2.0.0"
ms "^2.1.2"
node-gyp "^7.1.2"
nopt "^5.0.0"
npm-audit-report "^2.1.5"
npm-install-checks "^4.0.0"
npm-package-arg "^8.1.5"
npm-pick-manifest "^6.1.1"
npm-profile "^5.0.3"
npm-registry-fetch "^11.0.0"
npm-user-validate "^1.0.1"
npmlog "^5.0.1"
opener "^1.5.2"
pacote "^11.3.5"
parse-conflict-json "^1.1.1"
qrcode-terminal "^0.12.0"
read "~1.0.7"
read-package-json "^4.1.1"
read-package-json-fast "^2.0.3"
readdir-scoped-modules "^1.1.0"
rimraf "^3.0.2"
semver "^7.3.5"
ssri "^8.0.1"
tar "^6.1.11"
text-table "~0.2.0"
tiny-relative-date "^1.3.0"
treeverse "^1.0.4"
validate-npm-package-name "~3.0.0"
which "^2.0.2"
write-file-atomic "^3.0.3"

npmlog@*, npmlog@^6.0.0, npmlog@^6.0.1:
version "6.0.1"
Expand Down