Skip to content

Commit

Permalink
refactor mockserver to be file-specific, avoid context bleeding acros…
Browse files Browse the repository at this point in the history
…s tests
  • Loading branch information
Dtphelan1 committed May 31, 2024
1 parent 94424cd commit ab574a0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion test/download.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion test/kick-off.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 8 additions & 5 deletions test/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
25 changes: 17 additions & 8 deletions test/logging.test.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -49,6 +51,7 @@ describe("Logging", function () {
);

const { log } = await invoke({
mockServer,
options: { logResponseHeaders: [] },
});
console.log(log);
Expand Down Expand Up @@ -101,6 +104,7 @@ describe("Logging", function () {
);

const { log } = await invoke({
mockServer,
options: { logResponseHeaders: [] },
});
const entryStart = Utils.getLogEvent(log, "kickoff_start");
Expand Down Expand Up @@ -141,6 +145,7 @@ describe("Logging", function () {
);

const { log } = await invoke({
mockServer,
options: { logResponseHeaders: [] },
});
const entryStart = Utils.getLogEvent(log, "kickoff_start");
Expand All @@ -159,6 +164,7 @@ describe("Logging", function () {
);

const { log } = await invoke({
mockServer,
args: ["--count", "3"],
});

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -217,6 +224,7 @@ describe("Logging", function () {
);

const { log } = await invoke({
mockServer,
options: {
logResponseHeaders: ["x-debugging-header", "content-location"],
},
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -321,6 +329,7 @@ describe("Logging", function () {
});

const { log } = await invoke({
mockServer,
options: { logResponseHeaders: ["x-debugging-header"] },
});
const entry = Utils.getLogEvent(log, "status_error");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions test/status-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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({
Expand Down

0 comments on commit ab574a0

Please sign in to comment.