Skip to content

Commit 0f88b48

Browse files
refactor: remove mockedFunction for Node.js test runner mock method (#201)
1 parent 8d8abe0 commit 0f88b48

File tree

4 files changed

+73
-89
lines changed

4 files changed

+73
-89
lines changed

test/probes/isArrayExpression.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

test/probes/isArrayExpression.spec.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Import Node.js dependencies
2+
import { test } from "node:test";
3+
import assert from "node:assert";
4+
5+
// Import Internal Dependencies
6+
import { getSastAnalysis, parseScript } from "../utils/index.js";
7+
import isArrayExpression from "../../src/probes/isArrayExpression.js";
8+
9+
test("it should trigger analyzeLiteral method one time", (t) => {
10+
const str = "['foo']";
11+
12+
const ast = parseScript(str);
13+
const sastAnalysis = getSastAnalysis(str, isArrayExpression);
14+
15+
t.mock.method(sastAnalysis.analysis, "analyzeLiteral");
16+
sastAnalysis.execute(ast.body);
17+
18+
assert.strictEqual(sastAnalysis.warnings().length, 0);
19+
20+
const calls = sastAnalysis.analysis.analyzeLiteral.mock.calls;
21+
assert.strictEqual(calls.length, 1);
22+
23+
const literalNode = calls[0].arguments[0];
24+
assert.strictEqual(literalNode.value, "foo");
25+
});
26+
27+
test("it should trigger analyzeLiteral method two times (ignoring the holey between)", (t) => {
28+
const str = "[5, ,10]";
29+
30+
const ast = parseScript(str);
31+
const sastAnalysis = getSastAnalysis(str, isArrayExpression);
32+
33+
t.mock.method(sastAnalysis.analysis, "analyzeLiteral");
34+
sastAnalysis.execute(ast.body);
35+
36+
const calls = sastAnalysis.analysis.analyzeLiteral.mock.calls;
37+
assert.strictEqual(calls.length, 2);
38+
assert.strictEqual(calls[0].arguments[0].value, 5);
39+
assert.strictEqual(calls[1].arguments[0].value, 10);
40+
});
41+
42+
test("it should trigger analyzeLiteral one time (ignoring non-literal Node)", (t) => {
43+
const str = "[5, () => void 0]";
44+
45+
const ast = parseScript(str);
46+
const sastAnalysis = getSastAnalysis(str, isArrayExpression);
47+
48+
t.mock.method(sastAnalysis.analysis, "analyzeLiteral");
49+
sastAnalysis.execute(ast.body);
50+
51+
const calls = sastAnalysis.analysis.analyzeLiteral.mock.calls;
52+
assert.strictEqual(calls.length, 1);
53+
54+
const literalNode = calls[0].arguments[0];
55+
assert.strictEqual(literalNode.value, 5);
56+
});

test/probes/isLiteral.spec.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,43 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { getSastAnalysis, parseScript, mockedFunction } from "../utils/index.js";
6+
import { getSastAnalysis, parseScript } from "../utils/index.js";
77
import isLiteral from "../../src/probes/isLiteral.js";
88

9-
test("should throw an unsafe-import because the hexadecimal string is equal to the core 'http' dependency", () => {
9+
test("should throw an unsafe-import because the hexadecimal string is equal to the core 'http' dependency", (t) => {
1010
const str = "const foo = '68747470'";
1111
const ast = parseScript(str);
1212

13-
const analyzeStringMock = mockedFunction();
1413
const sastAnalysis = getSastAnalysis(str, isLiteral);
15-
sastAnalysis.analysis.analyzeString = analyzeStringMock.callback.bind(analyzeStringMock);
14+
t.mock.method(sastAnalysis.analysis, "analyzeString");
1615
sastAnalysis.execute(ast.body);
1716

1817
assert.strictEqual(sastAnalysis.warnings().length, 1);
1918
const warning = sastAnalysis.getWarning("unsafe-import");
2019
assert.strictEqual(warning.kind, "unsafe-import");
2120

2221
assert.ok(sastAnalysis.dependencies().has("http"));
23-
assert.ok(analyzeStringMock.haveBeenCalledTimes(1));
24-
assert.ok(analyzeStringMock.haveBeenCalledWith("http"));
22+
const calls = sastAnalysis.analysis.analyzeString.mock.calls;
23+
assert.strictEqual(calls.length, 1);
24+
assert.ok(calls[0].arguments.includes("http"));
2525
});
2626

27-
test("should throw an encoded-literal warning because the hexadecimal value is equal to 'require'", () => {
27+
28+
test("should throw an encoded-literal warning because the hexadecimal value is equal to 'require'", (t) => {
2829
const str = "const _t = globalThis['72657175697265']";
2930
const ast = parseScript(str);
3031

31-
const analyzeStringMock = mockedFunction();
3232
const sastAnalysis = getSastAnalysis(str, isLiteral);
33-
sastAnalysis.analysis.analyzeString = analyzeStringMock.callback.bind(analyzeStringMock);
33+
t.mock.method(sastAnalysis.analysis, "analyzeString");
3434
sastAnalysis.execute(ast.body);
3535

3636
assert.strictEqual(sastAnalysis.warnings().length, 1);
3737
const warning = sastAnalysis.getWarning("encoded-literal");
3838
assert.strictEqual(warning.value, "72657175697265");
3939

40-
assert.ok(analyzeStringMock.haveBeenCalledTimes(1));
41-
assert.ok(analyzeStringMock.haveBeenCalledWith("require"));
40+
const calls = sastAnalysis.analysis.analyzeString.mock.calls;
41+
assert.strictEqual(calls.length, 1);
42+
assert.ok(calls[0].arguments.includes("require"));
4243
});
4344

4445
test("should not throw an encoded-literal warning because hexadecimal value is safe", () => {
@@ -62,19 +63,19 @@ test("should throw an encoded-literal warning because hexadecimal value is not s
6263
assert.strictEqual(warning.value, "68656c6c6f20776f726c64");
6364
});
6465

65-
test("should not throw any warnings without hexadecimal value (and should call analyzeLiteral of Analysis class)", () => {
66+
test("should not throw any warnings without hexadecimal value (and should call analyzeLiteral of Analysis class)", (t) => {
6667
const str = "const foo = 'hello world!'";
6768
const ast = parseScript(str);
6869

69-
const analyzeLiteralMock = mockedFunction();
7070
const sastAnalysis = getSastAnalysis(str, isLiteral);
71-
sastAnalysis.analysis.analyzeLiteral = analyzeLiteralMock.callback.bind(analyzeLiteralMock);
71+
t.mock.method(sastAnalysis.analysis, "analyzeLiteral");
7272
sastAnalysis.execute(ast.body);
7373

7474
assert.strictEqual(sastAnalysis.warnings().length, 0);
75-
assert.ok(analyzeLiteralMock.haveBeenCalledTimes(1));
75+
const calls = sastAnalysis.analysis.analyzeLiteral.mock.calls;
76+
assert.strictEqual(calls.length, 1);
7677

77-
const astNode = analyzeLiteralMock.args[0];
78+
const astNode = calls[0].arguments[0];
7879
assert.strictEqual(astNode.value, "hello world!");
7980
});
8081

test/utils/index.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,6 @@ export function getWarningKind(warnings) {
1010
return warnings.slice().map((warn) => warn.kind).sort();
1111
}
1212

13-
export function mockedFunction() {
14-
return {
15-
called: 0,
16-
args: [],
17-
at(position) {
18-
return this.args[position];
19-
},
20-
haveBeenCalledTimes(count = 0) {
21-
return this.called === count;
22-
},
23-
haveBeenCalledWith(value) {
24-
return this.args.includes(value);
25-
},
26-
callback(...args) {
27-
this.args.push(...args);
28-
this.called++;
29-
}
30-
};
31-
}
32-
3313
export function parseScript(str) {
3414
return meriyah.parseScript(str, {
3515
next: true,

0 commit comments

Comments
 (0)