Skip to content

Commit 2eb7e4a

Browse files
author
Esben Sparre Andreasen
committed
JS: classify x.test.js files with test(...) calls as jest tests
1 parent 5ebcef4 commit 2eb7e4a

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

javascript/ql/src/semmle/javascript/frameworks/Testing.qll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ class BDDTest extends Test, @callexpr {
3838
}
3939
}
4040

41+
/**
42+
* Gets the test file for `f` with stem extension `stemExt`.
43+
* That is, a file named file named `<base>.<stemExt>.<ext>` in the
44+
* same directory as `f` which is named `<base>.<ext>`.
45+
*/
46+
bindingset[stemExt]
47+
private File getTestFile(File f, string stemExt) {
48+
result = f.getParentContainer().getFile(f.getStem() + "." + stemExt + "." + f.getExtension())
49+
}
50+
51+
/**
52+
* A Jest test, that is, an invocation of a global function named
53+
* `test` where the first argument is a string and the second
54+
* argument is a function. Additionally, the invocation happens in a file
55+
* named `<base>.test.<ext>` in the same directory as a file named
56+
* `<base>.<ext>`.
57+
*/
58+
class JestTest extends Test, @callexpr {
59+
JestTest() {
60+
exists(CallExpr call | call = this |
61+
call.getCallee().(GlobalVarAccess).getName() = "test" and
62+
exists(call.getArgument(0).getStringValue()) and
63+
call.getArgument(1).analyze().getAValue() instanceof AbstractFunction
64+
) and
65+
getFile() = getTestFile(any(File f), "test")
66+
}
67+
}
68+
4169
/**
4270
* A xUnit.js fact, that is, a function annotated with an xUnit.js
4371
* `Fact` annotation.

javascript/ql/test/query-tests/filters/ClassifyFiles/ClassifyFiles.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| etherpad.html:0:0:0:0 | etherpad.html | generated |
88
| exported-data.js:0:0:0:0 | exported-data.js | generated |
99
| htmltidy.html:0:0:0:0 | htmltidy.html | generated |
10+
| implementation.test.js:0:0:0:0 | implementation.test.js | test |
1011
| jison-lex.js:0:0:0:0 | jison-lex.js | generated |
1112
| jison.js:0:0:0:0 | jison.js | generated |
1213
| jquery-datatables.js:0:0:0:0 | jquery-datatables.js | library |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var x = 42;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test("this should work", function(){});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test("this should work", function(){});

0 commit comments

Comments
 (0)