Skip to content

Commit 28a0075

Browse files
Move creation of the mock context to a function
1 parent 428014c commit 28a0075

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000;
2121

2222
describe("query history scrubber", () => {
2323
let deregister: vscode.Disposable | undefined;
24-
let mockCtx: vscode.ExtensionContext;
2524
let runCount = 0;
2625

2726
const tmpDir = dirSync({
@@ -35,24 +34,6 @@ describe("query history scrubber", () => {
3534
doNotFake: ["setTimeout"],
3635
now,
3736
});
38-
39-
mockCtx = {
40-
globalState: {
41-
lastScrubTime: now,
42-
get(key: string) {
43-
if (key !== "lastScrubTime") {
44-
throw new Error(`Unexpected key: ${key}`);
45-
}
46-
return this.lastScrubTime;
47-
},
48-
async update(key: string, value: any) {
49-
if (key !== "lastScrubTime") {
50-
throw new Error(`Unexpected key: ${key}`);
51-
}
52-
this.lastScrubTime = value;
53-
},
54-
},
55-
} as any as vscode.ExtensionContext;
5637
});
5738

5839
afterEach(() => {
@@ -63,7 +44,8 @@ describe("query history scrubber", () => {
6344
});
6445

6546
it("should not throw an error when the query directory does not exist", async () => {
66-
registerScrubber("idontexist");
47+
const mockCtx = createMockContext();
48+
registerScrubber("idontexist", mockCtx);
6749

6850
jest.advanceTimersByTime(ONE_HOUR_IN_MS);
6951
await wait();
@@ -97,7 +79,7 @@ describe("query history scrubber", () => {
9779
TWO_HOURS_IN_MS,
9880
THREE_HOURS_IN_MS,
9981
);
100-
registerScrubber(queryDir);
82+
registerScrubber(queryDir, createMockContext());
10183

10284
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
10385
await wait();
@@ -176,7 +158,27 @@ describe("query history scrubber", () => {
176158
return `query-${timestamp}`;
177159
}
178160

179-
function registerScrubber(dir: string) {
161+
function createMockContext(): vscode.ExtensionContext {
162+
return {
163+
globalState: {
164+
lastScrubTime: now,
165+
get(key: string) {
166+
if (key !== "lastScrubTime") {
167+
throw new Error(`Unexpected key: ${key}`);
168+
}
169+
return this.lastScrubTime;
170+
},
171+
async update(key: string, value: any) {
172+
if (key !== "lastScrubTime") {
173+
throw new Error(`Unexpected key: ${key}`);
174+
}
175+
this.lastScrubTime = value;
176+
},
177+
},
178+
} as any as vscode.ExtensionContext;
179+
}
180+
181+
function registerScrubber(dir: string, ctx: vscode.ExtensionContext) {
180182
deregister = registerQueryHistoryScrubber(
181183
ONE_HOUR_IN_MS,
182184
TWO_HOURS_IN_MS,
@@ -187,7 +189,7 @@ describe("query history scrubber", () => {
187189
return Promise.resolve();
188190
},
189191
}),
190-
mockCtx,
192+
ctx,
191193
{
192194
increment: () => runCount++,
193195
},

0 commit comments

Comments
 (0)