Skip to content

Commit

Permalink
test most options
Browse files Browse the repository at this point in the history
fixes #6
  • Loading branch information
saiichihashimoto committed Jan 15, 2024
1 parent 161c3f0 commit 79f22ae
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 34 deletions.
169 changes: 136 additions & 33 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,7 @@ describe("main", () => {

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-api",
expect.objectContaining({
method: "GET",
redirect: "manual",
headers: {},
})
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
Expand Down Expand Up @@ -284,11 +280,7 @@ describe("main", () => {

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-api",
expect.objectContaining({
method: "GET",
redirect: "manual",
headers: {},
})
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
Expand All @@ -312,11 +304,7 @@ describe("main", () => {

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-other-api",
expect.objectContaining({
method: "GET",
redirect: "manual",
headers: {},
})
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
Expand All @@ -340,11 +328,7 @@ describe("main", () => {

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-api",
expect.objectContaining({
method: "GET",
redirect: "manual",
headers: {},
})
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
Expand Down Expand Up @@ -494,12 +478,139 @@ describe("main", () => {

await jest.advanceTimersByTimeAsync(1000);

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-api",
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
currentRun: "2023-10-05T06:14:02.000Z",
level: 30,
msg: "Started /some-api Every second",
time: 1696486442000,
},
{
currentRun: "2023-10-05T06:14:02.000Z",
level: 30,
msg: "Succeeded /some-api Every second",
status: 200,
text: "Some Text",
time: 1696486442000,
},
]);
});

it("uses specified config", async () => {
const { fs } = memfs(
{
"./vercel-other.json": JSON.stringify({
crons: [{ path: "/some-api", schedule: "* * * * * *" }],
}),
},
process.cwd()
);

proc = main({
destination,
fs,
signal: controller.signal,
config: "./vercel-other.json",
});

await jest.advanceTimersByTimeAsync(0);
destination.clear();
await jest.advanceTimersByTimeAsync(1000);

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-api",
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
currentRun: "2023-10-05T06:14:02.000Z",
level: 30,
msg: "Started /some-api Every second",
time: 1696486442000,
},
{
currentRun: "2023-10-05T06:14:02.000Z",
level: 30,
msg: "Succeeded /some-api Every second",
status: 200,
text: "Some Text",
time: 1696486442000,
},
]);
});

it("uses specified url", async () => {
const { fs } = memfs(
{
"./vercel.json": JSON.stringify({
crons: [{ path: "/some-api", schedule: "* * * * * *" }],
}),
},
process.cwd()
);

proc = main({
destination,
fs,
signal: controller.signal,
url: "https://my-website.com",
});

await jest.advanceTimersByTimeAsync(0);
destination.clear();
await jest.advanceTimersByTimeAsync(1000);

expect(fetchSpy).toHaveBeenCalledWith(
"https://my-website.com/some-api",
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
currentRun: "2023-10-05T06:14:02.000Z",
level: 30,
msg: "Started /some-api Every second",
time: 1696486442000,
},
{
currentRun: "2023-10-05T06:14:02.000Z",
level: 30,
msg: "Succeeded /some-api Every second",
status: 200,
text: "Some Text",
time: 1696486442000,
},
]);
});

it("uses specified secret", async () => {
const { fs } = memfs(
{
"./vercel.json": JSON.stringify({
crons: [{ path: "/some-api", schedule: "* * * * * *" }],
}),
},
process.cwd()
);

proc = main({
destination,
fs,
signal: controller.signal,
secret: "mock-secret",
});

await jest.advanceTimersByTimeAsync(0);
destination.clear();
await jest.advanceTimersByTimeAsync(1000);

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-api",
expect.objectContaining({
method: "GET",
redirect: "manual",
headers: {},
headers: { Authorization: "Bearer mock-secret" },
})
);
expect(destination.logs).toStrictEqual([
Expand Down Expand Up @@ -552,11 +663,7 @@ describe("main", () => {

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-api",
expect.objectContaining({
method: "GET",
redirect: "manual",
headers: {},
})
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
Expand Down Expand Up @@ -601,11 +708,7 @@ describe("main", () => {

expect(fetchSpy).toHaveBeenCalledWith(
"http://localhost:3000/some-api",
expect.objectContaining({
method: "GET",
redirect: "manual",
headers: {},
})
expect.objectContaining({})
);
expect(destination.logs).toStrictEqual([
{
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Cron } from "croner";
import cronstrue from "cronstrue";
import { debounce } from "lodash/fp";
import pino from "pino";
import type { LoggerOptions } from "pino";
import z from "zod";

import { anySignal } from "./utils";
Expand Down Expand Up @@ -88,7 +89,7 @@ export const main = async ({
},
},
}),
};
} satisfies LoggerOptions;

const logger = !destination
? pino(loggerOptions)
Expand Down

0 comments on commit 79f22ae

Please sign in to comment.