How does IntelliJ discover Jasmine tests?
Using IntelliJ Ultimate 2019.1, we have a UI project with a Hybrid Stack with Angular and AngularJS using Jasmine for tests:
- AngularJS sources and tests are located in ./src/**
- Angular / TS sources and tests are located in ./projects/**
For reference, I opened the Angular tour of heroes tutorial project as well.
Using ng test from command line or Karma Run Config in IntelliJ, I can run all tests. However, when trying to interact with test files directly, it gets messy:
- In Tour of heroes, I can right click on the file to execute all tests of that file. And I can click on the play button next to each test function.
- In the AngularJS part, I can right click on the file to execute all tests of that file. No play buttons are displayed (so I cannot execute them)
- In the Angular part, No test run is displayed in context menu of a test file and not play buttons are displayed within the file.
My question to further investigate the issue: How does IntelliJ determine, if test file can be executed and if the play buttons are displayed in the file side bar? I assume that this is either a bug (or missing feature) in IntelliJ or a misconfiguration of our project. The be sure it's not the latter, I want to know how the test discovery works.
Thanks
Please sign in to leave a comment.
IDEA uses static code analysis to determine if a certain file is a test file (it looks for specific code patterns in the file). If a file is recognized as a test file, the IDE check for test runners available for it. The logic currently used for detecting what test runner is available for a given test file is based on dependencies declarations in
package.jsonnearest to this file.If the detected test runner supports running individual tests, the run icons will be shown in a file next to each test/suite. Otherwise, Run action will only be available from a file right-click menu. If no suitable (supported) test runner are found, there is no way to run a test from the right-click menu, you would need to create a run configuration manually
Hi Elena,
thanks for your response!
> IDEA uses static code analysis to determine if a certain file is a test file
So in out project, all tests are highlighted green in the project explorer, so I assume this step works.
> If a file is recognized as a test file, the IDE check for test runners available for it. The logic currently used for detecting what test runner is available for a given test file is based on dependencies declarations in
package.jsonnearest to this file.This is interesting, we have a full-fledged ./package.json in project root and one in projects/bci-ppm/core/package.json next to the Angular TS sources and tests. This one only has a version and peerDependencies. No other dependencies specified. So what exactly needs to be added here to match IDEAs logic? In our root package.json, there is "karma-chrome-launcher" as the only karma devDep. But I added it to the other package json and I still did not get the testing support. Do I need to restart the IDE / clear the cache to rerun the discovery mechanism?
Ok, for anyone interested, I was able to fix it after some experiments, I did the following:
- Remove the second package.json from ./projects/...
- Added the following devDependencies to package.json (Interestingly they were not necessary before to execute the tests but I seems that IntelliJ looks for one of them, before I only had the karma-chrome-launcher inlcuded, the others were present in the karma.conf.js):