From ab574a08e7d5ca7e2abbfa8d345a6875fedbe183 Mon Sep 17 00:00:00 2001 From: Dylan Phelan Date: Fri, 31 May 2024 13:29:13 -0400 Subject: [PATCH] refactor mockserver to be file-specific, avoid context bleeding across tests --- package-lock.json | 3 +++ test/download.test.ts | 5 ++++- test/kick-off.test.ts | 3 ++- test/lib/index.ts | 13 ++++++++----- test/logging.test.ts | 25 +++++++++++++++++-------- test/status-check.test.ts | 7 ++++--- 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8ada8b8..3e9c82b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,9 @@ "prompt-sync": "^4.2.0", "winston": "^3.13.0" }, + "bin": { + "bulk-match-client": "build/app.js" + }, "devDependencies": { "@eslint/js": "^9.0.0", "@hapi/code": "^9.0.3", diff --git a/test/download.test.ts b/test/download.test.ts index f55706d..2784e8c 100644 --- a/test/download.test.ts +++ b/test/download.test.ts @@ -5,9 +5,11 @@ import { join } from "path"; import { BulkMatchClient as Types } from ".."; import { BulkMatchClient } from "../src/client"; import baseSettings from "../src/default-config"; -import { Utils, invoke, mockServer } from "./lib"; +import { Utils, invoke } from "./lib"; +import MockServer from "./lib/MockServer"; describe("download", function () { + const mockServer = new MockServer("MockServer", true); // Set longer timeout this.timeout(10000); @@ -105,6 +107,7 @@ describe("download", function () { mockServer.mock("/output/file_1.ndjson", mockResponse); await invoke({ + mockServer, options: { fhirUrl: mockServer.baseUrl, destination: join(__dirname, "./tmp/downloads"), diff --git a/test/kick-off.test.ts b/test/kick-off.test.ts index 21eab1b..8e4622b 100644 --- a/test/kick-off.test.ts +++ b/test/kick-off.test.ts @@ -2,9 +2,10 @@ import { expect } from "@hapi/code"; import { BulkMatchClient as Types } from ".."; import { BulkMatchClient } from "../src/client"; import baseSettings from "../src/default-config"; -import { mockServer } from "./lib"; +import MockServer from "./lib/MockServer"; describe("kick-off", function () { + const mockServer = new MockServer("MockServer", true); // Set longer timeout this.timeout(10000); diff --git a/test/lib/index.ts b/test/lib/index.ts index c7dc7c9..db591a2 100644 --- a/test/lib/index.ts +++ b/test/lib/index.ts @@ -3,17 +3,19 @@ import { readFileSync, writeFileSync } from "fs"; import { join } from "path"; import baseSettings from "../../config/template-config.js"; import { BulkMatchClient as types } from "../../index"; -import MockServer from "./MockServer"; +import MockServer from "./MockServer.js"; export * as Utils from "./utils"; -// Setup server for use by tests -export const mockServer = new MockServer("Mock Server", true); - /** * Invokes the client and replies with a promise that will resolve when the * download is complete */ interface InvokeArguments { + /** + * The server to use in executing out bulk match invocation + */ + mockServer: MockServer; + /** * Any custom options to pass */ @@ -35,11 +37,12 @@ interface InvokeArguments { stdio?: StdioOptions; } export async function invoke({ + mockServer, options = {}, args = [], timeout = 30000, stdio = "pipe", -}: InvokeArguments = {}): Promise<{ +}: InvokeArguments): Promise<{ config: types.NormalizedOptions; log: string; exitCode: number | null; diff --git a/test/logging.test.ts b/test/logging.test.ts index 7519731..e476fd5 100644 --- a/test/logging.test.ts +++ b/test/logging.test.ts @@ -1,9 +1,11 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { expect } from "@hapi/code"; import { existsSync, rmSync } from "fs"; -import { Utils, invoke, mockServer } from "./lib"; +import { Utils, invoke } from "./lib"; +import MockServer from "./lib/MockServer"; describe("Logging", function () { + const mockServer = new MockServer("MockServer", true); // Set longer timeout this.timeout(10000); @@ -49,6 +51,7 @@ describe("Logging", function () { ); const { log } = await invoke({ + mockServer, options: { logResponseHeaders: [] }, }); console.log(log); @@ -101,6 +104,7 @@ describe("Logging", function () { ); const { log } = await invoke({ + mockServer, options: { logResponseHeaders: [] }, }); const entryStart = Utils.getLogEvent(log, "kickoff_start"); @@ -141,6 +145,7 @@ describe("Logging", function () { ); const { log } = await invoke({ + mockServer, options: { logResponseHeaders: [] }, }); const entryStart = Utils.getLogEvent(log, "kickoff_start"); @@ -159,6 +164,7 @@ describe("Logging", function () { ); const { log } = await invoke({ + mockServer, args: ["--count", "3"], }); @@ -187,6 +193,7 @@ describe("Logging", function () { ); const { log } = await invoke({ + mockServer, options: { logResponseHeaders: "all" }, }); // Should be able to find debugging header in log entries @@ -217,6 +224,7 @@ describe("Logging", function () { ); const { log } = await invoke({ + mockServer, options: { logResponseHeaders: ["x-debugging-header", "content-location"], }, @@ -262,7 +270,7 @@ describe("Logging", function () { }, }); - const { log } = await invoke(); + const { log } = await invoke({ mockServer }); const logs = Utils.getLogEvents(log, "status_progress"); expect(logs.length, "must have 3 status_progress log entries").to.equal(3); @@ -291,7 +299,7 @@ describe("Logging", function () { headers: { "x-debugging-header": "someValue" }, }); - const { log } = await invoke(); + const { log } = await invoke({ mockServer }); const entry = Utils.getLogEvent(log, "status_error"); expect(entry).to.exist(); expect(entry.eventDetail.code).to.equal(404); @@ -321,6 +329,7 @@ describe("Logging", function () { }); const { log } = await invoke({ + mockServer, options: { logResponseHeaders: ["x-debugging-header"] }, }); const entry = Utils.getLogEvent(log, "status_error"); @@ -357,7 +366,7 @@ describe("Logging", function () { }, }); - const { log } = await invoke(); + const { log } = await invoke({ mockServer }); const entry = Utils.getLogEvent(log, "status_complete"); expect(entry).to.exist(); expect(entry.eventDetail.transactionTime).to.exist(); @@ -387,7 +396,7 @@ describe("Logging", function () { headers: { "content-type": "application/json" }, }); - const { log } = await invoke(); + const { log } = await invoke({ mockServer }); const entry = Utils.getLogEvent(log, "status_error"); expect(entry).to.exist(); expect(entry.eventDetail.code).to.equal(200); @@ -457,7 +466,7 @@ describe("Logging", function () { }, }); - const { log } = await invoke(); + const { log } = await invoke({ mockServer }); const start = Utils.getLogEvent(log, "kickoff_start"); expect(start).to.exist(); @@ -596,7 +605,7 @@ describe("Logging", function () { headers: { "x-debugging-header": "someValue" }, }); - const { log } = await invoke(); + const { log } = await invoke({ mockServer }); const entry = Utils.getLogEvent(log, "download_error"); expect(entry).to.exist(); expect(entry.eventDetail.fileUrl).to.equal( @@ -661,7 +670,7 @@ describe("Logging", function () { }, }); - const { log } = await invoke(); + const { log } = await invoke({ mockServer }); const entries = Utils.getLogEvents(log, "download_request"); expect( entries.length, diff --git a/test/status-check.test.ts b/test/status-check.test.ts index f119d3a..3d21d2b 100644 --- a/test/status-check.test.ts +++ b/test/status-check.test.ts @@ -3,9 +3,10 @@ import { BulkMatchClient as Types } from ".."; import { BulkMatchClient } from "../src/client"; import baseSettings from "../src/default-config"; import { RequestError } from "../src/lib/errors"; -import { mockServer } from "./lib"; - +import MockServer from "./lib/MockServer"; describe("status", function () { + const mockServer = new MockServer("MockServer", true); + // Set longer timeout this.timeout(10000); @@ -18,7 +19,7 @@ describe("status", function () { it("returns the manifest", async () => { mockServer.mock("/status", { status: 200, - headers: { "content-type": "application/json" }, + headers: { "Content-Type": "application/json" }, body: { output: [{}] }, }); const client = new BulkMatchClient({