Skip to content

Commit 97c0c97

Browse files
author
Max Schaefer
committed
JavaScript: Classify __mocks__ and __tests_ as tests.
These are conventions used by jest: https://jestjs.io/docs/en/manual-mocks#mocking-user-modules.
1 parent d83faaf commit 97c0c97

File tree

5 files changed

+12
-0
lines changed

5 files changed

+12
-0
lines changed

change-notes/1.22/analysis-javascript.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## General improvements
44

5+
* Automatic classification of test files has been improved, in particular `__tests__` and `__mocks__` folders (as used by [Jest](https://jestjs.io)) are now recognized.
6+
57
* Support for the following frameworks and libraries has been improved:
68
- [cross-spawn](https://www.npmjs.com/package/cross-spawn)
79
- [cross-spawn-async](https://www.npmjs.com/package/cross-spawn-async)

javascript/ql/src/filters/ClassifyFiles.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ predicate classify(File f, string category) {
6767
exists(string stemExt | stemExt = "test" or stemExt = "spec" |
6868
f = getTestFile(any(File orig), stemExt)
6969
)
70+
or
71+
f.getAbsolutePath().regexpMatch(".*/__(mocks|tests)__/.*")
7072
) and
7173
category = "test"
7274
or

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
| 1.js:0:0:0:0 | 1.js | generated |
22
| AutoRest.js:0:0:0:0 | AutoRest.js | generated |
33
| ManyElementsOnLine.html:0:0:0:0 | ManyElementsOnLine.html | generated |
4+
| __mocks__/test.js:0:0:0:0 | __mocks__/test.js | test |
5+
| __tests__/test.js:0:0:0:0 | __tests__/test.js | test |
46
| ai.1.2.3-build0123.js:0:0:0:0 | ai.1.2.3-build0123.js | library |
57
| bundle-directive.js:0:0:0:0 | bundle-directive.js | generated |
68
| data.js:0:0:0:0 | data.js | generated |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// the only reason to classify this file as a test is that it is in a folder called `__mocks__`
2+
3+
export function f() {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// the only reason to classify this file as a test is that it is in a folder called `__tests__`
2+
3+
export function testF() {}

0 commit comments

Comments
 (0)