Skip to content

Commit

Permalink
Merge pull request #10 from saiichihashimoto/e2e-test
Browse files Browse the repository at this point in the history
integration tests
  • Loading branch information
kodiakhq[bot] authored Jan 13, 2024
2 parents b35e183 + 33c72d7 commit 881e6ed
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 52 deletions.
10 changes: 8 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@
"fp/no-rest-parameters": "off",
"fp/no-throw": "off",
"fp/no-unused-expression": "off",
"import/extensions": ["error", "never"],
"import/extensions": [
"error",
"never",
{
"json": "always"
}
],
"import/no-absolute-path": "error",
"import/no-amd": "error",
"import/no-anonymous-default-export": [
Expand Down Expand Up @@ -178,7 +184,7 @@
{
"missingExports": true,
"unusedExports": true,
"ignoreExports": ["./*", "./src/bin.ts", "**/*.d.ts"]
"ignoreExports": ["./*", "./src/bin.ts", "**/*.d.ts", "**/*.test.*"]
}
],
"import/no-useless-path-segments": "error",
Expand Down
4 changes: 0 additions & 4 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"fakeTimers": {
"enableGlobally": true,
"now": 1696486441293
},
"passWithNoTests": true,
"preset": "ts-jest",
"restoreMocks": true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"build": "tsup",
"dev": "concurrently --prefix-colors \"auto\" npm:dev:*",
"dev:build": "npm run build -- --watch --onSuccess \"./dist/bin.js --help && ./dist/bin.js --level trace\"",
"dev:build": "npm run build -- --watch --onSuccess \"./dist/bin.js --version && ./dist/bin.js --help && ./dist/bin.js --level trace\"",
"dev:test": "npm run test -- --watch",
"lint": "lint-staged --shell --verbose --diff \"4b825dc642cb6eb9a060e54bf8d69288fbee4904\" --no-stash",
"prepare": "is-ci || husky install",
Expand Down
56 changes: 56 additions & 0 deletions src/bin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { exec as execCallback } from "node:child_process";
import { promisify } from "node:util";

import { beforeAll, describe, expect, it } from "@jest/globals";

// eslint-disable-next-line global-require, @typescript-eslint/no-require-imports, unicorn/prefer-module, @typescript-eslint/no-unused-vars -- HACK We're "including" bin by running a process against the built file so jest won't pick it up with `--findRelatedTests`.
const helpJestFindRelatedTests = () => require("./bin");

const exec = promisify(execCallback);

describe("bin", () => {
beforeAll(async () => {
await exec("npm run build");
});

it("--version", async () => {
const { stderr, stdout } = await exec("node ./dist/bin.js --version");
expect(stderr).toBe("");
expect(stdout).toBe("0.0.0-development\n");
});

it("--help", async () => {
const { stderr, stdout } = await exec("node ./dist/bin.js --help");

expect(stderr).toBe("");
expect(stdout).toBe(`Usage: vercel-cron [options]
Options:
-V, --version output the version number
-u --url <url> Base URL (default: "http://localhost:3000")
-p --config <config> Vercel Config (default: "./vercel.json")
-s --secret <secret> Cron Secret
--dryRun Shows scheduled CRONs and quits (default: false)
-l --level <level> Logging Level (choices: "trace", "debug", "info",
"warn", "error", "fatal", default: "info")
-h, --help display help for command
`);
});

it("runs with empty vercel.json", async () => {
const { stderr, stdout } = await exec(
"node ./dist/bin.js --ignoreTime --dryRun",
{ env: { ...process.env, FORCE_COLOR: "0" } }
);

expect(stderr).toBe("");
expect(stdout).toBe(`╭─────────────────────────╮
│ │
│ ▲ Vercel CRON ▲ │
│ │
╰─────────────────────────╯
INFO: No CRONs Scheduled
`);
});
});
30 changes: 28 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
#!/usr/bin/env node
import { main } from ".";
import { Command, Option } from "commander";

main();
import { main, zOpts } from ".";
import pkg from "../package.json";

main(
zOpts.parse(
new Command()
.name(pkg.name)
.version(pkg.version)
.option("-u --url <url>", "Base URL", "http://localhost:3000")
.option("-p --config <config>", "Vercel Config", "./vercel.json")
.addOption(
new Option("-s --secret <secret>", "Cron Secret").default(
process.env.CRON_SECRET,
"process.env.CRON_SECRET"
)
)
.option("--dryRun", "Shows scheduled CRONs and quits", false)
.addOption(
new Option("-l --level <level>", "Logging Level")
.default("info")
.choices(["trace", "debug", "info", "warn", "error", "fatal"])
)
.addOption(new Option("--ignoreTime").hideHelp())
.parse()
.opts()
)
);
66 changes: 27 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from "node:fs/promises";

import boxen from "boxen";
import chalk from "chalk";
import { Command, Option } from "commander";
import chalk, { supportsColor } from "chalk";
import { Cron } from "croner";
import cronstrue from "cronstrue";
import pino from "pino";
Expand Down Expand Up @@ -48,41 +47,23 @@ const unshiftIterable = async function* <T>(
}
};

export const main = async () => {
const opts = z
.object({
url: z.string(),
config: z.string(),
secret: z.optional(z.string()),
level: z.union([
z.literal("trace"),
z.literal("debug"),
z.literal("info"),
z.literal("warn"),
z.literal("error"),
z.literal("fatal"),
]),
})
.parse(
new Command()
.name("vercel-cron")
.option("-u --url <url>", "Base URL", "http://localhost:3000")
.option("-p --config <config>", "Vercel Config", "./vercel.json")
.addOption(
new Option("-s --secret <secret>", "Cron Secret").default(
process.env.CRON_SECRET,
"process.env.CRON_SECRET"
)
)
.addOption(
new Option("-l --level <level>", "Logging Level")
.default("info")
.choices(["trace", "debug", "info", "warn", "error", "fatal"])
)
.parse()
.opts()
);

export const zOpts = z.object({
url: z.string(),
config: z.string(),
secret: z.optional(z.string()),
ignoreTime: z.optional(z.boolean()),
dryRun: z.boolean(),
level: z.union([
z.literal("trace"),
z.literal("debug"),
z.literal("info"),
z.literal("warn"),
z.literal("error"),
z.literal("fatal"),
]),
});

export const main = async (opts: z.infer<typeof zOpts>) => {
// eslint-disable-next-line no-console -- boxen!
console.log(
boxen("▲ Vercel CRON ▲", {
Expand All @@ -93,15 +74,16 @@ export const main = async () => {
})
);

const { config, level, secret, url } = opts;
const { config, dryRun, ignoreTime, level, secret, url } = opts;

const logger = pino({
level,
// timestamp: false,
timestamp: !ignoreTime,
errorKey: "error",
transport: {
target: "pino-pretty",
options: {
colorize: Boolean(supportsColor),
float: "center",
levelFirst: true,
singleLine: true,
Expand Down Expand Up @@ -205,5 +187,11 @@ export const main = async () => {
}

abortPrevious = await watchConfig();

if (dryRun) {
abortPrevious();

break;
}
}
};
2 changes: 1 addition & 1 deletion stryker.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"thresholds": {
"high": 95,
"low": 90,
"break": 85
"break": 40
}
}
4 changes: 1 addition & 3 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"$schema": "https://openapi.vercel.sh/vercel.json"
}
{}

0 comments on commit 881e6ed

Please sign in to comment.