test(harness): continue bounded risk-reduction with shared service harness
This commit is contained in:
@@ -1,116 +1,11 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const vm = require("vm");
|
||||
const assert = require("assert");
|
||||
|
||||
const rootDir = path.resolve(__dirname, "..", "..");
|
||||
const servicesDir = path.join(rootDir, "actions", "services");
|
||||
|
||||
const createAxiosMock = () => {
|
||||
const axios = (config) => axios.request(config);
|
||||
|
||||
axios.calls = [];
|
||||
axios.requestHandler = async () => {
|
||||
throw new Error("No axios.request handler configured");
|
||||
};
|
||||
axios.getHandler = async () => {
|
||||
throw new Error("No axios.get handler configured");
|
||||
};
|
||||
axios.postHandler = async () => {
|
||||
throw new Error("No axios.post handler configured");
|
||||
};
|
||||
|
||||
axios.request = (config) => {
|
||||
axios.calls.push({ type: "request", config });
|
||||
return axios.requestHandler(config);
|
||||
};
|
||||
|
||||
axios.get = (url, config) => {
|
||||
axios.calls.push({ type: "get", url, config });
|
||||
return axios.getHandler(url, config);
|
||||
};
|
||||
|
||||
axios.post = (url, data, config) => {
|
||||
axios.calls.push({ type: "post", url, data, config });
|
||||
return axios.postHandler(url, data, config);
|
||||
};
|
||||
|
||||
return axios;
|
||||
};
|
||||
|
||||
const createLoggerMock = () => {
|
||||
const calls = [];
|
||||
const consoleLogger = (error) => {
|
||||
calls.push(error);
|
||||
};
|
||||
return { consoleLogger, calls };
|
||||
};
|
||||
|
||||
const createAxiosError = (status = 500, statusText = "Server Error") => {
|
||||
return {
|
||||
response: {
|
||||
status,
|
||||
statusText,
|
||||
data: {}
|
||||
},
|
||||
config: {
|
||||
url: "/mock-url"
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const loadServiceModule = (fileName, injected = {}) => {
|
||||
const filePath = path.join(servicesDir, fileName);
|
||||
let source = fs.readFileSync(filePath, "utf8");
|
||||
|
||||
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
|
||||
|
||||
const exportNames = Array.from(
|
||||
source.matchAll(/export const\s+(\w+)\s*=/g)
|
||||
).map((match) => match[1]);
|
||||
|
||||
source = source.replace(/export const\s+/g, "const ");
|
||||
source += `\nmodule.exports = { ${exportNames.join(", ")} };\n`;
|
||||
|
||||
const defaultGetJson = (url, config) => {
|
||||
if (!injected.axios || !injected.axios.get) {
|
||||
throw new Error("Missing axios.get for default getJson mock");
|
||||
}
|
||||
|
||||
return injected.axios
|
||||
.get(url, config)
|
||||
.then((response) => response.data);
|
||||
};
|
||||
|
||||
const defaultRequestJson = (config) => {
|
||||
if (!injected.axios) {
|
||||
throw new Error("Missing axios for default requestJson mock");
|
||||
}
|
||||
|
||||
return injected.axios(config).then((response) => response.data);
|
||||
};
|
||||
|
||||
const context = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
require,
|
||||
URLSearchParams,
|
||||
encodeURIComponent,
|
||||
FormData: global.FormData,
|
||||
console: {
|
||||
log: () => {},
|
||||
info: () => {},
|
||||
warn: () => {},
|
||||
error: () => {}
|
||||
},
|
||||
getJson: injected.getJson || defaultGetJson,
|
||||
requestJson: injected.requestJson || defaultRequestJson,
|
||||
...injected
|
||||
};
|
||||
|
||||
vm.runInNewContext(source, context, { filename: filePath });
|
||||
return context.module.exports;
|
||||
};
|
||||
const {
|
||||
createAxiosMock,
|
||||
createLoggerMock,
|
||||
createAxiosError,
|
||||
loadServiceModule,
|
||||
normalize
|
||||
} = require("../serviceHarness.cjs");
|
||||
|
||||
const tests = [];
|
||||
|
||||
@@ -118,8 +13,6 @@ const test = (name, fn) => {
|
||||
tests.push({ name, fn });
|
||||
};
|
||||
|
||||
const normalize = (value) => JSON.parse(JSON.stringify(value));
|
||||
|
||||
test("search/getBasicSearch returns res.data on success", async () => {
|
||||
const axios = createAxiosMock();
|
||||
const logger = createLoggerMock();
|
||||
|
||||
Reference in New Issue
Block a user