@@ -16,6 +16,8 @@ import { SELECT_QUERY_NAME } from "../../../src/contextual/locationFinder";
1616import { QueryInProgress } from "../../../src/legacy-query-server/run-queries" ;
1717import { LegacyQueryRunner } from "../../../src/legacy-query-server/legacyRunner" ;
1818import { DatabaseItem } from "../../../src/local-databases" ;
19+ import { DeepPartial , mockedObject } from "../utils/mocking.helpers" ;
20+ import { BqrsKind } from "../../../src/pure/bqrs-cli-types" ;
1921
2022describe ( "run-queries" , ( ) => {
2123 let isCanarySpy : jest . SpiedFunction < typeof config . isCanary > ;
@@ -77,7 +79,7 @@ describe("run-queries", () => {
7779 ] ,
7880 bqrsDecode : [
7981 {
80- columns : [ { kind : "NotString" } , { kind : "String" } ] ,
82+ columns : [ { kind : "NotString" as BqrsKind } , { kind : "String" } ] ,
8183 tuples : [
8284 [ "a" , "b" ] ,
8385 [ "c" , "d" ] ,
@@ -89,8 +91,8 @@ describe("run-queries", () => {
8991 // this won't happen with the real CLI, but it's a good test
9092 columns : [
9193 { kind : "String" } ,
92- { kind : "NotString" } ,
93- { kind : "StillNotString" } ,
94+ { kind : "NotString" as BqrsKind } ,
95+ { kind : "StillNotString" as BqrsKind } ,
9496 ] ,
9597 tuples : [ [ "a" , "b" , "c" ] ] ,
9698 } ,
@@ -125,7 +127,7 @@ describe("run-queries", () => {
125127 ] ,
126128 bqrsDecode : [
127129 {
128- columns : [ { kind : "NotString" } , { kind : "String" } ] ,
130+ columns : [ { kind : "NotString" as BqrsKind } , { kind : "String" } ] ,
129131 // We only escape string columns. In practice, we will only see quotes in strings, but
130132 // it is a good test anyway.
131133 tuples : [
@@ -312,7 +314,7 @@ describe("run-queries", () => {
312314 function createMockQueryServerClient (
313315 cliServer ?: CodeQLCliServer ,
314316 ) : QueryServerClient {
315- return {
317+ return mockedObject < QueryServerClient > ( {
316318 config : {
317319 timeoutSecs : 5 ,
318320 } ,
@@ -326,20 +328,32 @@ describe("run-queries", () => {
326328 log : jest . fn ( ) ,
327329 } ,
328330 cliServer,
329- } as unknown as QueryServerClient ;
331+ } ) ;
330332 }
331333
334+ // A type that represents the mocked methods of a CodeQLCliServer. Exclude any non-methods.
335+ // This allows passing in an array of return values for a single method.
336+ type MockedCLIMethods = {
337+ [ K in keyof CodeQLCliServer ] : CodeQLCliServer [ K ] extends (
338+ ...args : any
339+ ) => any
340+ ? Array < DeepPartial < Awaited < ReturnType < CodeQLCliServer [ K ] > > > >
341+ : never ;
342+ } ;
343+
332344 function createMockCliServer (
333- mockOperations : Record < string , any [ ] > ,
345+ mockOperations : Partial < MockedCLIMethods > ,
334346 ) : CodeQLCliServer {
335- const mockServer : Record < string , any > = { } ;
347+ const mockedMethods : Record < string , jest . Mock > = { } ;
348+
336349 for ( const [ operation , returns ] of Object . entries ( mockOperations ) ) {
337- mockServer [ operation ] = jest . fn ( ) ;
338- returns . forEach ( ( returnValue ) => {
339- mockServer [ operation ] . mockResolvedValueOnce ( returnValue ) ;
350+ const fn = jest . fn ( ) ;
351+ returns . forEach ( ( returnValue : any ) => {
352+ fn . mockResolvedValueOnce ( returnValue ) ;
340353 } ) ;
354+ mockedMethods [ operation ] = fn ;
341355 }
342356
343- return mockServer as unknown as CodeQLCliServer ;
357+ return mockedObject < CodeQLCliServer > ( mockedMethods ) ;
344358 }
345359} ) ;
0 commit comments