Skip to content

Commit db0fea3

Browse files
committed
Remove as unknown as QuickPickItem
This is definitely not a perfect solution since we're essentially just moving the place where we're casting. However, because we have manually made the types similar, this provides some type assurances where there were none before. This also has the cast in only one place, which makes it easier to find and fix in the future.
1 parent 56d283f commit db0fea3

File tree

6 files changed

+45
-33
lines changed

6 files changed

+45
-33
lines changed

extensions/ql-vscode/test/vscode-tests/cli-integration/packaging.test.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extensions, QuickPickItem, window } from "vscode";
1+
import { extensions, window } from "vscode";
22
import { join } from "path";
33

44
import { CodeQLCliServer } from "../../../src/cli";
@@ -10,6 +10,7 @@ import {
1010
handleDownloadPacks,
1111
handleInstallPackDependencies,
1212
} from "../../../src/packaging";
13+
import { mockedQuickPickItem } from "../utils/mocking.helpers";
1314

1415
// up to 3 minutes per test
1516
jest.setTimeout(3 * 60 * 1000);
@@ -56,7 +57,7 @@ describe("Packaging commands", () => {
5657

5758
it("should download all core query packs", async () => {
5859
quickPickSpy.mockResolvedValue(
59-
"Download all core query packs" as unknown as QuickPickItem,
60+
mockedQuickPickItem("Download all core query packs"),
6061
);
6162

6263
await handleDownloadPacks(cli, progress);
@@ -67,7 +68,7 @@ describe("Packaging commands", () => {
6768

6869
it("should download valid user-specified pack", async () => {
6970
quickPickSpy.mockResolvedValue(
70-
"Download custom specified pack" as unknown as QuickPickItem,
71+
mockedQuickPickItem("Download custom specified pack"),
7172
);
7273
inputBoxSpy.mockResolvedValue("codeql/csharp-solorigate-queries");
7374

@@ -79,7 +80,7 @@ describe("Packaging commands", () => {
7980

8081
it("should show error when downloading invalid user-specified pack", async () => {
8182
quickPickSpy.mockResolvedValue(
82-
"Download custom specified pack" as unknown as QuickPickItem,
83+
mockedQuickPickItem("Download custom specified pack"),
8384
);
8485
inputBoxSpy.mockResolvedValue("foo/[email protected]");
8586

@@ -93,12 +94,14 @@ describe("Packaging commands", () => {
9394

9495
it("should install valid workspace pack", async () => {
9596
const rootDir = join(__dirname, "./data");
96-
quickPickSpy.mockResolvedValue([
97-
{
98-
label: "integration-test-queries-javascript",
99-
packRootDir: [rootDir],
100-
},
101-
] as unknown as QuickPickItem);
97+
quickPickSpy.mockResolvedValue(
98+
mockedQuickPickItem([
99+
{
100+
label: "integration-test-queries-javascript",
101+
packRootDir: [rootDir],
102+
},
103+
]),
104+
);
102105

103106
await handleInstallPackDependencies(cli, progress);
104107
expect(showAndLogInformationMessageSpy).toHaveBeenCalledWith(
@@ -108,12 +111,14 @@ describe("Packaging commands", () => {
108111

109112
it("should throw an error when installing invalid workspace pack", async () => {
110113
const rootDir = join(__dirname, "../data-invalid-pack");
111-
quickPickSpy.mockResolvedValue([
112-
{
113-
label: "foo/bar",
114-
packRootDir: [rootDir],
115-
},
116-
] as unknown as QuickPickItem);
114+
quickPickSpy.mockResolvedValue(
115+
mockedQuickPickItem([
116+
{
117+
label: "foo/bar",
118+
packRootDir: [rootDir],
119+
},
120+
]),
121+
);
117122

118123
try {
119124
// expect this to throw an error

extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-manager.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
commands,
44
env,
55
extensions,
6-
QuickPickItem,
76
TextDocument,
87
TextEditor,
98
Uri,
@@ -59,7 +58,7 @@ import { DbManager } from "../../../../src/databases/db-manager";
5958
import { App } from "../../../../src/common/app";
6059
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
6160
import { DbConfigStore } from "../../../../src/databases/config/db-config-store";
62-
import { mockedObject } from "../../utils/mocking.helpers";
61+
import { mockedObject, mockedQuickPickItem } from "../../utils/mocking.helpers";
6362

6463
// up to 3 minutes per test
6564
jest.setTimeout(3 * 60 * 1000);
@@ -131,7 +130,7 @@ describe("Variant Analysis Manager", () => {
131130
beforeEach(async () => {
132131
jest
133132
.spyOn(window, "showQuickPick")
134-
.mockResolvedValueOnce("javascript" as unknown as QuickPickItem);
133+
.mockResolvedValueOnce(mockedQuickPickItem("javascript"));
135134

136135
cancellationTokenSource = new CancellationTokenSource();
137136

extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-submission-integration.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
authentication,
55
commands,
66
extensions,
7-
QuickPickItem,
87
TextDocument,
98
window,
109
workspace,
@@ -13,6 +12,7 @@ import {
1312
import { CodeQLExtensionInterface } from "../../../../src/extension";
1413
import { MockGitHubApiServer } from "../../../../src/mocks/mock-gh-api-server";
1514
import { setRemoteControllerRepo } from "../../../../src/config";
15+
import { mockedQuickPickItem } from "../../utils/mocking.helpers";
1616

1717
jest.setTimeout(30_000);
1818

@@ -71,9 +71,7 @@ describe("Variant Analysis Submission Integration", () => {
7171
await showQlDocument("query.ql");
7272

7373
// Select target language for your query
74-
quickPickSpy.mockResolvedValueOnce(
75-
"javascript" as unknown as QuickPickItem,
76-
);
74+
quickPickSpy.mockResolvedValueOnce(mockedQuickPickItem("javascript"));
7775

7876
await commands.executeCommand("codeQL.runVariantAnalysis");
7977

@@ -112,9 +110,7 @@ describe("Variant Analysis Submission Integration", () => {
112110
await showQlDocument("query.ql");
113111

114112
// Select target language for your query
115-
quickPickSpy.mockResolvedValueOnce(
116-
"javascript" as unknown as QuickPickItem,
117-
);
113+
quickPickSpy.mockResolvedValueOnce(mockedQuickPickItem("javascript"));
118114

119115
await commands.executeCommand("codeQL.runVariantAnalysis");
120116

extensions/ql-vscode/test/vscode-tests/no-workspace/databaseFetcher.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { join } from "path";
22
import { createFileSync, mkdirSync } from "fs-extra";
33
import * as tmp from "tmp";
4-
import { QuickPickItem, window } from "vscode";
4+
import { window } from "vscode";
55

66
import {
77
convertGithubNwoToDatabaseUrl,
88
findDirWithFile,
99
} from "../../../src/databaseFetcher";
1010
import * as Octokit from "@octokit/rest";
11+
import { mockedQuickPickItem } from "../utils/mocking.helpers";
1112

1213
// These tests make API calls and may need extra time to complete.
1314
jest.setTimeout(10000);
@@ -71,7 +72,7 @@ describe("databaseFetcher", () => {
7172
],
7273
};
7374
mockRequest.mockResolvedValue(mockApiResponse);
74-
quickPickSpy.mockResolvedValue("javascript" as unknown as QuickPickItem);
75+
quickPickSpy.mockResolvedValue(mockedQuickPickItem("javascript"));
7576
const githubRepo = "github/codeql";
7677
const result = await convertGithubNwoToDatabaseUrl(
7778
githubRepo,

extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import { createMockVariantAnalysisHistoryItem } from "../../../factories/query-h
2222
import { VariantAnalysisHistoryItem } from "../../../../src/query-history/variant-analysis-history-item";
2323
import { QueryStatus } from "../../../../src/query-status";
2424
import { VariantAnalysisStatus } from "../../../../src/variant-analysis/shared/variant-analysis";
25-
import { QuickPickItem, TextEditor } from "vscode";
25+
import { TextEditor } from "vscode";
2626
import { WebviewReveal } from "../../../../src/interface-utils";
2727
import * as helpers from "../../../../src/helpers";
28-
import { mockedObject } from "../../utils/mocking.helpers";
28+
import { mockedObject, mockedQuickPickItem } from "../../utils/mocking.helpers";
2929

3030
describe("QueryHistoryManager", () => {
3131
const mockExtensionLocation = join(tmpDir.name, "mock-extension-location");
@@ -977,9 +977,12 @@ describe("QueryHistoryManager", () => {
977977
it("should find the second query to compare when one is selected", async () => {
978978
const thisQuery = localQueryHistory[3];
979979
queryHistoryManager = await createMockQueryHistory(allHistory);
980-
showQuickPickSpy.mockResolvedValue({
981-
query: localQueryHistory[0],
982-
} as unknown as QuickPickItem);
980+
showQuickPickSpy.mockResolvedValue(
981+
mockedQuickPickItem({
982+
label: "Query 1",
983+
query: localQueryHistory[0],
984+
}),
985+
);
983986

984987
const otherQuery = await (
985988
queryHistoryManager as any

extensions/ql-vscode/test/vscode-tests/utils/mocking.helpers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { QuickPickItem, window } from "vscode";
2+
13
export type DeepPartial<T> = T extends object
24
? {
35
[P in keyof T]?: DeepPartial<T[P]>;
@@ -41,3 +43,9 @@ export function mockedObject<T extends object>(
4143
},
4244
});
4345
}
46+
47+
export function mockedQuickPickItem<T extends QuickPickItem | string>(
48+
value: T | T[],
49+
): Awaited<ReturnType<typeof window.showQuickPick>> {
50+
return value as Awaited<ReturnType<typeof window.showQuickPick>>;
51+
}

0 commit comments

Comments
 (0)