Skip to content

Commit

Permalink
overhaul: make unit tests dependency-less
Browse files Browse the repository at this point in the history
  • Loading branch information
marshift committed Aug 29, 2024
1 parent d36a574 commit b3940c2
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 1,204 deletions.
641 changes: 2 additions & 639 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "spitroast",
"version": "2.0.3",
"version": "2.0.4",
"description": "A simple JavaScript function patcher.",
"main": "dist/cjs.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"scripts": {
"test": "npm run prepublish && mocha",
"prepublish": "tsup ./src/index.ts --dts --format esm,cjs"
"prepublish": "tsup ./src/index.ts --dts --format esm,cjs",
"test": "npm run prepublish && node --no-warnings --import ./test/setup.mjs --test ./test/index.mjs"
},
"repository": {
"type": "git",
Expand All @@ -17,8 +17,6 @@
"license": "CC0-1.0",
"devDependencies": {
"@types/node": "^22.5.1",
"chai": "^5.1.1",
"mocha": "^10.7.3",
"tsup": "^8.2.4",
"typescript": "^5.5.4"
}
Expand Down
396 changes: 2 additions & 394 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions test/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "./suites/after.mjs";
import "./suites/before.mjs";
import "./suites/instead.mjs";
import "./suites/unpatches.mjs";
20 changes: 20 additions & 0 deletions test/setup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { notEqual as isNotEqual } from "node:assert/strict";
import { beforeEach } from "node:test";

beforeEach(() => {
globalThis.testFuncs = {};

// for testing basic single patch-unpatch
testFuncs.simple = (a, b) => a + b;

// now we know that works, for testing context
testFuncs.contextual = function (a) {
isNotEqual(this?.x, undefined);
isNotEqual(this.y, undefined);
isNotEqual(this.z, undefined);
return (this.x - a) / this.y + this.z;
};

// now we know patching works, for testing patch composing
testFuncs.passthru = (a) => a;
});
166 changes: 0 additions & 166 deletions test/spitroast.test.mjs

This file was deleted.

31 changes: 31 additions & 0 deletions test/suites/after.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { equal as isEqual } from "node:assert/strict";
import { describe, it } from "node:test";
import { after } from "../../dist/index.mjs";

describe("spitroast after patches", () => {
it("should patch a simple func", () => {
after("simple", testFuncs, ([, b], ret) => ret * b);

isEqual(testFuncs.simple(1, 2), 6);
});

it("should be unpatchable", () => {
const unpatch = after("simple", testFuncs, () => 0);

unpatch();

isEqual(testFuncs.simple(1, 2), 3);
});

it("should maintain context", () => {
after("contextual", testFuncs, function () {
isEqual(this?.x, 17);
isEqual(this.y, 5);
isEqual(this.z, "test");
});

const ctx = { x: 17, y: 5, z: "test" };

isEqual(testFuncs.contextual.call(ctx, 1), "3.2test");
});
});
31 changes: 31 additions & 0 deletions test/suites/before.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { equal as isEqual } from "node:assert/strict";
import { describe, it } from "node:test";
import { before } from "../../dist/index.mjs";

describe("spitroast before patches", () => {
it("should patch a simple func", () => {
before("simple", testFuncs, ([a, b]) => [a + b, a * b]);

isEqual(testFuncs.simple(1, 2), 5);
});

it("should be unpatchable", () => {
const unpatch = before("simple", testFuncs, () => [0, 0]);

unpatch();

isEqual(testFuncs.simple(1, 2), 3);
});

it("should maintain context", () => {
before("contextual", testFuncs, function () {
isEqual(this?.x, 17);
isEqual(this.y, 5);
isEqual(this.z, "test");
});

const ctx = { x: 17, y: 5, z: "test" };

isEqual(testFuncs.contextual.call(ctx, 1), "3.2test");
});
});
33 changes: 33 additions & 0 deletions test/suites/instead.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { equal as isEqual } from "node:assert/strict";
import { describe, it } from "node:test";
import { instead } from "../../dist/index.mjs";

describe("spitroast instead patches", () => {
it("should patch a simple func", () => {
instead("simple", testFuncs, ([a, b], orig) => orig(a + b, b - a) * b);

isEqual(testFuncs.simple(1, 2), 8);
});

it("should be unpatchable", () => {
const unpatch = instead("simple", testFuncs, () => 0);

unpatch();

isEqual(testFuncs.simple(1, 2), 3);
});

it("should maintain context", () => {
instead("contextual", testFuncs, function (args, orig) {
isEqual(this?.x, 17);
isEqual(this.y, 5);
isEqual(this.z, "test");

return orig.apply(this, args);
});

const ctx = { x: 17, y: 5, z: "test" };

isEqual(testFuncs.contextual.call(ctx, 1), "3.2test");
});
});
49 changes: 49 additions & 0 deletions test/suites/unpatches.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { equal as isEqual } from "node:assert/strict";
import { describe, it } from "node:test";
import { after, before, instead, unpatchAll } from "../../dist/index.mjs";

describe("spitroast unpatches", () => {
it("should be able to unpatch the most recent on a given func", () => {
after("passthru", testFuncs, ([], ret) => ret + "a");
const unpatch = after("passthru", testFuncs, ([], ret) => ret + "b");

unpatch();
isEqual(testFuncs.passthru("x_"), "x_a");
});

it("should be able to unpatch the first on a given func", () => {
const unpatch = after("passthru", testFuncs, ([], ret) => ret + "a");
after("passthru", testFuncs, ([], ret) => ret + "b");

unpatch();
isEqual(testFuncs.passthru("x_"), "x_b");
});

it("should be able to unpatch an in-between on a given func", () => {
after("passthru", testFuncs, ([], ret) => ret + "a");
const unpatch = after("passthru", testFuncs, ([], ret) => ret + "b");
after("passthru", testFuncs, ([], ret) => ret + "c");

unpatch();
isEqual(testFuncs.passthru("x_"), "x_ac");
});

it("should be able to completely unpatch", () => {
before("simple", testFuncs, ([a, b]) => [a + 1, b + 1]);
after("simple", testFuncs, ([], ret) => ret / 2);

after("passthru", testFuncs, ([], ret) => ret + "_patched");

instead("contextual", testFuncs, ([a], orig) =>
orig.call({ x: 1, y: 1, z: "a" }, a - 4),
);

unpatchAll();

const ctx = { x: 17, y: 5, z: "test" };

isEqual(testFuncs.simple(1, 2), 3);
isEqual(testFuncs.contextual.call(ctx, 1), "3.2test");
isEqual(testFuncs.passthru("x"), "x");
});
});

0 comments on commit b3940c2

Please sign in to comment.