Skip to content

Commit

Permalink
Add dist tests. Rename dist files to resemble the file structure of c…
Browse files Browse the repository at this point in the history
…roner@8.
  • Loading branch information
Hexagon committed Oct 6, 2024
1 parent b0ef543 commit 90fdf57
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 16 deletions.
15 changes: 9 additions & 6 deletions build/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ relativeProjectRoot = resolve(currentScriptDir, relativeProjectRoot);
const tsConfig = {
declaration: true,
compilerOptions: {
outFile: resolve(relativeProjectRoot, "dist", "croner.d.ts"),
outFile: resolve(relativeProjectRoot, "dist", "croner.min.d.ts"),
allowImportingTsExtensions: true,
target: "ES6",
},
Expand All @@ -28,8 +28,9 @@ async function build() {
// CommonJS build
await esbuild.build({
entryPoints: [resolve(relativeProjectRoot, "src/croner.ts")],
outfile: resolve(relativeProjectRoot, outputFolder, "croner.cjs"),
outfile: resolve(relativeProjectRoot, outputFolder, "croner.min.cjs"),
bundle: true,
minify: true,
platform: "node",
format: "cjs",
sourcemap: false,
Expand All @@ -39,8 +40,9 @@ async function build() {
// UMD build
await esbuild.build({
entryPoints: [resolve(relativeProjectRoot, "src/croner.ts")],
outfile: resolve(relativeProjectRoot, outputFolder, "croner.umd.js"),
outfile: resolve(relativeProjectRoot, outputFolder, "croner.umd.min.js"),
bundle: true,
minify: true,
platform: "browser",
format: "iife",
globalName: "croner",
Expand All @@ -51,8 +53,9 @@ async function build() {
// ESM build
await esbuild.build({
entryPoints: [resolve(relativeProjectRoot, "src/croner.ts")],
outfile: resolve(relativeProjectRoot, outputFolder, "croner.js"),
outfile: resolve(relativeProjectRoot, outputFolder, "croner.min.js"),
bundle: true,
minify: true,
platform: "neutral",
format: "esm",
sourcemap: false,
Expand All @@ -70,6 +73,6 @@ try {

// Copy .d.ts to .d.cts
await cp(
resolve(relativeProjectRoot, outputFolder, "croner.d.ts"),
resolve(relativeProjectRoot, outputFolder, "croner.d.cts"),
resolve(relativeProjectRoot, outputFolder, "croner.min.d.ts"),
resolve(relativeProjectRoot, outputFolder, "croner.min.d.cts"),
);
18 changes: 9 additions & 9 deletions build/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ async function generatePackageJson() {
"crontab",
],
"type": "module",
"main": "./dist/croner.cjs",
"browser": "./dist/croner.umd.js",
"module": "./dist/croner.js",
"types": "./dist/croner.d.ts",
"main": "./dist/croner.min.cjs",
"browser": "./dist/croner.umd.min.js",
"module": "./dist/croner.min.js",
"types": "./dist/croner.min.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/croner.d.ts",
"default": "./dist/croner.js",
"types": "./dist/croner.min.d.ts",
"default": "./dist/croner.min.js",
},
"require": {
"types": "./dist/croner.d.cts",
"default": "./dist/croner.cjs",
"types": "./dist/croner.min.d.cts",
"default": "./dist/croner.min.cjs",
},
"browser": "./dist/croner.umd.js",
"browser": "./dist/croner.umd.min.js",
},
},
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hexagon/croner",
"version": "9.0.0-dev.5",
"version": "9.0.0-dev.6",
"exports": "./src/croner.ts",
"lint": {
"include": ["src", "build"]
Expand Down
167 changes: 167 additions & 0 deletions test/croner.dist.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* A few sanity checks for the distributed build */

import { assertEquals, assertThrows } from "@std/assert";
import { test } from "@cross/test";
import { Cron } from "../dist/croner.min.js";

test("new Cron(...) should not throw", function () {
let scheduler = new Cron("* * * * * *");
scheduler.nextRun();
});

test("Array passed as next date should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * * * *");
scheduler.nextRun([]);
});
});

test("31st february should not be found", function () {
let scheduler = new Cron("* * * 31 2 *");
assertEquals(scheduler.nextRun(), null);
});

test("Too high days should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * 32 * *");
scheduler.nextRun();
});
});

test("Too low days should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * 0 * *");
scheduler.nextRun();
});
});

test("Valid months should not throw", function () {
let scheduler = new Cron("* * * * 1,2,3,4,5,6,7,8,9,10,11,12 *");
scheduler.nextRun();
});

test("Options as second argument should not throw", function () {
let scheduler = new Cron("* * * * * *", { maxRuns: 1 });
scheduler.nextRun();
});

test("Options as third argument should not throw", function () {
let scheduler = new Cron("* * * * * *", () => {}, { maxRuns: 1 });
scheduler.nextRun();
scheduler.stop();
});

test("Text as second argument should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * * * *", "bogus", { maxRuns: 1 });
scheduler.nextRun();
});
});

test("Text as third argument should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * * * *", { maxRuns: 1 }, "test");
scheduler.nextRun();
});
});

test("Too high months should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * * 7-13 *");
scheduler.nextRun();
});
});

test("Too low months should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * * 0-3 *");
scheduler.nextRun();
});
});

test("Valid weekdays should not throw", function () {
let scheduler = new Cron("* * * * * 0,1,2,3,4,5,6,7");
scheduler.nextRun();
});

test("Too high weekday should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * * * 8");
scheduler.nextRun();
});
});

test("Too low weekday should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * * * * -1");
scheduler.nextRun();
});
});

test("Too high hours minute should throw", function () {
assertThrows(() => {
let scheduler = new Cron("* * 0,23,24 * * *");
scheduler.nextRun();
});
});

test("Next 10 run times is returned by enumeration(), and contain a reasonable time span", () => {
let now = new Date(),
nextRuns = new Cron("*/30 * * * * *").nextRuns(10);

// Check number of times returned
assertEquals(nextRuns.length, 10);

// Check that time span of first entry is within a minute
assertEquals(nextRuns[0].getTime() >= now.getTime() - 1000, true);
assertEquals(nextRuns[0].getTime() <= now.getTime() + 61 * 1000, true);

// Check that time span of last entry is about 5 minutes from now
assertEquals(nextRuns[9].getTime() > now.getTime() + 4 * 60 * 1000, true);
assertEquals(nextRuns[9].getTime() < now.getTime() + 6 * 60 * 1000, true);
});

test("Extra whitespace at beginning should throw", () => {
assertThrows(() => {
new Cron(" 0 0 12 9 *").nextRun();
});
});

test("Extra whitespace at end should throw", () => {
assertThrows(() => {
new Cron("0 0 12 9 * ").nextRun();
});
});

test("Next 10 run times is returned by enumeration(), and contain a reasonable time span, when using modified start time", () => {
// 20 minutes before now
let now = new Date(new Date().getTime() - 1200 * 1000),
nextRuns = new Cron("0 * * * * *").nextRuns(10, now);

// Check number of times returned
assertEquals(nextRuns.length, 10);

// Check that time span of first entry is within a minute
assertEquals(nextRuns[0].getTime() >= now.getTime(), true);
assertEquals(nextRuns[0].getTime() <= now.getTime() + 61 * 1000, true);

// Check that time span of last entry is about 10 minutes from 'now'
assertEquals(nextRuns[9].getTime() > now.getTime() + 9 * 60 * 1000, true);
assertEquals(nextRuns[9].getTime() < now.getTime() + 11 * 60 * 1000, true);
});

test("@yearly should be replaced", function () {
let nextRuns = new Cron("@yearly").nextRuns(3, "2022-02-17T00:00:00");
assertEquals(nextRuns[0].getFullYear(), 2023);
assertEquals(nextRuns[0].getMonth(), 0);
assertEquals(nextRuns[0].getDate(), 1);
assertEquals(nextRuns[1].getFullYear(), 2024);
assertEquals(nextRuns[2].getFullYear(), 2025);
});

test("@annually should be replaced", function () {
let nextRuns = new Cron("@annually").nextRuns(3, "2022-02-17T00:00:00");
assertEquals(nextRuns[0].getFullYear(), 2023);
assertEquals(nextRuns[0].getMonth(), 0);
assertEquals(nextRuns[0].getDate(), 1);
});

0 comments on commit 90fdf57

Please sign in to comment.