diff --git a/src/testing.ts b/src/testing.ts index 17211981a2123c05b666bf8d177fd5db7bc0f555..8a9a4f29fd98de05a10ee6431ba0621a97e9c2d7 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -3,29 +3,41 @@ import GrpcTestSuite from "./tests/grpcTests"; import { performance } from "perf_hooks"; import { EndpointReturn, TestResult } from "./types"; -import * as _ from 'lodash'; +import * as _ from "lodash"; -const runTest: (func: () => any, expected?: any) => Promise<TestResult> = async (func: () => Promise<EndpointReturn>, expected?: any) => { +const runTest: (func: () => any, expected?: any) => Promise<TestResult> = async ( + func: () => Promise<EndpointReturn>, + expected?: any, +) => { const start = performance.now(); const result = await func(); const end = performance.now(); - const pass = expected ? (_.isEqual(result.payload, expected) || _.differenceWith(expected, result.payload, _.isEqual).length == 0) && result.ok : result.ok; + const pass = expected + ? (_.isEqual(result?.payload, expected) || _.differenceWith(expected, result?.payload, _.isEqual).length == 0) && + result?.ok + : result?.ok; - let message = pass ? 'ok.' : `failed. + let message = pass + ? "ok." + : `failed. Expected: ${JSON.stringify(expected)} Received: - ${JSON.stringify(result.payload)}`; + ${JSON.stringify(result?.payload)}`; return { ok: pass, time: end - start, - message + message, }; -} +}; -export const avgRuntime: (func: () => any, iterations: number, expected?: any) => Promise<TestResult> = async (func: () => any, iterations: number, expected?: any) => { +export const avgRuntime: (func: () => any, iterations: number, expected?: any) => Promise<TestResult> = async ( + func: () => any, + iterations: number, + expected?: any, +) => { const promises = []; for (let i = 0; i < iterations; i++) { promises.push(runTest(func, expected)); @@ -33,13 +45,12 @@ export const avgRuntime: (func: () => any, iterations: number, expected?: any) = const results = await Promise.all(promises); return { - ok: results.reduce((acc, curr) => acc && curr.ok, true), - time: results.reduce((acc, curr) => acc + curr.time, 0) / iterations, - message: results[0].message, + ok: results?.reduce((acc, curr) => acc && curr.ok, true), + time: results?.reduce((acc, curr) => acc + curr.time, 0) / iterations, + message: results?.[0]?.message, }; }; - export async function runTests(awsUrl: string, resultCache: any, iterations: number, ws?: any) { const restTestSuite = new RestTestSuite(awsUrl); const grpcTestSuite = new GrpcTestSuite(awsUrl); @@ -51,6 +62,5 @@ export async function runTests(awsUrl: string, resultCache: any, iterations: num resultCache[awsUrl]["rest"] = await restTestSuite.runSuite(iterations); resultCache[awsUrl]["grpc"] = await grpcTestSuite.runSuite(iterations); - if (ws) - ws.send(JSON.stringify(resultCache[awsUrl])); + if (ws) ws.send(JSON.stringify(resultCache[awsUrl])); }