Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sunny-kiwis-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/js-x-ray": patch
---

Add unit tests for isMinifiedCode utility.
63 changes: 63 additions & 0 deletions workspaces/js-x-ray/test/utils/isMinifiedCode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Import Node.js Dependencies
import assert from "node:assert";
import { describe, it } from "node:test";

// Import Internal Dependencies
import { isMinifiedCode } from "../../src/utils/index.ts";

describe("isMinifiedCode", () => {
it("should return false for formatted multi-line code", () => {
const formattedCode = `
function add(a, b) {
return a + b;
}
`;
assert.strictEqual(isMinifiedCode(formattedCode), false);
});

it("should return true for a single line code", () => {
const minifiedCode = "function add(a,b){return a+b;}";
assert.strictEqual(isMinifiedCode(minifiedCode), true);
});

it("should return true when median line length exceeds 200", () => {
const longString = "a".repeat(250);
const code = `
${longString}
${longString}
`;
assert.strictEqual(isMinifiedCode(code), true);
});

it("should ignore comments when evaluating minification", () => {
const code = `
// this is a comment
function test() {
return 1;
}
`;
assert.strictEqual(isMinifiedCode(code), false);
});

it("should handle empty code as minified", () => {
assert.strictEqual(isMinifiedCode(""), true);
});

it("should treat comment-only code as minified", () => {
const code = `
// comment
/* block comment */
`;
assert.strictEqual(isMinifiedCode(code), true);
});

it("should not be affected by a single long line", () => {
const longLine = "a".repeat(250);
const code = `
short line
${longLine}
short line
`;
assert.strictEqual(isMinifiedCode(code), false);
});
});