Skip to content

Commit 1c0d6b9

Browse files
committed
Enable restoreMocks Jest configuration option
This will ensure all mocks are restored after every test. This required a significant amount of changes in the tests since `jest.spyOn` now needs to be called in `beforeEach`, rather than in the `describe` block.
1 parent 68d867e commit 1c0d6b9

18 files changed

Lines changed: 358 additions & 318 deletions

extensions/ql-vscode/src/vscode-tests/cli-integration/databases.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ describe("Databases", () => {
2121
"https://lgtm.com/projects/g/aeisenberg/angular-bind-notifier/";
2222

2323
let databaseManager: DatabaseManager;
24-
const inputBoxStub = jest.spyOn(window, "showInputBox");
24+
let inputBoxStub: jest.SpiedFunction<typeof window.showInputBox>;
2525
let cli: CodeQLCliServer;
2626
const progressCallback = jest.fn();
2727

28-
jest.spyOn(window, "showErrorMessage").mockResolvedValue(undefined);
29-
jest.spyOn(window, "showInformationMessage").mockResolvedValue(undefined);
30-
3128
beforeEach(async () => {
32-
inputBoxStub.mockResolvedValue(undefined);
29+
inputBoxStub = jest
30+
.spyOn(window, "showInputBox")
31+
.mockResolvedValue(undefined);
32+
33+
jest.spyOn(window, "showErrorMessage").mockResolvedValue(undefined);
34+
jest.spyOn(window, "showInformationMessage").mockResolvedValue(undefined);
3335

3436
const extension = await extensions
3537
.getExtension<CodeQLExtensionInterface | Record<string, never>>(

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,28 @@ jest.setTimeout(3 * 60 * 1000);
1717
describe("Packaging commands", () => {
1818
let cli: CodeQLCliServer;
1919
const progress = jest.fn();
20-
const quickPickSpy = jest.spyOn(window, "showQuickPick");
21-
const inputBoxSpy = jest.spyOn(window, "showInputBox");
22-
const showAndLogErrorMessageSpy = jest.spyOn(
23-
helpers,
24-
"showAndLogErrorMessage",
25-
);
26-
const showAndLogInformationMessageSpy = jest.spyOn(
27-
helpers,
28-
"showAndLogInformationMessage",
29-
);
20+
let quickPickSpy: jest.SpiedFunction<typeof window.showQuickPick>;
21+
let inputBoxSpy: jest.SpiedFunction<typeof window.showInputBox>;
22+
let showAndLogErrorMessageSpy: jest.SpiedFunction<
23+
typeof helpers.showAndLogErrorMessage
24+
>;
25+
let showAndLogInformationMessageSpy: jest.SpiedFunction<
26+
typeof helpers.showAndLogInformationMessage
27+
>;
3028

3129
beforeEach(async () => {
32-
quickPickSpy.mockResolvedValue(undefined);
33-
inputBoxSpy.mockResolvedValue(undefined);
34-
showAndLogErrorMessageSpy.mockResolvedValue(undefined);
35-
showAndLogInformationMessageSpy.mockResolvedValue(undefined);
30+
quickPickSpy = jest
31+
.spyOn(window, "showQuickPick")
32+
.mockResolvedValue(undefined);
33+
inputBoxSpy = jest
34+
.spyOn(window, "showInputBox")
35+
.mockResolvedValue(undefined);
36+
showAndLogErrorMessageSpy = jest
37+
.spyOn(helpers, "showAndLogErrorMessage")
38+
.mockResolvedValue(undefined);
39+
showAndLogInformationMessageSpy = jest
40+
.spyOn(helpers, "showAndLogInformationMessage")
41+
.mockResolvedValue(undefined);
3642

3743
const extension = await extensions
3844
.getExtension<CodeQLExtensionInterface | Record<string, never>>(

extensions/ql-vscode/src/vscode-tests/cli-integration/remote-queries/remote-queries-manager.test.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ describe("Remote queries", () => {
4747
let cli: CodeQLCliServer;
4848
let cancellationTokenSource: CancellationTokenSource;
4949
const progress = jest.fn();
50-
const showQuickPickSpy = jest.spyOn(window, "showQuickPick");
51-
const getRepositoryFromNwoStub = jest.spyOn(
52-
ghApiClient,
53-
"getRepositoryFromNwo",
54-
);
50+
let showQuickPickSpy: jest.SpiedFunction<typeof window.showQuickPick>;
51+
let getRepositoryFromNwoStub: jest.SpiedFunction<
52+
typeof ghApiClient.getRepositoryFromNwo
53+
>;
5554
let ctx: ExtensionContext;
5655
let logger: any;
5756
let remoteQueriesManager: RemoteQueriesManager;
5857

5958
let originalDeps: Record<string, string> | undefined;
6059

6160
beforeEach(async () => {
61+
showQuickPickSpy = jest.spyOn(window, "showQuickPick");
62+
getRepositoryFromNwoStub = jest.spyOn(ghApiClient, "getRepositoryFromNwo");
63+
6264
const extension = await extensions
6365
.getExtension<CodeQLExtensionInterface | Record<string, never>>(
6466
"GitHub.vscode-codeql",
@@ -125,17 +127,20 @@ describe("Remote queries", () => {
125127
});
126128

127129
describe("runRemoteQuery", () => {
128-
const mockSubmitRemoteQueries = jest.spyOn(
129-
ghApiClient,
130-
"submitRemoteQueries",
131-
);
132-
const executeCommandSpy = jest.spyOn(commands, "executeCommand");
130+
let mockSubmitRemoteQueries: jest.SpiedFunction<
131+
typeof ghApiClient.submitRemoteQueries
132+
>;
133+
let executeCommandSpy: jest.SpiedFunction<typeof commands.executeCommand>;
133134

134135
beforeEach(() => {
135-
mockSubmitRemoteQueries.mockResolvedValue({
136-
workflow_run_id: 20,
137-
repositories_queried: ["octodemo/hello-world-1"],
138-
});
136+
mockSubmitRemoteQueries = jest
137+
.spyOn(ghApiClient, "submitRemoteQueries")
138+
.mockResolvedValue({
139+
workflow_run_id: 20,
140+
repositories_queried: ["octodemo/hello-world-1"],
141+
});
142+
143+
executeCommandSpy = jest.spyOn(commands, "executeCommand");
139144
});
140145

141146
it("should run a remote query that is part of a qlpack", async () => {

0 commit comments

Comments
 (0)