nano-service/tests/service.test.ts
qpismont d93c21d6aa
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
fix unit test
2024-01-10 22:28:27 +01:00

92 lines
2.4 KiB
TypeScript

import { z } from "zod";
import NatsAdaptor from "../src/adaptors/nats.ts";
import { Message } from "../src/messages.ts";
import Service from "../src/service.ts";
import { assertEquals, assertRejects, assertThrows } from "std/assert/mod.ts";
import { afterEach, beforeEach, it } from "std/testing/bdd.ts";
let srv!: Service;
const natsServer = Deno.env.get("NATS_HOST") || "127.0.0.1:4222";
const serviceName = "test-service";
const adaptorName = "nats";
const subject = "test";
beforeEach(() => {
srv = new Service(serviceName);
});
afterEach(async () => {
await srv.stop();
});
it("request success", {
sanitizeExit: false,
sanitizeOps: false,
sanitizeResources: false,
}, async () => {
const dataExpected = "hello success";
const statusCodeExpected = 200;
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
srv.subscribe(adaptorName, subject, async (msg) => {
return { data: msg.data, statusCode: statusCodeExpected };
}, z.string());
await srv.listen();
const res = await srv.request(adaptorName, {
service: serviceName,
subject: subject,
data: dataExpected,
}, z.string());
assertEquals(res.data, dataExpected);
assertEquals(res.statusCode, statusCodeExpected);
});
it("request error", {
sanitizeExit: false,
sanitizeOps: false,
sanitizeResources: false,
}, async () => {
const dataExpected = "hello error";
const statusCodeExpected = 500;
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
srv.subscribe(adaptorName, subject, async (msg) => {
return { data: msg.data, statusCode: statusCodeExpected };
}, z.string());
await srv.listen();
assertRejects(() => {
return srv.request(adaptorName, {
service: serviceName,
subject: subject,
data: dataExpected,
}, z.string());
});
});
it("request adaptor not found", {
sanitizeExit: false,
sanitizeOps: false,
sanitizeResources: false,
}, async () => {
const dataExpected = "hello success";
const statusCodeExpected = 200;
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
srv.subscribe(adaptorName, subject, async (msg) => {
return { data: msg.data, statusCode: statusCodeExpected };
});
await srv.listen();
assertRejects(() => {
return srv.request("bad adaptor", {
service: serviceName,
subject: subject,
data: dataExpected,
});
});
});