@@ -3,42 +3,43 @@ import { test } from "node:test";
3
3
import assert from "node:assert" ;
4
4
5
5
// Import Internal Dependencies
6
- import { getSastAnalysis , parseScript , mockedFunction } from "../utils/index.js" ;
6
+ import { getSastAnalysis , parseScript } from "../utils/index.js" ;
7
7
import isLiteral from "../../src/probes/isLiteral.js" ;
8
8
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 ) => {
10
10
const str = "const foo = '68747470'" ;
11
11
const ast = parseScript ( str ) ;
12
12
13
- const analyzeStringMock = mockedFunction ( ) ;
14
13
const sastAnalysis = getSastAnalysis ( str , isLiteral ) ;
15
- sastAnalysis . analysis . analyzeString = analyzeStringMock . callback . bind ( analyzeStringMock ) ;
14
+ t . mock . method ( sastAnalysis . analysis , "analyzeString" ) ;
16
15
sastAnalysis . execute ( ast . body ) ;
17
16
18
17
assert . strictEqual ( sastAnalysis . warnings ( ) . length , 1 ) ;
19
18
const warning = sastAnalysis . getWarning ( "unsafe-import" ) ;
20
19
assert . strictEqual ( warning . kind , "unsafe-import" ) ;
21
20
22
21
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" ) ) ;
25
25
} ) ;
26
26
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 ) => {
28
29
const str = "const _t = globalThis['72657175697265']" ;
29
30
const ast = parseScript ( str ) ;
30
31
31
- const analyzeStringMock = mockedFunction ( ) ;
32
32
const sastAnalysis = getSastAnalysis ( str , isLiteral ) ;
33
- sastAnalysis . analysis . analyzeString = analyzeStringMock . callback . bind ( analyzeStringMock ) ;
33
+ t . mock . method ( sastAnalysis . analysis , "analyzeString" ) ;
34
34
sastAnalysis . execute ( ast . body ) ;
35
35
36
36
assert . strictEqual ( sastAnalysis . warnings ( ) . length , 1 ) ;
37
37
const warning = sastAnalysis . getWarning ( "encoded-literal" ) ;
38
38
assert . strictEqual ( warning . value , "72657175697265" ) ;
39
39
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" ) ) ;
42
43
} ) ;
43
44
44
45
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
62
63
assert . strictEqual ( warning . value , "68656c6c6f20776f726c64" ) ;
63
64
} ) ;
64
65
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 ) => {
66
67
const str = "const foo = 'hello world!'" ;
67
68
const ast = parseScript ( str ) ;
68
69
69
- const analyzeLiteralMock = mockedFunction ( ) ;
70
70
const sastAnalysis = getSastAnalysis ( str , isLiteral ) ;
71
- sastAnalysis . analysis . analyzeLiteral = analyzeLiteralMock . callback . bind ( analyzeLiteralMock ) ;
71
+ t . mock . method ( sastAnalysis . analysis , "analyzeLiteral" ) ;
72
72
sastAnalysis . execute ( ast . body ) ;
73
73
74
74
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 ) ;
76
77
77
- const astNode = analyzeLiteralMock . args [ 0 ] ;
78
+ const astNode = calls [ 0 ] . arguments [ 0 ] ;
78
79
assert . strictEqual ( astNode . value , "hello world!" ) ;
79
80
} ) ;
80
81
0 commit comments