-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(plugin-typescript): add a timeout to the Algolia auto-types lookup #7206
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
Open
sebdanielsson
wants to merge
2
commits into
yarnpkg:master
Choose a base branch
from
sebdanielsson:claude/berry-7111-fix-8doaul
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| releases: | ||
| "@yarnpkg/core": patch | ||
| "@yarnpkg/plugin-typescript": patch | ||
|
|
||
| declined: | ||
| - "@yarnpkg/cli" | ||
| - "@yarnpkg/extensions" | ||
| - "@yarnpkg/plugin-catalog" | ||
| - "@yarnpkg/plugin-compat" | ||
| - "@yarnpkg/plugin-constraints" | ||
| - "@yarnpkg/plugin-dlx" | ||
| - "@yarnpkg/plugin-essentials" | ||
| - "@yarnpkg/plugin-exec" | ||
| - "@yarnpkg/plugin-file" | ||
| - "@yarnpkg/plugin-git" | ||
| - "@yarnpkg/plugin-github" | ||
| - "@yarnpkg/plugin-http" | ||
| - "@yarnpkg/plugin-init" | ||
| - "@yarnpkg/plugin-interactive-tools" | ||
| - "@yarnpkg/plugin-jsr" | ||
| - "@yarnpkg/plugin-link" | ||
| - "@yarnpkg/plugin-nm" | ||
| - "@yarnpkg/plugin-npm" | ||
| - "@yarnpkg/plugin-npm-cli" | ||
| - "@yarnpkg/plugin-pack" | ||
| - "@yarnpkg/plugin-patch" | ||
| - "@yarnpkg/plugin-pnp" | ||
| - "@yarnpkg/plugin-pnpm" | ||
| - "@yarnpkg/plugin-stage" | ||
| - "@yarnpkg/plugin-version" | ||
| - "@yarnpkg/plugin-workspace-tools" | ||
| - "@yarnpkg/builder" | ||
| - "@yarnpkg/doctor" | ||
| - "@yarnpkg/nm" | ||
| - "@yarnpkg/pnp" | ||
| - "@yarnpkg/pnpify" | ||
| - "@yarnpkg/sdks" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import {Configuration, Hooks, Plugin, httpUtils, structUtils} from '@yarnpkg/core'; | ||
| import {PortablePath} from '@yarnpkg/fslib'; | ||
|
|
||
| import {hasDefinitelyTyped} from '../sources/typescriptUtils'; | ||
| import plugin from '../sources'; | ||
|
|
||
| const requestMock = jest.fn<void, [AbortSignal]>(); | ||
|
|
||
| const descriptor = structUtils.makeDescriptor( | ||
| structUtils.makeIdent(null, `is-number`), | ||
| `unknown`, | ||
| ); | ||
|
|
||
| const makeConfiguration = (executeRequest: (signal: AbortSignal) => Promise<httpUtils.Response>) => { | ||
| const testPlugin: Plugin<Hooks> = { | ||
| hooks: { | ||
| wrapNetworkRequest: async (_executor, {signal}) => { | ||
| if (typeof signal === `undefined`) | ||
| throw new Error(`Expected the Algolia request to receive an abort signal`); | ||
|
|
||
| requestMock(signal); | ||
|
|
||
| return () => executeRequest(signal); | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| return Configuration.create(PortablePath.root, new Map<string, Plugin>([ | ||
| [`@yarnpkg/plugin-typescript`, plugin], | ||
| [`test-plugin`, testPlugin], | ||
| ])); | ||
| }; | ||
|
|
||
| const flushPromises = async () => { | ||
| for (let t = 0; t < 10; t++) { | ||
| await Promise.resolve(); | ||
| } | ||
| }; | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| jest.restoreAllMocks(); | ||
| requestMock.mockReset(); | ||
| }); | ||
|
|
||
| describe(`typescriptUtils`, () => { | ||
| describe(`hasDefinitelyTyped`, () => { | ||
| it(`aborts the Algolia request when the lookup times out`, async () => { | ||
| jest.useFakeTimers(); | ||
|
|
||
| const emitWarning = jest.spyOn(process, `emitWarning`).mockImplementation(() => {}); | ||
| const configuration = makeConfiguration(signal => { | ||
| return new Promise((_resolve, reject) => { | ||
| if (signal.aborted) { | ||
| reject(signal.reason); | ||
| } else { | ||
| signal.addEventListener(`abort`, () => { | ||
| reject(signal.reason); | ||
| }, {once: true}); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| const result = hasDefinitelyTyped(descriptor, configuration); | ||
|
|
||
| await flushPromises(); | ||
| expect(requestMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| jest.advanceTimersByTime(10_000); | ||
|
|
||
| await expect(result).resolves.toBe(false); | ||
| await flushPromises(); | ||
|
|
||
| expect(requestMock).toHaveBeenCalledTimes(1); | ||
| expect(requestMock.mock.calls[0][0].aborted).toBe(true); | ||
| expect(emitWarning).toHaveBeenCalledWith(expect.stringContaining(`Couldn't query Algolia's npm-search index`)); | ||
| }); | ||
|
|
||
| it(`warns and returns false when all Algolia hosts are unreachable`, async () => { | ||
| const emitWarning = jest.spyOn(process, `emitWarning`).mockImplementation(() => {}); | ||
| const configuration = makeConfiguration(async () => { | ||
| throw new Error(`Network unavailable`); | ||
| }); | ||
|
|
||
| await expect(hasDefinitelyTyped(descriptor, configuration)).resolves.toBe(false); | ||
|
|
||
| expect(requestMock).toHaveBeenCalledTimes(4); | ||
| expect(emitWarning).toHaveBeenCalledWith(expect.stringContaining(`Couldn't query Algolia's npm-search index`)); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.