Skip to content

Commit f5d744d

Browse files
authored
test(utils): add unit tests for isOneLineExpressionExport (#539)
* test(utils): add unit tests for isOneLineExpressionExport * chore: add changeset for isOneLineExpressionExport tests * style: update isOneLineExpressionExport.spec.ts * refactor(test): destructure body from parseScript
1 parent f548a44 commit f5d744d

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

.changeset/solid-wasps-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/js-x-ray": patch
3+
---
4+
5+
Add unit tests for isOneLineExpressionExport utility.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Import Node.js Dependencies
2+
import assert from "node:assert";
3+
import { describe, it } from "node:test";
4+
5+
// Import Third-party Dependencies
6+
import { parseScript } from "meriyah";
7+
8+
// Import Internal Dependencies
9+
import { isOneLineExpressionExport } from "../../src/utils/index.ts";
10+
11+
describe("isOneLineExpressionExport()", () => {
12+
it("should return false for empty body", () => {
13+
assert.strictEqual(isOneLineExpressionExport([]), false);
14+
});
15+
16+
it("should return false for multiple statements", () => {
17+
const { body } = parseScript(`
18+
require('a');
19+
require('b');
20+
`);
21+
assert.strictEqual(isOneLineExpressionExport(body), false);
22+
});
23+
24+
it("should return true for single require call", () => {
25+
const { body } = parseScript("require('a');");
26+
assert.strictEqual(isOneLineExpressionExport(body), true);
27+
});
28+
29+
it("should return true for module.exports assignment to require", () => {
30+
const { body } = parseScript("module.exports = require('a');");
31+
assert.strictEqual(isOneLineExpressionExport(body), true);
32+
});
33+
34+
it("should return true for conditional require export", () => {
35+
const { body } = parseScript(`
36+
module.exports = condition ? require('a') : require('b');
37+
`);
38+
assert.strictEqual(isOneLineExpressionExport(body), true);
39+
});
40+
41+
it("should return true for logical require export", () => {
42+
const { body } = parseScript(`
43+
module.exports = condition && require('b');
44+
`);
45+
assert.strictEqual(isOneLineExpressionExport(body), true);
46+
});
47+
48+
it("should return false for non-require expression", () => {
49+
const { body } = parseScript("module.exports = foo();");
50+
assert.strictEqual(isOneLineExpressionExport(body), false);
51+
});
52+
53+
it("should return true for require member access", () => {
54+
const { body } = parseScript(`
55+
module.exports = require("foo").bar.baz;
56+
`);
57+
assert.strictEqual(isOneLineExpressionExport(body), true);
58+
});
59+
60+
it("should return false for non-require member access", () => {
61+
const { body } = parseScript(`module.exports = something.require("foo");`);
62+
assert.strictEqual(isOneLineExpressionExport(body), false);
63+
});
64+
});

0 commit comments

Comments
 (0)